当前位置:首页 > VUE

vue怎么实现导出word

2026-01-21 11:32:29VUE

使用 docx 库生成 Word 文件

安装 docx 库:

npm install docx

在 Vue 组件中引入并创建 Word 文档:

import { Document, Paragraph, Packer, TextRun } from 'docx';

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

      Packer.toBlob(doc).then(blob => {
        const link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = 'example.docx';
        link.click();
        URL.revokeObjectURL(link.href);
      });
    },
  },
};

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

安装 html-docx-js

npm install html-docx-js

将 HTML 内容转换为 Word:

import htmlDocx from 'html-docx-js';

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

      const link = document.createElement('a');
      link.href = URL.createObjectURL(converted);
      link.download = 'document.docx';
      link.click();
      URL.revokeObjectURL(link.href);
    },
  },
};

使用 FileSaver 保存文件

安装 file-saver

npm install file-saver

结合 docx 使用 FileSaver:

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

export default {
  methods: {
    exportWithFileSaver() {
      const doc = new Document({
        sections: [{
          children: [
            new Paragraph({
              text: "Example document",
            }),
          ],
        }],
      });

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

使用模板引擎动态生成内容

结合模板引擎如 Mustache 或 Handlebars 动态生成 Word 内容:

import { Document } from 'docx';
import Mustache from 'mustache';

export default {
  data() {
    return {
      templateData: {
        title: "Report",
        items: ["Item 1", "Item 2", "Item 3"]
      }
    };
  },
  methods: {
    exportTemplatedWord() {
      const template = `
        {{title}}
        {{#items}}
          - {{.}}
        {{/items}}
      `;
      const rendered = Mustache.render(template, this.templateData);

      const doc = new Document({
        sections: [{
          children: [
            new Paragraph({
              text: rendered,
            }),
          ],
        }],
      });

      // 使用 Packer 导出...
    },
  },
};

服务器端生成 Word 文件

通过 API 调用服务器端生成 Word:

vue怎么实现导出word

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

注意事项

  • 样式处理:Word 文档的样式需要通过库的 API 设置,无法直接使用 CSS
  • 复杂格式:对于表格、图片等复杂内容,需要查阅具体库的文档
  • 浏览器兼容性:Blob URL 方式在主流浏览器中都支持
  • 文件大小:大文档建议使用服务器端生成

标签: vueword
分享给朋友:

相关文章

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组件中引入…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue nexttick实现

vue nexttick实现

Vue 中 nextTick 的实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它的核心实现依赖于 JavaScript 的事件循环机制。…