docx插件 动态插入文本 导出word文件
·
docx插件 动态创建文本
安装依赖
npm install file-saver
npm install docx
引入依赖
import { saveAs } from 'file-saver';
import { Document, Packer, Paragraph,HeadingLevel ,AlignmentType} from "docx";
function getAllBlob(){
// 创建一个Word文档
let sections = [{
children: [
new Paragraph({
text: "标题内容",
// style: "Normal",
heading: HeadingLevel.HEADING_1,
alignment: AlignmentType.CENTER
}),
],
}];
const doc = new Document({
title:fileTitle.value,
sections
});
return new Promise((resolve,reject)=>{
// 将文档保存为Blob对象
Packer.toBlob(doc).then((blob) => {
resolve(blob);
});
})
}
let titleBlob;
if(fileTitle.value){
let [blob,err] = await getAllBlob().then(res=>[res,null]).catch(err=>[null,err]);
if(!err){
titleBlob = blob;
}
}
saveAs(titleBlob ,"test.docx");
更多推荐



所有评论(0)