excel-csv-handler 1.0.10 → 1.0.11
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/package.json +6 -1
- package/src/excel-csv-handler.d.ts +9 -1
- package/src/index.js +68 -8
- package/debug.js +0 -121
- package/test-output/multi-append.csv +0 -4
- package/test-output/new-file.csv +0 -4
- package/test-output/special.csv +0 -4
- package/test-output/test.csv +0 -6
- package/test-output/test.xlsx +0 -0
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "excel-csv-handler",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "A Node.js utility to read/write Excel and CSV files with GBK encoding support",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "src/excel-csv-handler.d.ts",
|
|
7
7
|
"type": "module",
|
|
8
|
+
"files": [
|
|
9
|
+
"src/",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
8
12
|
"keywords": [
|
|
9
13
|
"excel",
|
|
10
14
|
"csv",
|
|
@@ -16,6 +20,7 @@
|
|
|
16
20
|
"author": "Chao_bei",
|
|
17
21
|
"license": "MIT",
|
|
18
22
|
"dependencies": {
|
|
23
|
+
"chardet": "^2.1.1",
|
|
19
24
|
"fast-csv": "^5.0.5",
|
|
20
25
|
"iconv-lite": "^0.7.0",
|
|
21
26
|
"xlsx": "^0.18.5"
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
// src/excel-csv-handler.d.ts
|
|
2
2
|
export default class ExcelCsvHandler {
|
|
3
|
+
/**
|
|
4
|
+
* 检测文件编码(用于 CSV 文件)
|
|
5
|
+
* @param filePath - 文件路径
|
|
6
|
+
* @returns 检测到的编码格式 (utf8 或 gbk)
|
|
7
|
+
*/
|
|
8
|
+
detectEncoding(filePath: string): string;
|
|
3
9
|
/**
|
|
4
10
|
* 读取 Excel 或 CSV 文件(支持 GBK 编码)
|
|
5
11
|
* @param filePath - 文件路径
|
|
@@ -30,10 +36,12 @@ export default class ExcelCsvHandler {
|
|
|
30
36
|
* @param filePath - 文件路径
|
|
31
37
|
* @param data - 要追加的数据
|
|
32
38
|
* @param headers - 可选列顺序(如果文件不存在会自动创建并写入标题行)
|
|
39
|
+
* @param encoding - 编码格式,默认 'gbk',可选 'utf8'
|
|
33
40
|
*/
|
|
34
41
|
appendCsv(
|
|
35
42
|
filePath: string,
|
|
36
43
|
data: Array<Record<string, any>>,
|
|
37
|
-
headers?: string[] | null
|
|
44
|
+
headers?: string[] | null,
|
|
45
|
+
encoding?: string
|
|
38
46
|
): Promise<void>;
|
|
39
47
|
}
|
package/src/index.js
CHANGED
|
@@ -6,9 +6,48 @@ import * as path from 'path';
|
|
|
6
6
|
import iconv from 'iconv-lite';
|
|
7
7
|
import * as csv from 'fast-csv';
|
|
8
8
|
import { writeToString } from 'fast-csv';
|
|
9
|
+
import chardet from 'chardet';
|
|
9
10
|
const require = createRequire(import.meta.url);
|
|
10
11
|
const XLSX = require('xlsx');
|
|
11
12
|
class ExcelCsvHandler {
|
|
13
|
+
/**
|
|
14
|
+
* 检测文件编码(用于 CSV 文件)
|
|
15
|
+
* @param {string} filePath - 文件路径
|
|
16
|
+
* @returns {string} 检测到的编码格式 (utf8 或 gbk)
|
|
17
|
+
*/
|
|
18
|
+
detectEncoding(filePath) {
|
|
19
|
+
// 读取文件的前几个字节来检测编码
|
|
20
|
+
const buffer = fs.readFileSync(filePath);
|
|
21
|
+
|
|
22
|
+
// 检查 UTF-8 BOM
|
|
23
|
+
if (buffer.length >= 3 &&
|
|
24
|
+
buffer[0] === 0xEF &&
|
|
25
|
+
buffer[1] === 0xBB &&
|
|
26
|
+
buffer[2] === 0xBF) {
|
|
27
|
+
return 'utf8';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 使用 chardet 库检测编码
|
|
31
|
+
const detected = chardet.detect(buffer);
|
|
32
|
+
|
|
33
|
+
// 将检测结果映射到 iconv-lite 支持的编码名称
|
|
34
|
+
if (detected) {
|
|
35
|
+
const encoding = detected.toLowerCase();
|
|
36
|
+
|
|
37
|
+
// GB2312, GBK, GB18030 都映射为 gbk
|
|
38
|
+
if (encoding.includes('gb') || encoding.includes('gbk')) {
|
|
39
|
+
return 'gbk';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// UTF-8 相关
|
|
43
|
+
if (encoding.includes('utf-8') || encoding.includes('utf8')) {
|
|
44
|
+
return 'utf8';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 默认返回 gbk(考虑到中国用户常用 GBK)
|
|
49
|
+
return 'gbk';
|
|
50
|
+
}
|
|
12
51
|
/**
|
|
13
52
|
* 读取 Excel 或 CSV 文件(支持 GBK 编码)
|
|
14
53
|
* @param {string} filePath - 文件路径
|
|
@@ -97,10 +136,13 @@ class ExcelCsvHandler {
|
|
|
97
136
|
// ========= CSV 读写 =========
|
|
98
137
|
|
|
99
138
|
async #readCsvFile(filePath, headerRow) {
|
|
139
|
+
// 自动检测编码
|
|
140
|
+
const encoding = this.detectEncoding(filePath);
|
|
141
|
+
|
|
100
142
|
return new Promise((resolve, reject) => {
|
|
101
143
|
const allRows = [];
|
|
102
144
|
const stream = fs.createReadStream(filePath)
|
|
103
|
-
.pipe(iconv.decodeStream(
|
|
145
|
+
.pipe(iconv.decodeStream(encoding))
|
|
104
146
|
.pipe(csv.parse({ headers: false, skipLines: headerRow }));
|
|
105
147
|
|
|
106
148
|
stream.on('data', row => {
|
|
@@ -132,7 +174,14 @@ class ExcelCsvHandler {
|
|
|
132
174
|
});
|
|
133
175
|
}
|
|
134
176
|
|
|
135
|
-
|
|
177
|
+
/**
|
|
178
|
+
* 写入 CSV 文件(以 GBK 编码保存)
|
|
179
|
+
* @param {string} filePath - 文件路径
|
|
180
|
+
* @param {Array<Object>} data - 要写入的数据
|
|
181
|
+
* @param {Array<string>} headers - 可选列顺序
|
|
182
|
+
* @param {string} encoding - 编码格式,默认 'gbk',可选 'utf8'
|
|
183
|
+
*/
|
|
184
|
+
async #writeCsvFile(filePath, data, headers = null, encoding = 'gbk') {
|
|
136
185
|
if (!headers && data.length > 0) {
|
|
137
186
|
headers = Object.keys(data[0]);
|
|
138
187
|
} else if (!headers) {
|
|
@@ -145,8 +194,15 @@ class ExcelCsvHandler {
|
|
|
145
194
|
rowDelimiter: '\r\n',
|
|
146
195
|
});
|
|
147
196
|
|
|
148
|
-
const
|
|
149
|
-
|
|
197
|
+
const buffer = iconv.encode(csvString, encoding);
|
|
198
|
+
|
|
199
|
+
// 如果是 UTF-8,添加 BOM 标记以确保 Excel 正确识别
|
|
200
|
+
if (encoding === 'utf8') {
|
|
201
|
+
const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
|
|
202
|
+
fs.writeFileSync(filePath, Buffer.concat([bom, buffer]));
|
|
203
|
+
} else {
|
|
204
|
+
fs.writeFileSync(filePath, buffer);
|
|
205
|
+
}
|
|
150
206
|
}
|
|
151
207
|
|
|
152
208
|
/**
|
|
@@ -154,8 +210,9 @@ class ExcelCsvHandler {
|
|
|
154
210
|
* @param {string} filePath - 文件路径
|
|
155
211
|
* @param {Array<Object>} data - 要追加的数据
|
|
156
212
|
* @param {Array<string>} headers - 可选列顺序(如果文件不存在会自动创建并写入标题行)
|
|
213
|
+
* @param {string} encoding - 编码格式,默认 'gbk',可选 'utf8'
|
|
157
214
|
*/
|
|
158
|
-
async appendCsv(filePath, data, headers = null) {
|
|
215
|
+
async appendCsv(filePath, data, headers = null, encoding = 'gbk') {
|
|
159
216
|
if (!headers && data.length > 0) {
|
|
160
217
|
headers = Object.keys(data[0]);
|
|
161
218
|
} else if (!headers) {
|
|
@@ -167,9 +224,12 @@ class ExcelCsvHandler {
|
|
|
167
224
|
|
|
168
225
|
if (!fileExists) {
|
|
169
226
|
// 文件不存在,自动创建并写入标题行和数据
|
|
170
|
-
await this.#writeCsvFile(filePath, data, headers);
|
|
227
|
+
await this.#writeCsvFile(filePath, data, headers, encoding);
|
|
171
228
|
return;
|
|
172
229
|
}
|
|
230
|
+
|
|
231
|
+
// 文件存在时,检测现有文件的编码
|
|
232
|
+
const detectedEncoding = this.detectEncoding(filePath);
|
|
173
233
|
|
|
174
234
|
// 文件存在,追加数据(不包含标题行)
|
|
175
235
|
const csvString = await writeToString(data, {
|
|
@@ -191,8 +251,8 @@ class ExcelCsvHandler {
|
|
|
191
251
|
}).join(',')
|
|
192
252
|
).join('\r\n') + '\r\n';
|
|
193
253
|
|
|
194
|
-
const
|
|
195
|
-
fs.appendFileSync(filePath,
|
|
254
|
+
const buffer = iconv.encode(rows, detectedEncoding);
|
|
255
|
+
fs.appendFileSync(filePath, buffer);
|
|
196
256
|
}
|
|
197
257
|
}
|
|
198
258
|
|
package/debug.js
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
// debug.js - 测试 ExcelCsvHandler
|
|
2
|
-
import ExcelCsvHandler from './src/index.js';
|
|
3
|
-
import * as fs from 'fs';
|
|
4
|
-
import * as path from 'path';
|
|
5
|
-
|
|
6
|
-
const handler = new ExcelCsvHandler();
|
|
7
|
-
|
|
8
|
-
// 创建测试目录
|
|
9
|
-
const testDir = './test-output';
|
|
10
|
-
if (!fs.existsSync(testDir)) {
|
|
11
|
-
fs.mkdirSync(testDir, { recursive: true });
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
console.log('🧪 开始测试 ExcelCsvHandler...\n');
|
|
15
|
-
|
|
16
|
-
// 测试数据
|
|
17
|
-
const testData = [
|
|
18
|
-
{ 姓名: '张三', 年龄: '25', 城市: '北京' },
|
|
19
|
-
{ 姓名: '李四', 年龄: '30', 城市: '上海' },
|
|
20
|
-
{ 姓名: '王五', 年龄: '28', 城市: '广州' }
|
|
21
|
-
];
|
|
22
|
-
|
|
23
|
-
const appendData = [
|
|
24
|
-
{ 姓名: '赵六', 年龄: '32', 城市: '深圳' },
|
|
25
|
-
{ 姓名: '孙七', 年龄: '27', 城市: '杭州' }
|
|
26
|
-
];
|
|
27
|
-
|
|
28
|
-
(async () => {
|
|
29
|
-
try {
|
|
30
|
-
// ==================== 测试 1: CSV 写入 ====================
|
|
31
|
-
console.log('📝 测试 1: CSV 写入');
|
|
32
|
-
const csvPath = path.join(testDir, 'test.csv');
|
|
33
|
-
await handler.write(csvPath, testData, ['姓名', '年龄', '城市']);
|
|
34
|
-
console.log('✅ CSV 写入成功:', csvPath);
|
|
35
|
-
|
|
36
|
-
// ==================== 测试 2: CSV 读取 ====================
|
|
37
|
-
console.log('\n📖 测试 2: CSV 读取');
|
|
38
|
-
const csvData = await handler.read(csvPath, 0);
|
|
39
|
-
console.log('✅ CSV 读取成功,数据:');
|
|
40
|
-
console.table(csvData);
|
|
41
|
-
|
|
42
|
-
// ==================== 测试 3: appendCsv - 追加到已存在的文件 ====================
|
|
43
|
-
console.log('\n➕ 测试 3: appendCsv - 追加数据到已存在文件');
|
|
44
|
-
await handler.appendCsv(csvPath, appendData, ['姓名', '年龄', '城市']);
|
|
45
|
-
console.log('✅ 追加成功');
|
|
46
|
-
|
|
47
|
-
// 读取追加后的数据
|
|
48
|
-
const appendedData = await handler.read(csvPath, 0);
|
|
49
|
-
console.log('📊 追加后的完整数据:');
|
|
50
|
-
console.table(appendedData);
|
|
51
|
-
|
|
52
|
-
// ==================== 测试 4: appendCsv - 自动创建新文件 ====================
|
|
53
|
-
console.log('\n🆕 测试 4: appendCsv - 自动创建新文件');
|
|
54
|
-
const newCsvPath = path.join(testDir, 'new-file.csv');
|
|
55
|
-
// 确保文件不存在
|
|
56
|
-
if (fs.existsSync(newCsvPath)) {
|
|
57
|
-
fs.unlinkSync(newCsvPath);
|
|
58
|
-
}
|
|
59
|
-
await handler.appendCsv(newCsvPath, testData, ['姓名', '年龄', '城市']);
|
|
60
|
-
console.log('✅ 新文件创建成功:', newCsvPath);
|
|
61
|
-
|
|
62
|
-
const newFileData = await handler.read(newCsvPath, 0);
|
|
63
|
-
console.log('📊 新文件数据:');
|
|
64
|
-
console.table(newFileData);
|
|
65
|
-
|
|
66
|
-
// ==================== 测试 5: Excel 写入和读取 ====================
|
|
67
|
-
console.log('\n📊 测试 5: Excel 写入和读取');
|
|
68
|
-
const xlsxPath = path.join(testDir, 'test.xlsx');
|
|
69
|
-
await handler.write(xlsxPath, testData, ['姓名', '年龄', '城市'], 'Sheet1');
|
|
70
|
-
console.log('✅ Excel 写入成功:', xlsxPath);
|
|
71
|
-
|
|
72
|
-
const xlsxData = await handler.read(xlsxPath, 0);
|
|
73
|
-
console.log('✅ Excel 读取成功,数据:');
|
|
74
|
-
console.table(xlsxData);
|
|
75
|
-
|
|
76
|
-
// ==================== 测试 6: 包含特殊字符的数据 ====================
|
|
77
|
-
console.log('\n🔤 测试 6: 包含特殊字符的数据');
|
|
78
|
-
const specialData = [
|
|
79
|
-
{ 姓名: '测试,逗号', 年龄: '25', 备注: '包含"引号"的内容' },
|
|
80
|
-
{ 姓名: '测试换行', 年龄: '30', 备注: '第一行\n第二行' }
|
|
81
|
-
];
|
|
82
|
-
const specialPath = path.join(testDir, 'special.csv');
|
|
83
|
-
await handler.write(specialPath, specialData, ['姓名', '年龄', '备注']);
|
|
84
|
-
console.log('✅ 特殊字符数据写入成功');
|
|
85
|
-
|
|
86
|
-
const specialReadData = await handler.read(specialPath, 0);
|
|
87
|
-
console.log('✅ 特殊字符数据读取成功:');
|
|
88
|
-
console.table(specialReadData);
|
|
89
|
-
|
|
90
|
-
// ==================== 测试 7: 连续追加多次 ====================
|
|
91
|
-
console.log('\n🔄 测试 7: 连续追加多次');
|
|
92
|
-
const multiAppendPath = path.join(testDir, 'multi-append.csv');
|
|
93
|
-
if (fs.existsSync(multiAppendPath)) {
|
|
94
|
-
fs.unlinkSync(multiAppendPath);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// 第一次追加(创建文件)
|
|
98
|
-
await handler.appendCsv(multiAppendPath, [testData[0]], ['姓名', '年龄', '城市']);
|
|
99
|
-
console.log(' ✓ 第 1 次追加完成');
|
|
100
|
-
|
|
101
|
-
// 第二次追加
|
|
102
|
-
await handler.appendCsv(multiAppendPath, [testData[1]], ['姓名', '年龄', '城市']);
|
|
103
|
-
console.log(' ✓ 第 2 次追加完成');
|
|
104
|
-
|
|
105
|
-
// 第三次追加
|
|
106
|
-
await handler.appendCsv(multiAppendPath, [testData[2]], ['姓名', '年龄', '城市']);
|
|
107
|
-
console.log(' ✓ 第 3 次追加完成');
|
|
108
|
-
|
|
109
|
-
const multiAppendData = await handler.read(multiAppendPath, 0);
|
|
110
|
-
console.log('✅ 连续追加测试成功,最终数据:');
|
|
111
|
-
console.table(multiAppendData);
|
|
112
|
-
|
|
113
|
-
console.log('\n✨ 所有测试通过!');
|
|
114
|
-
console.log(`\n📁 测试文件已保存到: ${path.resolve(testDir)}`);
|
|
115
|
-
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error('\n❌ 测试失败:', error.message);
|
|
118
|
-
console.error(error.stack);
|
|
119
|
-
process.exit(1);
|
|
120
|
-
}
|
|
121
|
-
})();
|
package/test-output/new-file.csv
DELETED
package/test-output/special.csv
DELETED
package/test-output/test.csv
DELETED
package/test-output/test.xlsx
DELETED
|
Binary file
|