如何在Angular 6中使用Microsoft Office-js定义Excel [英] How to define Excel using microsoft office-js in Angular 6

查看:312
本文介绍了如何在Angular 6中使用Microsoft Office-js定义Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注

I am following this site to integrate office js

在这样声明时:

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

import * as Excel from '@microsoft/office-js/excel' It's showing compile time error:

找不到模块'@ microsoft/office-js/excel'

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

在这样声明时:

declare var Excel: any

它显示了运行时错误:

错误ReferenceError:未定义Excel

ERROR ReferenceError: Excel is not defined

请提出如何声明它.我需要这样使用它:

Please suggest how to declare it. I need to use it like this:

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

推荐答案

安装办公室类型定义:

npm install --save @types/office-js

在tsconfig.app.json中添加:

In tsconfig.app.json you add:

"types": [
    "office-js"
]

在main.ts中,您将应用程序引导为:

In main.ts you bootstrap app as:

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

在index.html中添加:

In index.html you add:

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

之后,您应该可以在角度服务中编写如下内容:

After that you should be able to write something like this in your angular service:

  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;
    }
  }

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

Here you can find a sample of angular office-js project:

Angular office-js示例项目

它是使用Angular CLI版本6.0.8的Angular 6构建

It is Angular 6 build with Angular CLI version 6.0.8

这篇关于如何在Angular 6中使用Microsoft Office-js定义Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆