Browse Source

更新合并单元

刘忠健 1 year ago
parent
commit
532e4df36f
6 changed files with 107 additions and 6 deletions
  1. 2 2
      excel/package-lock.json
  2. 1 1
      excel/package.json
  3. 31 1
      excel/readme.md
  4. 72 1
      excel/src/libs/ExcelWriter.ts
  5. 1 1
      excel/src/libs/Yc.ts
  6. BIN
      excel/sync.ffs_db

+ 2 - 2
excel/package-lock.json

@@ -1,12 +1,12 @@
 {
   "name": "@ycxxkj/uniapp",
-  "version": "1.0.16",
+  "version": "1.0.27",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
       "name": "@ycxxkj/uniapp",
-      "version": "1.0.16",
+      "version": "1.0.27",
       "license": "ISC",
       "dependencies": {
         "await-to-js": "^3.0.0"

+ 1 - 1
excel/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@ycxxkj/excel",
-  "version": "1.0.16",
+  "version": "1.0.27",
   "description": "ycxxkj excel libs",
   "main": "./dist/index.js",
   "types": "./dist/index.d.ts",

+ 31 - 1
excel/readme.md

@@ -1,5 +1,9 @@
 # excel组件
 
+## 基础依赖
+### write-excel-file
+文档:https://www.npmjs.com/package/write-excel-file
+
 ## 安装
 
 ```
@@ -17,7 +21,8 @@ yarn add @ycxxkj/excel
 ```
 import { ExcelWriter } from "@ycxxkj/excel";
 ```
-## 第2步,写导出方法
+## 第2步,写导出方法 
+###2.1 分页查询
 ```
     /**
      * 分页查询数据并导出
@@ -54,6 +59,10 @@ import { ExcelWriter } from "@ycxxkj/excel";
         await this._dealOutput(data);//导出数据
         return { code: 0 };//返回结果
     },
+```
+### 2.2 数据处理
+#### 方式1:通过定义表头与表体 (不支持合并单元格)
+```
     //=========私有方法================
     /**
      * 根据数据,构造打印EXCEL表格
@@ -112,6 +121,27 @@ import { ExcelWriter } from "@ycxxkj/excel";
         ExcelWriter.outputExcel(header, excelDataBody, options);//导出excel
     }
 ```
+#### 方式2 定义单元格内容
+数据data项的参数,支持write-excel-file中的Cell Parameters值
+```
+        async _dealOutput() {
+            //数据组合逻辑自行实现		
+			let data = [
+				{ poi: "A1", value: "表名", span: 4, align: "center" },
+				{ poi: "A2", value: "姓名" },
+				{ poi: "A3", value: "张三" },
+				{ poi: "A4", value: "李四" },
+				{ poi: "B2", value: "性别", span: 2 },
+				{ poi: "B3", value: "男", span: 3 },
+				{ poi: "B4", value: "女", rowSpan: 2, span: 2, alignVertical: "center", align: "center" },
+				{ poi: "d2", borderColor: "#ff0000" },
+				{ poi: "A6", borderColor: "#ff0000" },
+				{ poi: "D6", value: "" }
+			];
+			
+			ExcelWriter.outputExcelByData(data, { border: true, fileName: "测试导出.xlsx" });
+		}
+```
 
 ## 第3步,调用导出方法
 ```

+ 72 - 1
excel/src/libs/ExcelWriter.ts

@@ -43,7 +43,7 @@ function convertType(value: any, type: any) {
 	}
 }
 const ExcelWriter = {
-	async getColumnNames() {
+	getColumnNames() {
 		return columnNames;
 	},
 	async outputExcel(headerConfig: Array<any>, data: Array<any>, options: Options) {
@@ -77,6 +77,77 @@ const ExcelWriter = {
 		//excelData = [excelDataHeader, ...excelDataBody];
 		await this._output(excelData, options);
 	},
+
+	async outputExcelByData(data: Array<any>, options: Options) {
+		let columnArray = this.getColumnNames(); //await ExcelWriter.getColumnNames();
+		console.log("#debug#🚀 ~ file: test.vue:36 ~ testOne2Two ~ columnArray:", columnArray);
+		//rowSpan span
+		let arrayOrg = data;
+		console.log("#debug#🚀 ~ file: test.vue:42 ~ testOne2Two ~ arrayOrg:", JSON.stringify(arrayOrg));
+		let maxRow = 0;
+		let maxColumn = 0;
+		arrayOrg.forEach((item) => {
+			let letters = item.poi.replace(/[^a-z]/gi, "").toUpperCase();
+			let num = item.poi.replace(/\D/g, "");
+			let row = columnArray.indexOf(letters);
+			if (row > maxRow) {
+				maxRow = row;
+			}
+			let column = num;
+			if (column > maxColumn) {
+				maxColumn = column;
+			}
+			console.log("#debug#🚀 ~ file: test.vue:47 ~ arrayOrg.forEach ~ num:", num);
+			console.log("#debug#🚀 ~ file: test.vue:46 ~ testOne2Two ~ letters:", letters, columnArray.indexOf(letters));
+		});
+		let excelData = [] as any;
+		console.log("#debug#🚀 ~ file: test.vue:67 ~ testOne2Two ~ maxRow:", maxRow);
+		console.log("#debug#🚀 ~ file: test.vue:70 ~ testOne2Two ~ maxColumn:", maxColumn);
+		for (let i = 0; i < maxColumn; i++) {
+			excelData[i] = [];
+			for (let j = 0; j <= maxRow; j++) {
+				excelData[i][j] = null;
+			}
+		}
+		console.log("#debug#🚀 ~ file: test.vue:75 ~ arrayOrg.forEach ~ excelData:", excelData);
+		arrayOrg.forEach((item) => {
+			let letters = item.poi.replace(/[^a-z]/gi, "").toUpperCase();
+			let num = item.poi.replace(/\D/g, "");
+			let row = columnArray.indexOf(letters);
+			excelData[num - 1][row] = item;
+		});
+		console.log("#debug#🚀 ~ file: test.vue:75 ~ arrayOrg.forEach ~ excelData:", excelData);
+
+		console.log("#debug#🚀 ~ file: ExcelWriter.ts:82 ~ outputExcelByData ~ options:", options);
+		if (options?.border) {
+			//如果有边框
+			excelData.forEach((row: any, rowIndex: number) => {
+				console.log("#debug#🚀 ~ file: ExcelWriter.ts:87 ~ data.forEach ~ row:", row);
+				row.forEach((cell: any, cellIndex: number) => {
+					console.log("#debug#🚀 ~ file: ExcelWriter.ts:87 ~ data.forEach ~ cell:", cell);
+					let borderColor = "#000000";
+					if (cell) {
+						if (options?.borderColor) {
+							borderColor = options?.borderColor;
+						}
+						if (cell?.borderColor) {
+							borderColor = cell?.borderColor;
+						}
+						cell.borderColor = borderColor;
+						// cell.borderColor = options?.borderColor ?? "#000000";
+						console.log("#debug#🚀 ~ file: ExcelWriter.ts:87 ~ data.forEach ~ cell.borderColor:", cell.borderColor);
+					}
+				});
+			});
+			console.log("#debug#🚀 ~ file: ExcelWriter.ts:90 ~ data.forEach ~ data:", data);
+		}
+		if (!options.hasOwnProperty("fileName")) {
+			// 对象 options 上没有名为 "fileName" 的属性
+			options.fileName = "导出.xlsx";
+		}
+
+		await writeXlsxFile(excelData, options);
+	},
 	//==================私有方法============================
 
 	/**

+ 1 - 1
excel/src/libs/Yc.ts

@@ -1,6 +1,6 @@
 const Yc = {
 	getVersion() {
-		return "@ycxxkj/excel v1.0.16";
+		return "@ycxxkj/excel v1.0.28";
 	},
 	/**
 	 * X毫秒后执行

BIN
excel/sync.ffs_db