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

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Car…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…