首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Typescript编译输出中包含.xlsx和.docx文件?

如何在Typescript编译输出中包含.xlsx和.docx文件?
EN

Stack Overflow用户
提问于 2019-05-21 01:56:56
回答 1查看 365关注 0票数 0

我在我的服务器上使用Typescript和NodeJS。

作为服务器逻辑的一部分,我需要操作.xlsx和.docx文件,但它们不包括在Typescript编译输出中。.xlsx和.docx文件用作创建其他文件的模板。

这是我的项目的样子:

代码语言:javascript
运行
复制
package.json
package-lock.json
ormconfig.json
tsconfig.json
/src
  /reports
    --other-.ts-files
    Router.ts
    Report.xlsx //File that I want to be included in the Typescript compilation output
    Report.docx //File that I want to be included in the Typescript compilation output

如何在Typescript编译输出中包含.xlsx和.docx文件?我需要向tsconfig.json文件添加什么内容?

EN

回答 1

Stack Overflow用户

发布于 2019-05-21 04:17:42

1)有可能向nodejs添加额外的扩展,但这种方式已经被弃用

代码语言:javascript
运行
复制
var fs = require('fs');

require.extensions['.txt'] = function (module, filename) {
    module.exports = fs.readFileSync(filename, 'utf8');
};

var words = require("./words.txt");

2)您始终可以使用node的file API读取文件

代码语言:javascript
运行
复制
const fs = require('fs')
const path = require('path')
const css = fs.readFileSync(path.resolve(__dirname, 'email.css'), 'utf8')

3)结合typescript和webpack可以加载自定义文件类型。例如,在webpack + ts中,SVG文件就是这样加载的。在这种情况下,Typescript需要额外的类型,例如(这是前端应用程序更常见的方式):

代码语言:javascript
运行
复制
declare module "*.svg" {
  const content: any;
  export default content;
}

4)用于阅读XLSX表的第三个库是一个很好的库,称为xlsx

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56225961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档