当前位置:首页 > VUE

vue实现word文档导出

2026-02-22 12:36:59VUE

使用docx.js库生成Word文档

安装docx.js库:

npm install docx

创建一个简单的Word文档并导出:

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

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 = "example.docx";
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
});

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

安装html-docx-js库:

npm install html-docx-js

将HTML内容转换为Word文档:

vue实现word文档导出

import htmlDocx from "html-docx-js";

const html = `
  <h1>标题</h1>
  <p>这是一个段落</p>
  <ul>
    <li>项目1</li>
    <li>项目2</li>
  </ul>
`;

const converted = htmlDocx.asBlob(html);
const url = URL.createObjectURL(converted);
const link = document.createElement("a");
link.href = url;
link.download = "document.docx";
link.click();

使用file-saver保存文件

安装file-saver库简化下载过程:

npm install file-saver

结合docx.js使用:

vue实现word文档导出

import { saveAs } from "file-saver";
import { Packer } from "docx";

// ...创建doc对象后
Packer.toBlob(doc).then(blob => {
  saveAs(blob, "document.docx");
});

后端生成Word文档

使用Node.js后端服务生成Word:

const express = require("express");
const { Document, Paragraph, Packer } = require("docx");

const app = express();

app.get("/export-word", (req, res) => {
  const doc = new Document({
    sections: [{
      children: [
        new Paragraph("后端生成的Word文档")
      ]
    }]
  });

  Packer.toBuffer(doc).then(buffer => {
    res.setHeader(
      "Content-Type",
      "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    );
    res.setHeader("Content-Disposition", "attachment; filename=export.docx");
    res.send(buffer);
  });
});

app.listen(3000);

处理复杂格式和样式

创建带样式的复杂文档:

const doc = new Document({
  sections: [{
    properties: {
      page: {
        margin: {
          top: 1000,
          right: 1000,
          bottom: 1000,
          left: 1000,
        }
      }
    },
    children: [
      new Paragraph({
        heading: "Heading1",
        children: [
          new TextRun({
            text: "带样式的标题",
            bold: true,
            size: 32,
            font: "Arial"
          })
        ]
      }),
      new Paragraph({
        children: [
          new TextRun({
            text: "红色文本",
            color: "FF0000",
            italics: true
          })
        ]
      })
    ]
  }]
});

使用模板替换内容

创建模板替换方案:

function generateDocument(data) {
  return new Document({
    sections: [{
      children: [
        new Paragraph(`姓名: ${data.name}`),
        new Paragraph(`年龄: ${data.age}`),
        new Paragraph({
          text: `地址: ${data.address}`,
          alignment: "left"
        })
      ]
    }]
  });
}

// 使用示例
const userData = {
  name: "张三",
  age: 30,
  address: "北京市朝阳区"
};

const doc = generateDocument(userData);
// 然后使用Packer导出...

这些方法提供了从简单到复杂的Vue中导出Word文档的解决方案,可以根据项目需求选择合适的方式。对于简单需求,纯前端方案足够;复杂需求可能需要结合后端服务。

标签: 文档vue
分享给朋友:

相关文章

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Route…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue单选实现

vue单选实现

Vue 单选实现 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选效果。当用户选择不同的选…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue监听实现

vue监听实现

Vue 监听实现方法 在 Vue 中可以通过多种方式实现监听数据变化或事件触发,以下是常见的几种方法: 使用 watch 选项 watch 是 Vue 提供的核心功能之一,用于观察和响应 Vue 实…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…