table表格导出为excel文件并设置样式

server/2024/9/23 22:26:06/

excel_0">table表格导出为excel文件并设置样式

安装xlsx、xlsx-style-medalsoft 的 npm 包:

npm i xlsx xlsx-style-medalsoft

设置全局:

Vue.prototype.$XLSX = XLSX; // 设置全局
Vue.prototype.$XLSXStyle = XLSXStyle; // 设置全局

具体代码实现:

// 导出excel文件
exportFile() {// 导出名称let excelName = "file" + dateFormat(new Date(), "YYYY-MM-DD HH-mm-ss") + ".xlsx";const xlsxParam = { raw: true }; // 转化成Excel使用原始格式const table_book = this.$XLSX.utils.table_to_book(document.querySelector("#table"),xlsxParam);// console.log(table_book)let wbs = table_book.Sheets.Sheet1;// console.log(wbs)let arrA = Object.keys(wbs);arrA.forEach((item, index) => {if (wbs[item].v == "") {delete wbs[item];}});// 设置列宽this.column.forEach((item, i) => {wbs["!cols"][i] = { wch: 16 };});// 设置行高this.allTable.forEach((row, rowIndex) => {wbs["!rows"][rowIndex + 1] = { hpt: 24 };});wbs["!rows"][0] = { hpt: 24 };// 循环遍历每一个表格,设置样式for (const key in wbs) {if (key.indexOf("!") === -1) {wbs[key].s = {font: {sz: 11, // 字体大小bold: false, // 加粗name: "宋体", // 字体color: {rgb: "000000", // 十六进制,不带#},},alignment: {// 文字居中horizontal: "center",vertical: "center",wrapText: false, // 文本自动换行},border: {// 设置边框top: { style: "thin" },bottom: { style: "thin" },left: { style: "thin" },right: { style: "thin" },},};}// 设置表头样式if (key == "A1" || key == "B1" || key == "C1" || key == "D1" || key == "E1" ) {wbs[key].s.fill = {bgColor: { indexed: 64 },fgColor: { rgb: "95B3D7" },};}}let table_write = this.$XLSXStyle.write(table_book, {bookType: "xlsx",type: "buffer",});try {this.$FileSaver.saveAs(new Blob([table_write], { type: "application/octet-stream" }),excelName);} catch (e) {if (typeof console !== "undefined") {// console.log(e, table_write);}}return table_write;
},

http://www.ppmy.cn/server/19265.html

相关文章

洛谷 P3806 [模板] 点分治 1 题解

【模板】点分治 1 题目描述 给定一棵有 n n n 个点的树,询问树上距离为 k k k 的点对是否存在。 输入格式 第一行两个数 n , m n,m n,m。 第 2 2 2 到第 n n n 行,每行三个整数 u , v , w u, v, w u,v,w,代表树上存在一条连接 u …

大模型咨询培训老师叶梓:利用知识图谱和Llama-Index增强大模型应用

大模型(LLMs)在自然语言处理领域取得了显著成就,但它们有时会产生不准确或不一致的信息,这种现象被称为“幻觉”。为了提高LLMs的准确性和可靠性,可以借助外部知识源,如知识图谱。那么我们如何通过Llama-In…

【设计模式】11、flyweight 享元模式

文章目录 十一、flyweight11.1 pool 连接池11.1.1 pool_test.go11.1.2 pool.go11.1.3 conn.go 11.2 chess_board11.2.1 chess_test.go11.2.2 chess.go 十一、flyweight https://refactoringguru.cn/design-patterns/flyweight 大量重复的对象, 如果很消耗资源, 没必要每次都初…

什么是redis服务+redis服务数据类型有哪些??

一、背景: 在运维工作会一定会接触到数据库服务,例如oracle数据库、mysql数据库、redis数据库等,这里要介绍的就是redis数据库。 二、什么是redis?? Redis,英文全称是Remote Dictionary Server(…

Day 20 Linux的WEB服务——apache

WEB服务简介 目前主流的web服务器软件 Linux:apache , nginx Windows-server:IIS 服务器安装nginx或apache后,叫做web服务器(又称WWW服务器) web服务器软件属于C/S框架模型 web服务器是一种被动程序只…

Hybrid Homomorphic Encryption:SE + HE

参考文献: [NLV11] Naehrig M, Lauter K, Vaikuntanathan V. Can homomorphic encryption be practical?[C]//Proceedings of the 3rd ACM workshop on Cloud computing security workshop. 2011: 113-124.[MJS16] Maux P, Journault A, Standaert F X, et al. To…

Android NDK开发 CMAKE 相关总结

预设变量含义介绍 工程结构组织: 代码目录 ├── CMakeLists.txt ├── a │ ├── CMakeLists.txt │ └── a.cpp └── b├── CMakeLists.txt├── b.cpp└── b.h路径相关: CMAKE_SOURCE_DIR:最顶层 CMakceLists.txt 所在…

AutoGPT-Forge使用教程,自行构建agent智能体

本博客给出AutoGPT-forge四个教程的翻译与理解,使用GPT4翻译, 参考官方教程https://aiedge.medium.com/autogpt-forge-a-comprehensive-guide-to-your-first-steps-a1dfdf46e3b4 使用AutoGPT Github代码日期2024/4/22; 博客开始编辑日期20…