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

vue怎么实现导出word

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

vue怎么实现导出word

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:

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实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

vue底层实现

vue底层实现

Vue.js 的底层实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现细节: 响应式系统 Vue 2…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…

vue实现表单

vue实现表单

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