Skip to content

Commit ca65b1a

Browse files
committed
chore: init
0 parents  commit ca65b1a

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data/
2+
node_modules/
3+
output.*

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# json2xlsx
2+
3+
使用 sheetjs,文档:https://docs.sheetjs.com/docs/getting-started/installation/nodejs
4+
5+
## 📊 示例
6+
7+
👇👇 将 json 数据 👇👇
8+
9+
```json
10+
[
11+
{ "name": "张三", "age": 18 },
12+
{ "name": "李四", "age": 20 }
13+
]
14+
```
15+
16+
👇👇 转换为 Excel 文件,格式如下 👇👇
17+
18+
| name | age |
19+
| ---- | --- |
20+
| 张三 | 18 |
21+
| 李四 | 20 |
22+
23+
## ✨ 安装
24+
25+
```js
26+
pnpm i
27+
```
28+
29+
## 💨 运行
30+
31+
1. 生成测试数据
32+
33+
```js
34+
pnpm init:data
35+
```
36+
37+
2. 将 json 数据转换为 excel
38+
39+
```js
40+
pnpm build
41+
```
42+
43+
## 🏗️ 产物
44+
45+
output.xlsx

index.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const XLSX = require('xlsx');
5+
const { dataPath } = require('./utils');
6+
// 创建一个工作簿
7+
const workbook = XLSX.utils.book_new();
8+
9+
// 读取数据,然后保存为 Excel 文件
10+
fs.readdir(dataPath, (err, files) => {
11+
if (err) {
12+
console.error(`读取文件夹失败,请将json数据存入 ${dataPath} 文件夹`);
13+
return;
14+
}
15+
files.forEach(file => {
16+
const filePath = path.join(dataPath, file);
17+
18+
const data = fs.readFileSync(filePath, 'utf8');
19+
saveData(JSON.parse(data), file.split('.')[0]);
20+
})
21+
console.log('数据已保存为 Excel 文件');
22+
});
23+
24+
/**
25+
* 保存数据为 Excel 文件
26+
*/
27+
function saveData(data, fileName) {
28+
// 将 JSON 数据转换为工作表
29+
const worksheet = XLSX.utils.json_to_sheet(data);
30+
31+
// 将工作表添加到工作簿中
32+
XLSX.utils.book_append_sheet(workbook, worksheet, fileName);
33+
34+
// 将工作簿保存为 Excel 文件
35+
XLSX.writeFile(workbook, 'output.xlsx');
36+
}

initData.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fs = require('fs');
2+
const { initData, dataPath } = require('./utils');
3+
4+
if (fs.existsSync(dataPath)) {
5+
// 如果存在则删除文件夹
6+
fs.rmSync(dataPath, { recursive: true });
7+
console.log(`清理旧的 ${dataPath} 文件夹`);
8+
}
9+
// 检查文件夹是否存在
10+
if (!fs.existsSync(dataPath)) {
11+
// 如果不存在则创建文件夹
12+
fs.mkdirSync(dataPath);
13+
console.log(`新的文件夹 ${dataPath} 创建成功`);
14+
}
15+
16+
initData();

package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "json2xlsx",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"init:data": "node initData.js",
8+
"excel": "node index.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.0/xlsx-0.20.0.tgz"
15+
}
16+
}

pnpm-lock.yaml

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 存放数据的文件夹
3+
*/
4+
const dataPath = 'data';
5+
6+
/**
7+
* 根据指定的格式生成单份json数据
8+
*/
9+
function generateRandomData(count = 1000) {
10+
const data = [];
11+
const cities = ['New York', 'San Francisco', 'Los Angeles', 'Chicago', 'Miami'];
12+
13+
for (let i = 0; i < count; i++) {
14+
const randomAge = Math.floor(Math.random() * 50) + 20; // 随机年龄在 20 到 70 之间
15+
const randomCity = cities[Math.floor(Math.random() * cities.length)]; // 随机选取城市
16+
17+
const person = {
18+
Name: `Person ${i + 1}`,
19+
Age: randomAge,
20+
City: randomCity,
21+
Random: Math.random(),
22+
};
23+
24+
data.push(person);
25+
}
26+
27+
return data;
28+
}
29+
30+
/**
31+
* 随机生成多份json数据,默认为5
32+
*/
33+
function initData(count = 5) {
34+
const fs = require('fs');
35+
for (let i = 1; i <= count; i++) {
36+
const data = generateRandomData(1000);
37+
fs.writeFileSync(`data/data${i}.json`, JSON.stringify(data));
38+
}
39+
}
40+
41+
module.exports = {
42+
dataPath,
43+
generateRandomData,
44+
initData,
45+
}

0 commit comments

Comments
 (0)