[Vue]Vue3.0纯前端导出Word文档 |
您所在的位置:网站首页 › 如何把网页保存为word文档 › [Vue]Vue3.0纯前端导出Word文档 |
画画的baby~ npm i docxtemplater pizzip file-saver --save 创建需要的模板docx文档; 使用vue2.0是可以放在static目录下,使用vue3.0时可放在public目录下(注意: 模板文档最好存放在此位置,不然可能会报路径错误等问题) 注意: 模板中如果需要使用到遍历时,使用 {#数组名称}{/数组名称} 包裹即可 在需要的模块中引入使用 导出 import 'docxtemplater/build/docxtemplater.js' import 'pizzip/dist/pizzip.js' import 'pizzip/dist/pizzip-utils.js' import 'file-saver' export default { data() { return { wordData: { name: '导出word', nameList: [{ name: "张三", age: 16, hobby: ['吃饭', '睡觉', '打豆豆'] }, { name: "李四", age: 19, hobby: ['抽烟', '喝酒', '打麻将'] }, ] }, } }, methods: { // 导出word loadFile(url, callback) { PizZipUtils.getBinaryContent(url, callback); }, generate() { var that = this; this.loadFile("/word.docx", function (error, content) { //url模板存放的位置 if (error) { throw error }; var zip = new PizZip(content); var doc = new window.docxtemplater().loadZip(zip) doc.setData({ ...that.wordData //导入到模板文档的数据对象 }); try { // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...) doc.render() } catch (error) { var e = { message: error.message, name: error.name, stack: error.stack, properties: error.properties, } console.log(JSON.stringify({ error: e })); // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object). throw error; } var out = doc.getZip().generate({ type: "blob", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", }) //Output the document using Data-URI saveAs(out, "output.docx") }) }, } }好啦,这样就完成了啦啦啦~ 可能出现的问题报错: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |