当前位置:首页 > 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

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

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实现word下载功能

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

相关文章

vue实现选择本地文件

vue实现选择本地文件

实现文件选择功能 在Vue中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &…

vue自己实现下拉导航

vue自己实现下拉导航

实现下拉导航的基本思路 在Vue中实现下拉导航,可以通过结合Vue的响应式数据和事件绑定特性来完成。主要思路是利用v-show或v-if控制下拉菜单的显示与隐藏,并通过鼠标事件或点击事件触发状态变化。…

vue实现字母添加排序

vue实现字母添加排序

实现字母添加排序的方法 在Vue中实现字母添加排序功能,可以通过以下步骤完成: 创建Vue实例并定义数据 new Vue({ el: '#app', data: { letters…

vue 实现jqslidedown

vue 实现jqslidedown

在 Vue 中实现类似 jQuery 的 slideDown 效果,可以通过 Vue 的过渡系统或 CSS 动画结合动态样式绑定来实现。以下是几种实现方式: 使用 Vue Transition…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template&…

vue实现计算

vue实现计算

Vue 实现计算的方法 在 Vue 中实现计算功能可以通过多种方式完成,主要包括计算属性(computed)、方法(methods)和侦听器(watch)。以下是具体的实现方法: 计算属性(Com…