在我的应用中,我需要单击一个按钮以Word文档的形式生成报告。 使用“ officeGen”模块,我已经在一个单独的项目中做到了这一点。 https://www.npmjs.com/package/officegen
generateDoc.ts
const officegen = require('officegen')
const fs = require('fs')
// Create an empty PowerPoint object:
let pptx = officegen('pptx')
// Officegen calling this function after finishing to generate the pptx document:
pptx.on('finalize', function(written) {
console.log(
'Finish to create a Microsoft PowerPoint document.'
)
})
// Officegen calling this function to report errors:
pptx.on('error', function(err) {
console.log(err)
})
// Let's add a title slide:
let slide = pptx.makeTitleSlide('Officegen', 'Example to a PowerPoint document')
// Pie chart slide example:
slide = pptx.makeNewSlide()
slide.name = 'Pie Chart slide'
slide.back = 'ffff00'
slide.addChart(
{
title: 'My production',
renderType: 'pie',
data:
[
{
name: 'Oil',
labels: ['Czech Republic', 'Ireland', 'Germany', 'Australia', 'Austria', 'UK', 'Belgium'],
values: [301, 201, 165, 139, 128, 99, 60],
colors: ['ff0000', '00ff00', '0000ff', 'ffff00', 'ff00ff', '00ffff', '000000']
}
]
}
)
// Let's generate the PowerPoint document into a file:
let out = fs.createWriteStream('example.pptx')
out.on('error', function(err) {
console.log(err)
})
// Async call to generate the output file:
pptx.generate(out)
为了集成这些功能,我在Angular项目中创建了一个包含先前代码的服务:
generateReport.service.ts
import { Injectable } from "@angular/core";
const async = require("async");
const officegen = require("officegen");
const fs = require("fs");
const path = require("path");
@Injectable()
export class generateReport {
constructor() {}
/**
*
*
//var outDir = path.join(__dirname, '../tmp/')
// var themeXml = fs.readFileSync(path.resolve(__dirname, 'themes/testTheme.xml'), 'utf8')
createDoc() {
var docx = officegen({
type: "docx",
orientation: "portrait",
pageMargins: {
top: 1000,
left: 1000,
bottom: 1000,
right: 1000
}
// The theme support is NOT working yet...
// themeXml: themeXml
});
// Remove this comment in case of debugging Officegen:
// officegen.setVerboseMode ( true )
docx.on("error", function(err) {
console.log(err);
});
pObj = docx.createP({
align: "center"
});
pObj.addText(" Risk Analysis Report", {
font_face: "Arial",
font_size: 40
});
//pObj.addLineBreak()
pObj = docx.createP({
backline: "E0E0E0"
});
pObj.addText("Author: Sami Habboubi", {
bold: true
});
pObj = docx.createP({
backline: "E0E0E0"
});
pObj.addText("Date: 11/06/2019");
pObj = docx.createP({
align: "center"
});
pObj.addText("Business Process General Information", {
border: "dotted",
borderSize: 12,
borderColor: "88CCFF",
bold: true
});
var table = [
[
{
opts: {
cellColWidth: 4261,
b: true,
sz: "10",
shd: {
fill: "7F7F7F",
themeFill: "Arial",
themeFillTint: "20"
},
fontFamily: "Arial"
}
},
{
opts: {
b: true,
align: "left",
shd: {
fill: "92CDDC",
themeFill: "text1",
themeFillTint: "80"
},
fontFamily: "Avenir Book"
}
}
],
[
"1. What are your main business objectives? ",
"All grown-ups were once children"
],
[
"2. In which sites(Countries) your team is located?",
"there is no harm in putting off a piece of work until another day."
],
[
"3. What are your key business processes? How are they ranked in terms of criticality?",
"4. But when it is a matter of baobabs, that always means a catastrophe."
],
[
"5. What are your main interactions with other Business Lines?",
"watch out for the baobabs!"
]
];
var tableStyle = {
tableColWidth: 4261,
tableSize: 24,
tableColor: "ada",
tableAlign: "left",
tableFontFamily: "Arial"
};
pObj = docx.createTable(table, tableStyle);
docx.putPageBreak();
var pObj = docx.createP();
pObj.addText("Simple");
pObj.addText(" with color", {
color: "000088"
});
pObj.addText(" and back color.", {
color: "00ffff",
back: "000088"
});
pObj = docx.createP();
pObj.addText("Since ");
pObj.addText("officegen 0.2.12", {
back: "00ffff",
shdType: "pct12",
shdColor: "ff0000"
}); // Use pattern in the background.
pObj.addText(" you can do ");
pObj.addText("more cool ", {
highlight: true
}); // Highlight!
pObj.addText("stuff!", {
highlight: "darkGreen"
}); // Different highlight color.
pObj = docx.createP();
pObj.addText("Even add ");
pObj.addText("external link", {
link: "https://github.com"
});
pObj.addText("!");
pObj = docx.createP();
pObj.addText("Bold + underline", {
bold: true,
underline: true
});
pObj = docx.createP({
align: "center"
});
pObj.addText("Center this text", {
border: "dotted",
borderSize: 12,
borderColor: "88CCFF"
});
pObj = docx.createP();
pObj.options.align = "right";
pObj.addText("Align this text to the right.");
pObj = docx.createP();
pObj.addText("Those two lines are in the same paragraph,");
pObj.addLineBreak();
pObj.addText("but they are separated by a line break.");
docx.putPageBreak();
pObj = docx.createP();
pObj.addText("Fonts face only.", {
font_face: "Arial"
});
pObj.addText(" Fonts face and size.", {
font_face: "Arial",
font_size: 40
});
docx.putPageBreak();
pObj = docx.createP();
//pObj.addImage(path.resolve(__dirname, 'images_for_examples/image3.png'))
docx.putPageBreak();
pObj = docx.createP();
//pObj.addImage(path.resolve(__dirname, 'images_for_examples/image1.png'))
pObj = docx.createP();
//pObj.addImage(path.resolve(__dirname, 'images_for_examples/sword_001.png'))
//pObj.addImage(path.resolve(__dirname, 'images_for_examples/sword_002.png'))
//pObj.addImage(path.resolve(__dirname, 'images_for_examples/sword_003.png'))
//pObj.addText('... some text here ...', { font_face: 'Arial' })
//pObj.addImage(path.resolve(__dirname, 'images_for_examples/sword_004.png'))
pObj = docx.createP();
//pObj.addImage(path.resolve(__dirname, 'images_for_examples/image1.png'))
docx.putPageBreak();
pObj = docx.createListOfNumbers();
pObj.addText("Option 1");
pObj = docx.createListOfNumbers();
pObj.addText("Option 2");
pObj.addHorizontalLine();
pObj = docx.createP({
backline: "E0E0E0"
});
pObj.addText("Backline text1");
pObj.addText(" text2");
pObj = docx.createP();
pObj.addText("Strikethrough text", {
strikethrough: true
});
pObj.addText("superscript", {
superscript: true
});
pObj.addText("subscript", {
subscript: true
});
var out = fs.createWriteStream(path.join("Risk Analysis Report.docx"));
out.on("error", function(err) {
console.log(err);
});
async.parallel(
[
function(done) {
out.on("close", function() {
console.log("Finish to create a DOCX file.");
done(null);
});
docx.generate(out);
}
],
function(err) {
if (err) {
console.log("error: " + err);
} // Endif.
}
);
}
*/
}
我打算在app.Componenet.ts中调用该服务,并以与按钮绑定的方法运行它。
但是,前四行“ require”行会在Angular项目中导致这些错误(当代码在单独的项目中运行时,代码可以正常工作):
src / app / services / generateReport.service.ts(2,15)中的ERROR:错误 TS2580:找不到名称“ require”。您需要安装类型吗 节点的定义?尝试
npm i @types/node
,然后将node
添加到 tsconfig中的类型字段。 src / app / services / generateReport.service.ts(3,19):错误TS2580:无法 找到名称“ require”。您是否需要为节点安装类型定义? 尝试npm i @types/node
,然后将node
添加到您的 tsconfig。 src / app / services / generateReport.service.ts(5,12):错误 TS2580:找不到名称“ require”。您需要安装类型吗 节点的定义?尝试npm i @types/node
,然后将node
添加到 tsconfig中的类型字段。 src / app / services / generateReport.service.ts(6,14):错误TS2580:无法 找到名称“ require”。您是否需要为节点安装类型定义? 尝试npm i @types/node
,然后将node
添加到您的 tsconfig。
我尝试过:
npm install --save @ types / node
但是,它没有解决问题。
所以这是我的问题:
1 /为什么我只在Angular项目中收到这些错误,而在单独的项目中却没有收到?
2 /有办法解决吗?
3 /我实现服务并在主要组件中调用其方法的方法是解决此生成报告任务的正确方法吗?
谢谢你们!
答案 0 :(得分:0)
我相信我已经通过这种方式解决了这个问题:
import * as async from "async";
import * as officegen from "officegen";
import * as fs from "fs";
import * as path from "path";