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

将后端数据转换为docx文件

使用docx

npm install docx 

按照注释处理数据并转换为对应的bolb数据流 

<template><Button type="primary" @click="handleDocxCreate">{{buttonTitle || "报告生成"}}</Button>
</template><script>
import {Document,Paragraph,TextRun,Packer,AlignmentType,Table,TableCell,TableRow,WidthType,BorderStyle,
} from "docx";
export default {props: {buttonTitle: {// 报告生成type: String,},docxData: {type: Array,required: true,//   [//     {//       isTable: Boolean, // 是否为表格,默认为false,若table为true,则data必填,其余忽略//       isAlignment: String, // 字段位置,默认为LEFT,可以为BOTH,CENTER,END,LEFT,RIGHT,START...//       isIndent: Boolean, // 是否首行缩进两个字符,默认为false//       text: String, // 内容//       size: Number, // 字体大小,默认为22//       font: String, // 字体类型,默认为仿宋//       isBold: Boolean, // 是否加粗,默认为false//       data: [ // Array//         [//           {//             text: String, // 表格单元格内容//             width: Number, // 单元格宽度,默认为每个单元格平均分配宽度,如果不设置则会自动计算//             columnSpan: Number, // 合并单元格,默认为1//             isBolb: Boolean, // 是否加粗,默认为false//             size: Number, // 字体大小,默认为22//             font: String, // 字体类型,默认为仿宋//           }//         ]//       ], // 表格数据,如果isTable为true,则必填////     }//   ]},},data() {return {};},methods: {/*** 文件blob流生成*/async handleDocxCreate() {// 创建 Word 文档const doc = new Document({sections: [{properties: {},children: this.docxData.map((item) => {// 如果是表格,则处理表格数据if (item.isTable) {return this.handleTabel(item.data);} else {// 否则处理普通文本段落return new Paragraph({alignment: AlignmentType[item.isAlignment || "LEFT"],indent: {firstLine: item.isIndent ? 500 : 0, // 直接设置缩进值(单位为twips,1厘米=567twips,约1.27厘米缩进)},children: [new TextRun({text: item.text || '',bold: item.isBold || false,size: item.size || 22,font: item.font || "仿宋",}),],});}}),},],});// 生成 Blob 对象const blob = await Packer.toBlob(doc);},/*** 表格* @param {*} data*/handleTabel(data) {const table = new Table({width: {size: 100,type: WidthType.PERCENTAGE,},rows: data.map((row, rowIndex) =>new TableRow({children: row.map((cell, cellIndex) => {// 合并单元格return new TableCell({width: {size: cell.width || (cell.length / row.length) * 100,type: WidthType.PERCENTAGE,},columnSpan: cell.columnSpan || 1,children: [new Paragraph({alignment: AlignmentType.CENTER,children: [new TextRun({text: cell.text || '',bold: cell.isBolb || false,size: cell.size || 22,font: cell.font || "仿宋",}),],}),],// 可选:设置单元格样式shading: { fill: "F3F3F3" }, // 背景色borders: {top: {style: BorderStyle.SINGLE,size: 4,color: "000000",},bottom: {style: BorderStyle.SINGLE,size: 4,color: "000000",},left: {style: BorderStyle.SINGLE,size: 4,color: "000000",},right: {style: BorderStyle.SINGLE,size: 4,color: "000000",},},});}),})),// 设置表格对齐为左对齐alignment: AlignmentType.LEFT,borders: {top: { style: BorderStyle.SINGLE, size: 4, color: "000000" },bottom: { style: BorderStyle.SINGLE, size: 4, color: "000000" },left: { style: BorderStyle.SINGLE, size: 4, color: "000000" },right: { style: BorderStyle.SINGLE, size: 4, color: "000000" },},});return table;},},
};
</script>
<style scoped>
</style>

若需要将该文件传给后端,需要转换bolb数据流的类型

      let formData = new FormData();// 转换文件类型const file = new File([blob], `${this.fileName || "报告.docx"}`, {type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",});formData.append("file", file);ajax.post(uploadUrl, formData).then((response) => {console.log("文件上传成功", response);})

文件下载

const downloadLink = document.createElement("a");
const blobUrl = URL.createObjectURL(blobData);downloadLink.href = blobUrl;
downloadLink.download = "name.docx";
downloadLink.click();URL.revokeObjectURL(blobUrl);

 

http://www.xdnf.cn/news/13995.html

相关文章:

  • 京东零售基于Flink的推荐系统智能数据体系 |Flink Forward Asia 峰会实录分享
  • 论文阅读:arxiv 2025 How Likely Do LLMs with CoT Mimic Human Reasoning?
  • 自动化模型管理:MediaPipe Android SDK 中的模型文件下载与加载机制
  • Flutter:步骤条组件
  • Wi-Fi 6 在 2.4GHz 频段的速率与优化分析
  • Unit 3 训练一个Q-Learning智能体 Frozen-Lake-v1
  • 基于springboot视频及游戏管理系统+源码+文档+应用视频
  • RTP MOS计算:语音质量的数字评估
  • STM32HAL库发送字符串,将uint8_t数据转为字符串发送,sprintf函数的使用方法
  • 声学成像仪在电力行业的应用品牌推荐
  • Java从入门到精通 - 面向对象高级(一)
  • vllm eagle支持分析
  • 燃气从业人员资格证书:开启职业大门的 “金钥匙”
  • Ntfs!NtfsInitializeRestartTable函数分析
  • 资金分析怎么做?如何预防短期现金流风险?
  • 刀客doc:WPP走下神坛
  • 中国AI Top30 访问量排行榜 - 2025年05月
  • 外部记忆的组织艺术:集合、树、栈与队列的深度解析
  • 燃气从业人员资格证书:职业发展的 “助推器”
  • 灌区信息化智能一体化闸门系统解决方案
  • 学习STC51单片机36(芯片为STC89C52RCRC)智能小车3(PWM差速小车)
  • 【软件安装那些事 4】CAD字体 SHX格式字库 免费 下载 及 使用方法
  • python中的分支结构:单分支、多分支,switch语句
  • JeecgBoot Pro-Online表单开发
  • 【经验篇】自签名TLS证书生成
  • 博客园突发大规模DDoS攻击 - 深度解析云安全防御新范式
  • P10987 [蓝桥杯 2023 国 Python A] 火车运输
  • 第一章 数字电路概述
  • 记一次错误 深拷贝 key值全部小写
  • 三次握手建立连接,四次挥手释放连接——TCP协议的核心机制