关注公众号 jb51net

首页 > 网络编程 > JavaScript > javascript类库 > vue.js > Vue导出word






npm install file-saver html-docx-js --save





* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email:
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2024-02-16

	<div class="djs-box">
		<div class="topBox">
			<div>大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
				<el-button type="primary" size="mini" @click="exportToWord()"> 导出word文档</el-button>
		<div class="dajianshi" id="dajianshi">
			<h3> 这是我要导出的文件标题</h3>
			<p>This is a very small library that is capable of converting HTML documents to DOCX format that is used by
				Microsoft Word 2007 and onward. It manages to perform the conversion in the browser by using a feature
				called 'altchunks'. In a nutshell, it allows embedding content in a different markup language. We are
				using MHT document to ship the embedded content to Word as it allows to handle images. After Word opens
				such file, it converts the external content to Word Processing ML (this is how the markup language of
				DOCX files is called) and replaces the reference.</p>
				Altchunks were not supported by Microsoft Word for Mac 2008 and are not supported by LibreOffice and
				Google Docs.</p>

	import FileSaver from 'file-saver';
	import htmlDocx from 'html-docx-js/dist/html-docx';
	export default {
		data() {
			return {
				message: 'hello world',
				price: 1234.56,
				date: '2022-01-01'

		methods: {
			exportToWord() {
				// 获取要导出的HTML内容
				const content = document.getElementById('dajianshi').innerHTML;
				// 将HTML内容转换为Word文档
				const converted = htmlDocx.asBlob(content);
				// 使用FileSaver保存Word文档
				FileSaver.saveAs(converted, 'example.docx');

<style scoped>
	.djs-box {
		width: 1000px;
		height: 650px;
		margin: 50px auto;
		border: 1px solid deepskyblue;

	.topBox {
		margin: 0 auto 0px;
		padding: 10px 0 20px;
		background: deepskyblue;
		color: #fff;

	.dajianshi {
		width: 93%;
		height: 400px;
		margin: 5px auto 0;
		border: 1px solid #369;
		background-color: cde;
		padding: 20px;

	p {
		font-size: 16px;
		text-align: left;


要生成 DOCX,只需将 HTML 文档(作为字符串)传递给 asBlob 方法以接收包含输出文件的 Blob(或缓冲区)。

var converted = htmlDocx.asBlob(content);
saveAs(converted, ‘test.docx');

asBlob 可以采用其他选项来控制文档的页面设置:

orientation: landscape or portrait (default)

margins: map of margin sizes (expressed in twentieths of point, see WordprocessingML documentation for details):

top: number (default: 1440, i.e. 2.54 cm)

right: number (default: 1440)

bottom: number (default: 1440)

left: number (default: 1440)

header: number (default: 720)

footer: number (default: 720)

gutter: number (default: 0)


please pass a complete, valid HTML (including DOCTYPE, html and body tags). This may be less convenient, but gives you possibility of including CSS rules in style tags.

html-docx-js is distributed as ‘standalone’ Browserify module (UMD). You can require it as html-docx. If no module loader is available, it will register itself as window.htmlDocx. See test/sample.html for details.

API 参考网址