当前位置:首页 > VUE

vue实现导出word格式

2026-02-24 19:35:46VUE

Vue 中实现导出 Word 文档的方法

使用 docx 库生成 Word 文档

安装 docx 库:

npm install docx

创建一个简单的 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 url = URL.createObjectURL(blob);
        const link = document.createElement('a');
        link.href = url;
        link.download = 'document.docx';
        link.click();
        URL.revokeObjectURL(url);
      });
    }
  }
}

使用 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 url = URL.createObjectURL(converted);
      const link = document.createElement('a');
      link.href = url;
      link.download = 'document.docx';
      link.click();
      URL.revokeObjectURL(url);
    }
  }
}

使用 FileSaver.js 保存文件

安装 FileSaver:

vue实现导出word格式

npm install file-saver

结合 docx 使用 FileSaver:

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

export default {
  methods: {
    exportWithFileSaver() {
      const doc = new Document({
        sections: [{
          children: [
            new Paragraph("Hello World")
          ]
        }]
      });

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

使用模板方法

创建 Word 模板文件 (.dotx) 并使用 docxtemplater:

npm install docxtemplater pizzip

实现代码:

import Docxtemplater from 'docxtemplater';
import PizZip from 'pizzip';
import { saveAs } from 'file-saver';

export default {
  methods: {
    exportWithTemplate() {
      // 加载模板文件
      fetch('/template.docx')
        .then(response => response.arrayBuffer())
        .then(buffer => {
          const zip = new PizZip(buffer);
          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');
        });
    }
  }
}

注意事项

确保在浏览器环境中使用这些方法,部分库可能不支持 SSR 对于复杂文档,考虑使用专业文档生成库如 docx 或模板引擎 处理大文件时要注意性能问题,可能需要分块处理 跨浏览器测试导出功能,不同浏览器可能有不同的行为限制

标签: 格式vue
分享给朋友:

相关文章

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue实现dag

vue实现dag

Vue实现DAG(有向无环图) 在Vue中实现DAG(Directed Acyclic Graph,有向无环图)通常涉及数据结构的建模、可视化渲染以及交互逻辑处理。以下是关键实现步骤和示例代码: 数…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…