vue 生产word_Vue-纯前端导出word文档

本文详细介绍了如何在Vue项目中使用docxtemplater、jszip-utils和file-saver库,结合JSON数据,实现纯前端生成并下载word文档。通过提供模板文件,设置模板变量并替换,最终完成文件导出。
摘要由CSDN通过智能技术生成

在项目中,我们可以借助后端返回文件流实现文件下载。如果前端有数据,也可以借助前端框架进行下载。本文将介绍如何在前端纯js实现word文档导出。

docxtemplater

docxtemplater 使用 JSON 数据格式作为输入,可以处理docx 和 ppt模板。不像一些其它的工具,比如 docx.js, docx4j, python-docx 等,需要自己编写代码来生成文件,docxtemplater只需要用户通过标签的形式编写模板,就可以生成文件。

贴一贴官网给的例子,我们将参考以下例子来实现。

var PizZip = require('pizzip');

var Docxtemplater = require('docxtemplater');

var fs = require('fs');

var path = require('path');

//Load the docx file as a binary

var content = fs

.readFileSync(path.resolve(__dirname, 'input.docx'), 'binary');

var zip = new PizZip(content);

var doc = new Docxtemplater();

doc.loadZip(zip);

//set the templateVariables

doc.setData({

first_name: 'John',

last_name: 'Doe',

phone: '0652455478',

description: 'New Website'

});

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 buf = doc.getZip()

.generate({type: 'nodebuffer'});

// buf is a nodejs buffer, you can either write it to a file or do anything else with it.

fs.writeFileSync(path.resolve(__dirname, 'output.docx'), buf);

jszip-utils

jszip-utils 提供一个getBinaryContent(path, data)接口,path即是文件的路径,支持AJAX get请求,data为读取的文件内容。下面是官网给的例子。

// loading a file and add it in a zip file

JSZipUtils.getBinaryContent("path/to/picture.png", function (err, data) {

if(err) {

throw err; // or handle the error

}

var zip = new JSZip();

zip.file("picture.png", data, {binary:true});

});

file-saver

file-saver是一款适合在客户端生成文件的工具,它提供的接口saveAs(blob, "1.docx")将会使用到。

安装

接下来就是安装以上工具

-- 安装 docxtemplater

cnpm install docxtemplater pizzip --save

-- 安装 jszip-utils

cnpm install jszip-utils --save

-- 安装 jszip

cnpm install jszip --save

-- 安装 FileSaver

cnpm install file-saver --save

引入

在需要用到的组件中引入以上工具

import docxtemplater from 'docxtemplater'

import PizZip from 'pizzip'

import JSZipUtils from 'jszip-utils'

import {saveAs} from 'file-saver'

创建模板文件

我们要先创建一个模板文件,事先定义好格式和内容。docxtemplater 之前介绍到是通过标签的形式来填充数据的,简单的数据我们可以使用{} + 变量名来实现简单的文本替换。

简单的文本替换

如果在模板中,定义

hello {name}

在设置数据时,定义

{name:'John'}

最终生成的文件,如下

hello John

有点像jsp中的变量解析。

循环输出

稍微复杂点的像表格,我们会传递一个数组。那这个表格标签实现起来挺简单的,例子如下:

模板文件,定义如下:

{#products}

{name}, {price} €

{/products}

设置数据时,定义如下:

{

"products": [

{ name :"Windows", price: 100},

{ name :"Mac OSX", price: 200},

{ name :"Ubuntu", price: 0}

]

}

最终实现效果如下:

Windows, 100 €

Mac OSX, 200 €

Ubuntu, 0€

如果数组中的都是字符串,不是对象类型,比如数据结构如下

{

"products": [

"Windows",

"Mac OSX",

"Ubuntu"

]

}

那么,模板文件中应该这样设置

{#products} {.} {/products}

最终的文件内容如下:

Windows Mac OSX Ubuntu

还有一些其它的复杂标签,比输支持条件判断,支持段落等等,笔者就不在这里一一赘述了。详情参考官网文档。笔者的要导出的比较简单,前端页面如下:

4ebfdab5ad35704f6d097430b2604b39.png

前端

模板如下,笔者放在了\static\model.docx路径下:

7e43e28a2de51e241d8ec741124ff5f7.png

模板

使用

我们可以参照 docxtemplater 给出的例子, 来实现文件导出。

读取模板文件内容

装载到zip对象中

设置文件数据

生成文件

保存文件

代码如下:

// 点击导出word

exportWord: function() {

let that = this;

// 读取并获得模板文件的二进制内容

JSZipUtils.getBinaryContent("../../static/model.docx", function(error, content) {

// model.docx是模板。我们在导出的时候,会根据此模板来导出对应的数据

// 抛出异常

if (error) {

throw error;

}

// 创建一个PizZip实例,内容为模板的内容

let zip = new PizZip(content);

// 创建并加载docxtemplater实例对象

let doc = new docxtemplater().loadZip(zip);

// 设置模板变量的值

doc.setData({

table: that.videoParam.data

});

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;

}

// 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)

let out = doc.getZip().generate({

type: "blob",

mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

});

// 将目标文件对象保存为目标类型的文件,并命名

saveAs(out, "视频参数.docx");

});

}

最终下载的效果如下

52ae376e8d3e95530cc183a2e7a94d50.png

image.png

注意

docxtemplater 不支持jszip,会有报错,因此要使用PizZip

注意模板的路径要写正确,不然会报错找不到文件

总结

本文简单的介绍了如何在前端使用已有的工具来实现前端导出word文档,希望对有类似需求的童鞋们有所帮助。

参考文章:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值