当前位置:首页 > VUE

vue实现word下载功能

2026-02-23 14:45:13VUE

使用 docx 库生成并下载 Word 文件

安装 docx 库和 file-saver 库:

npm install docx file-saver --save

在 Vue 组件中引入所需库:

import { Document, Paragraph, Packer, TextRun } from 'docx';
import { saveAs } from 'file-saver';

创建 Word 文档内容并触发下载:

export default {
  methods: {
    generateWordDocument() {
      const doc = new Document({
        sections: [{
          properties: {},
          children: [
            new Paragraph({
              children: [
                new TextRun("Hello World"),
                new TextRun({
                  text: "Foo Bar",
                  bold: true,
                }),
              ],
            }),
          ],
        }]
      });

      Packer.toBlob(doc).then(blob => {
        saveAs(blob, "example.docx");
      });
    }
  }
}

使用 html-docx-js 转换 HTML 为 Word

安装 html-docx-jsfile-saver

vue实现word下载功能

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

在组件中使用:

import htmlDocx from 'html-docx-js';
import { saveAs } from 'file-saver';

export default {
  methods: {
    exportAsWord() {
      const html = '<h1>Hello World</h1><p>This is a test document</p>';
      const converted = htmlDocx.asBlob(html);
      saveAs(converted, 'document.docx');
    }
  }
}

使用后端 API 下载 Word 文件

前端发起请求:

export default {
  methods: {
    downloadWordFile() {
      axios.get('/api/download-word', {
        responseType: 'blob'
      }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'file.docx');
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    }
  }
}

使用模板填充数据生成 Word

安装 docxtemplater

vue实现word下载功能

npm install docxtemplater pizzip file-saver --save

实现代码:

import Docxtemplater from 'docxtemplater';
import PizZip from 'pizzip';
import { saveAs } from 'file-saver';

export default {
  methods: {
    generateFromTemplate() {
      // 加载模板文件
      axios.get('/template.docx', { responseType: 'arraybuffer' })
        .then(response => {
          const zip = new PizZip(response.data);
          const doc = new Docxtemplater().loadZip(zip);

          // 设置模板变量
          doc.setData({
            name: "John",
            age: 30
          });

          try {
            doc.render();
          } catch (error) {
            console.error(error);
          }

          const out = doc.getZip().generate({ type: 'blob' });
          saveAs(out, 'output.docx');
        });
    }
  }
}

注意事项

确保在浏览器环境中使用这些方法时处理可能的兼容性问题。

对于复杂文档生成需求,考虑使用后端服务处理文档生成,前端仅负责下载。

大文件生成时需要考虑性能优化和内存管理。

标签: 功能vue
分享给朋友:

相关文章

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue实现表单

vue实现表单

Vue 表单实现方法 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定。适用于 input、textarea、select 等元素。 <template> <…

vue实现webshell

vue实现webshell

Vue 实现 WebShell 使用 Vue 实现 WebShell 需要结合前端与后端的交互,通常通过 WebSocket 或 HTTP 协议与服务器通信。以下是一个基于 Vue 和 WebSock…

vue实现treetable

vue实现treetable

Vue实现TreeTable的方法 使用第三方组件库(如Element UI) Element UI的el-table组件支持树形表格展示,通过设置row-key和tree-props属性即可实现。…