excel-csv-handler 1.0.5 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,7 +14,16 @@ npm install excel-csv-handler
14
14
  import ExcelCsvHandler from 'excel-csv-handler';
15
15
 
16
16
  const handler = new ExcelCsvHandler();
17
- await handler.write('output.csv', [{ name: '张三', age: 25 }]);
18
- const data = await handler.read('output.csv');
17
+ await handler.write('./output.csv', [{ name: '张三', age: 25 }]);
18
+ const data = await handler.read('./output.csv');
19
+ console.log(data);
20
+ ```
21
+
22
+ ```javascript
23
+ import ExcelCsvHandler from 'excel-csv-handler';
24
+
25
+ const handler = new ExcelCsvHandler();
26
+ await handler.write('output.xlsx', [{ name: '张三', age: 25 }]);
27
+ const data = await handler.read('output.xlsx');
19
28
  console.log(data);
20
29
  ```
package/package.json CHANGED
@@ -1,26 +1,26 @@
1
1
  {
2
- "name": "excel-csv-handler",
3
- "version": "1.0.5",
4
- "description": "A Node.js utility to read/write Excel and CSV files with GBK encoding support",
5
- "main": "src/index.js",
6
- "types": "src/excel-csv-handler.d.ts",
7
- "type": "module",
8
- "scripts": {
9
- "test": "echo \"Error: no test specified\" && exit 1"
10
- },
11
- "keywords": [
12
- "excel",
13
- "csv",
14
- "xlsx",
15
- "gbk",
16
- "node",
17
- "file"
18
- ],
19
- "author": "Chao_bei",
20
- "license": "MIT",
21
- "dependencies": {
22
- "fast-csv": "^5.0.5",
23
- "iconv-lite": "^0.7.0",
24
- "xlsx": "^0.18.5"
25
- }
26
- }
2
+ "name": "excel-csv-handler",
3
+ "version": "1.0.8",
4
+ "description": "A Node.js utility to read/write Excel and CSV files with GBK encoding support",
5
+ "main": "src/index.js",
6
+ "types": "src/excel-csv-handler.d.ts",
7
+ "type": "module",
8
+ "keywords": [
9
+ "excel",
10
+ "csv",
11
+ "xlsx",
12
+ "gbk",
13
+ "node",
14
+ "file"
15
+ ],
16
+ "author": "Chao_bei",
17
+ "license": "MIT",
18
+ "dependencies": {
19
+ "fast-csv": "^5.0.5",
20
+ "iconv-lite": "^0.7.0",
21
+ "xlsx": "^0.18.5"
22
+ },
23
+ "scripts": {
24
+ "test": "echo \"Error: no test specified\" && exit 1"
25
+ }
26
+ }
package/src/index.js CHANGED
@@ -28,16 +28,17 @@ class ExcelCsvHandler {
28
28
  }
29
29
 
30
30
  /**
31
- * 写入 Excel 或 CSV 文件(以 GBK 编码保存 CSV,Excel 保持默认)
32
- * @param {string} filePath - 文件路径
33
- * @param {Array<Object>} data - 要写入的数据
34
- * @param {Array<string>} headers - 可选列顺序
35
- */
36
- async write(filePath, data, headers = null) {
31
+ * 写入 Excel 或 CSV 文件(以 GBK 编码保存 CSV,Excel 保持默认)
32
+ * @param {string} filePath - 文件路径
33
+ * @param {Array<Object>} data - 要写入的数据
34
+ * @param {Array<string>} headers - 可选列顺序
35
+ * @param {string} sheetName - Excel sheet 名称(默认为 'Sheet1')
36
+ */
37
+ async write(filePath, data, headers = null, sheetName = 'Sheet1') {
37
38
  const ext = path.extname(filePath).toLowerCase();
38
39
 
39
40
  if (ext === '.xlsx' || ext === '.xls') {
40
- this.writeExcelFile(filePath, data, headers);
41
+ this.writeExcelFile(filePath, data, headers, sheetName);
41
42
  } else if (ext === '.csv') {
42
43
  await this.writeCsvFile(filePath, data, headers);
43
44
  } else {
@@ -79,7 +80,7 @@ class ExcelCsvHandler {
79
80
  });
80
81
  }
81
82
 
82
- writeExcelFile(filePath, data, headers = null) {
83
+ writeExcelFile(filePath, data, headers = null, sheetName = 'Sheet1') {
83
84
  if (!headers && data.length > 0) {
84
85
  headers = Object.keys(data[0]);
85
86
  } else if (!headers) {
@@ -89,7 +90,7 @@ class ExcelCsvHandler {
89
90
  const wsData = [headers, ...data.map(row => headers.map(h => row[h] ?? ''))];
90
91
  const worksheet = XLSX.utils.aoa_to_sheet(wsData);
91
92
  const workbook = XLSX.utils.book_new();
92
- XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
93
+ XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
93
94
  XLSX.writeFile(workbook, filePath);
94
95
  }
95
96
 
Binary file
Binary file
Binary file