当前位置:首页 > 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.js实现轮播

vue.js实现轮播

vue.js实现轮播的方法 使用第三方库(如Swiper) 安装Swiper库: npm install swiper 在Vue组件中引入Swiper: <template>…

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload()…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="cloc…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…