当前位置: 首页 > news >正文

vue-将组件内容导出为Word文档-docx

1. 安装依赖

首先,我们需要安装docx库,以便在前端生成Word文档。可以通过以下命令进行安装:

npm install docx

2. 实现导出功能

2.1 初始化文档

使用docx库创建一个新的文档实例,并定义文档的结构和内容。我们使用Document、Paragraph和TextRun等类来构建文档。

const doc = new Document({
  sections: [{
    properties: {},
    children: [
      new Paragraph({
        children: [new TextRun({ text: `id:${proposalDetail.value.pid}【这里填写自己要的信息哈】`, size: 24 })],
        spacing: { after: 200 },
        alignment: 'right', // 右对齐
      }),
      new Paragraph({
        children: [
          new TextRun({
            text: "详情信息",
            bold: true,
            size: 32,
          }),
        ],
        spacing: { after: 400 },
      }),
      // 其他信息...
    ],
  }],
});

     每个信息都使用Paragraph和TextRun进行格式化。总之就是要添加什么,就new 一个 Paragraph或者是TextPath(看代码就知道啥意思了)

2.2 生成并下载文档

使用Packer.toBlob方法将文档转换为Blob对象,并使用file-saver库的saveAs方法下载文档。

const blob = await Packer.toBlob(doc);
saveAs(blob, `${proposalDetail.value.pid || '详情'}.docx`);
2.3完整代码(以我的实现为例)
// 下载提案内容为Word文档
const download = async () => {
  try {
    isDownloading.value = true

    // 创建文档
    const doc = new Document({
      sections: [{
        properties: {},
        children: [
          new Paragraph({
            children: [new TextRun({ text: `提案号:${proposalDetail.value.pid}`, size: 24 })],
            spacing: { after: 200 },
            alignment: 'right',
          }),
          new Paragraph({
            children: [
              new TextRun({
                text: "提案详情",
                bold: true,
                size: 32,
              }),
            ],
            spacing: { after: 400 },
          }),
          // 基本信息
          new Paragraph({
            children: [new TextRun({ text: `案由:${proposalDetail.value.caseReason}`, size: 24 })],
            spacing: { after: 200 },
          }),
          new Paragraph({
            children: [new TextRun({ text: `提案人:${proposalDetail.value.proposer}`, size: 24 })],
            spacing: { after: 200 },
          }),
          new Paragraph({
            children: [new TextRun({ text: `联名人:${proposalDetail.value.signers || '无'}`, size: 24 })],
            spacing: { after: 200 },
          }),
          new Paragraph({
            children: [new TextRun({ text: `提案人地区:${proposalDetail.value.proposerRegion}`, size: 24 })],
            spacing: { after: 200 },
          }),
          new Paragraph({
            children: [new TextRun({ text: `提案类型:${proposalDetail.value.proposalType}`, size: 24 })],
            spacing: { after: 200 },
          }),
          new Paragraph({
            children: [new TextRun({ text: `提交时间:${formatDateTime(proposalDetail.value.submitTime)}`, size: 24 })],
            spacing: { after: 200 },
          }),
          new Paragraph({
            children: [new TextRun({ text: `是否愿意公示:${proposalDetail.value.isPublic ? '是' : '否'}`, size: 24 })],
            spacing: { after: 400 },
          }),
          // 提案内容
          new Paragraph({
            children: [
              new TextRun({
                text: "提案内容:",
                bold: true,
                size: 24,
              }),
            ],
            spacing: { after: 200 },
          }),
          new Paragraph({
            children: [new TextRun({ text: proposalDetail.value.proposalContent, size: 24 })],
            spacing: { after: 400 },
          }),
          // 处理结果
          new Paragraph({
            children: [
              new TextRun({
                text: "处理结果:",
                bold: true,
                size: 24,
              }),
            ],
            spacing: { after: 200 },
          }),
          new Paragraph({
            children: [new TextRun({ text: proposalDetail.value.handingResult || '暂无处理结果', size: 24 })],
            spacing: { after: 400 },
          }),

          // 添加处理状态部分
          new Paragraph({
            children: [
              new TextRun({
                text: "处理状态:",
                bold: true,
                size: 24,
              }),
            ],
            spacing: { after: 200 },
          }),

          // 添加所有状态阶段
          ...getStatusInfo.value
            .filter(stage => !stage.isHidden)
            .map(stage =>
              new Paragraph({
                children: [
                  new TextRun({
                    text: `${stage.label}—— `,
                    size: 24
                  }),
                  new TextRun({
                    text: getStatusText(stage),
                    size: 24,
                    color: stage.isRejected ? 'FF0000' : // 红色
                      stage.isProcessing ? '409EFF' : // 蓝色
                        !stage.time ? '909399' : '333333' // 灰色或黑色
                  })
                ],
                spacing: { after: 200 },
              })
            )
        ],
      }],
    })

    // 生成文档
    const blob = await Packer.toBlob(doc)
    saveAs(blob, `提案${proposalDetail.value.pid || '详情'}.docx`)
    ElMessage.success('提案内容已保存为Word文档')

  } catch (error) {
    console.error('生成Word文档失败:', error)
    ElMessage.error('生成Word文档失败,请重试')
  } finally {
    isDownloading.value = false
  }
}

3. 用户界面

在用户界面中,我们使用Element Plus的按钮组件来触发导出功能。按钮的文本会根据导出状态进行更新,提供良好的用户体验。

<el-button type="danger" @click="download" :loading="isDownloading" class="action-button">
  {{ isDownloading ? '正在生成...' : '详情导出Word' }}
</el-button>

4. 实现效果

相关文章:

  • StarRocks数据导入
  • 漏洞挖掘 --- Ollama未授权访问
  • 【区块链安全 | 第三篇】主流公链以太坊运行机制
  • PAT甲级(Advanced Level) Practice 1028 List Sorting
  • 【附代码】【MILP建模】3D装箱问题(3D-Bin Packing Problem)
  • 冠珠瓷砖×郭培:当东方美学邂逅匠心工艺,高定精神如何重塑品质生活?
  • 企业在人工智能创新与安全之间走钢丝
  • TDengine 中的系统信息统计
  • oracle查询归档日志使用量
  • 阳台光伏新守护者:电流传感器助力安全发电
  • 【开源宝藏】用 JavaScript 手写一个丝滑的打字机动画效果
  • 软件确认测试注意事项和工具分享,确认测试软件测评中心有哪些?
  • 【报错】 /root/anaconda3/conda.exe: cannot execute binary file: Exec format error
  • 可变形交互注意力模块(DIA-Module)及代码详解
  • 基础场景-------------------(5)重载和重写的区别
  • Squidex:一个基于.Net功能强大的CMS开源项目
  • 2025年渗透测试面试题总结-某深信服-深蓝攻防实验室(题目+回答)
  • Linux 练习一 NFS和DNS
  • K-均值聚类算法:数据海洋中的分类灯塔
  • springboot的跨域是什么?遇到跨域问题如何解决?
  • 门户网站建设和推广/哪里可以买链接网站
  • 网站备案 法人变更/天气预报最新天气预报
  • 在哪些网站做收录比较快/今天发生的重大新闻5条
  • 香港免费永久网站/创建网站需要多少资金
  • 网站吗/网站推广的6个方法是什么
  • 建站软件怎么免费升级/做网络优化哪家公司比较好