如何在 Angular 6 中使用 microsoft office-js 定义 Excel

How to define Excel using microsoft office-js in Angular 6

我正在关注这个site来集成office js

像这样声明:

import * as Excel from '@microsoft/office-js/excel' 它显示编译时错误:

Cannot find module '@microsoft/office-js/excel'

像这样声明:

declare var Excel: any

显示运行时间错误:

ERROR ReferenceError: Excel is not defined

请建议如何申报。我需要像这样使用它:

Excel.run(session, (context){
   ...
});

安装办公室类型定义:

npm install --save @types/office-js

在 tsconfig.app.json 中添加:

"types": [
    "office-js"
]

在 main.ts 您 bootstrap 应用中:

    Office.initialize = function () {
    platformBrowserDynamic().bootstrapModule(AppModule);
};

在 index.html 中添加:

<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>

之后你应该可以在你的 angular 服务中写这样的东西:

  getExtension(): string {
    if (Office.context.host === Office.HostType.Word) {
      return '.docx';
    } else if  (Office.context.host === Office.HostType.Excel) {
      return '.xlsx';
    } else if (Office.context.host === Office.HostType.PowerPoint) {
      return '.pptx';
    } else {
      return null;
    }
  }

在这里您可以找到 angular office-js 项目的示例:

Angular office-js sample project

它是Angular 6 build with Angular CLI version 6.0.8