安装四个依赖
npm install docxtemplater jszip
-utils jszip FileSaver
--S
创建一个world模板 目录放在 public里面
模板样式
引入
import docxtemplater
from 'docxtemplater';
import JSZipUtils
from 'jszip-utils';
import { saveAs
} from 'file-saver';
import JSZip
from 'jszip';
js代码
<script
>
export default {
name
: "home",
data() {
return {
form
: {
custName
: "杰斯",
phoneNumber
: "138xxxxxxxx",
projectRequirement
: "为了更美好的明天而战",
totalPrice
: 140,
remark
: "QEWARAEQAAAAAAAAA",
checkReason
: '同意'
},
tableData
: []
};
},
created() {
this.tableData
= [
{
number
: 1,
name
: "设备1",
num
: 1,
salePrice
: 10,
saleTotal
: 10,
remark
: "啦啦啦"
},
{
number
: 2,
name
: "设备2",
num
: 2,
salePrice
: 20,
saleTotal
: 40,
remark
: "啦啦啦"
},
{
number
: 3,
name
: "设备3",
num
: 3,
salePrice
: 30,
saleTotal
: 90,
remark
: "啦啦啦"
}
];
},
methods
: {
exportWord
: function() {
let _this
= this;
JSZipUtils
.getBinaryContent("input.docx", function(error
, content
) {
if (error
) {
throw error
;
}
let zip
= new JSZip(content
);
let doc
= new docxtemplater().loadZip(zip
);
doc
.setData({
..._this
.form
,
table
: _this
.tableData
});
try {
doc
.render();
} catch (error) {
let e
= {
message
: error
.message
,
name
: error
.name
,
stack
: error
.stack
,
properties
: error
.properties
};
console
.log(JSON.stringify({ error
: e
}));
throw error
;
}
let out
= doc
.getZip().generate({
type
: "blob",
mimeType
:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
saveAs(out
, "报价单.docx");
});
}
}
};
</script
>
转载自用(如有侵权请联系) 转自于https://www.jianshu.com/p/b3622d6f8d98
转载请注明原文地址:https://ipadbbs.8miu.com/read-29975.html