我在我的服务器上使用Typescript和NodeJS。
作为服务器逻辑的一部分,我需要操作.xlsx和.docx文件,但它们不包括在Typescript编译输出中。.xlsx和.docx文件用作创建其他文件的模板。
这是我的项目的样子:
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文件添加什么内容?
发布于 2019-05-21 04:17:42
1)有可能向nodejs添加额外的扩展,但这种方式已经被弃用
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读取文件
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需要额外的类型,例如(这是前端应用程序更常见的方式):
declare module "*.svg" {
const content: any;
export default content;
}
4)用于阅读XLSX表的第三个库是一个很好的库,称为xlsx
https://stackoverflow.com/questions/56225961
复制相似问题