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

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生…

django vue实现

django vue实现

Django与Vue.js整合实现方案 Django作为后端框架与Vue.js作为前端框架的整合,可以通过以下方式实现: 分离式开发 前后端完全分离,Django仅提供API接口,Vue.js通过a…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动…

vue 实现树状

vue 实现树状

Vue 实现树状结构的方法 递归组件实现树状结构 递归组件是 Vue 中实现树状结构的常见方法。通过组件自身调用自身,可以轻松构建多层级树状结构。 <template> <u…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装…