接到个需求,需要把某个酒店的所有电表的 id 插入到我们数据库里然后跟房间对应上,接着给我了一个水电表的 xlsx 表格,我看了一下有将近 900 条 里面是一堆水电表数据,庆幸的是里面有房间号可以让我对比
因为我是前端,我们数据库那边我没有访问权限,只好默默地打开了酒店的管理页,然后控制台查询到所有房间后,转成 json 文件,之后又转成xlsx文件,这下就有了两个可以对比的文件了
接着 我把酒店导出的 xlsx 文件 过滤掉一大堆不需要的字段,这个可以功能代码实现可参考我刚才发布的博客
接着我就根据 xlsx 的表头 对每一列进行了对比后 把电表 xlsx 符合名称的 的设备 id 写入到 酒店导出的 xlsx 文件中,接着我就发现了一个问题,酒店导出的 xlsx 需要对比的那一列 14 楼是 13B 而电表对应的那一列 14 楼是 14 没办法我又把 酒店的 xlsx 13B 转成了 14 接着运行 大功告成!酒店的 xlsx 里面每个房间对应的电表 id成功写入
1、使用 Node.js(没有的话你要先安装node.js) 和 xlsx 库
npm install xlsx
npm install express multer xlsx
2、新建 server.js ,主要是对比和处理的脚本内容如下:
const express = require('express');
const multer = require('multer');
const xlsx = require('xlsx');
const fs = require('fs');
const path = require('path');
const app = express();
const upload = multer({ dest: 'uploads/' });
// 设定静态文件目录
app.use(express.static('public'));
//我这里是有个表列里面内容是13B 我需要转成 14 要不然不好对比 '13B' to '14'
const convertHotl = (hotl) => {
// Regular expression to match and convert '13B46' to '14'
return hotl.replace(/(\d+)B(\d+)/g, (match, p1, p2) => {
return (parseInt(p1) + 1).toString() + p2;
});
};
app.post('/upload', upload.fields([{ name: 'fileA' }, { name: 'fileB' }]), (req, res) => {
const fileAPath = req.files['fileA'][0].path;
const fileBPath = req.files['fileB'][0].path;
// 读取文件
const workbookA = xlsx.readFile(fileAPath);
const workbookB = xlsx.readFile(fileBPath);
// 获取第一个工作表
const sheetA = workbookA.Sheets[workbookA.SheetNames[0]];
const sheetB = workbookB.Sheets[workbookB.SheetNames[0]];
// 将工作表转换为JSON
const dataA = xlsx.utils.sheet_to_json(sheetA);
const dataB = xlsx.utils.sheet_to_json(sheetB);
// 对比和更新b.xlsx的数据
dataB.forEach(rowB => {
const hotlValue = convertHotl(rowB['hotl']);
for (let rowA of dataA) {
if (rowA['名称'] && rowA['名称'].includes(hotlValue)) {
rowB['号码'] = rowA['编号']; // 正确的赋值操作
break;
}
}
});
// 将更新后的数据转换回工作表
const newSheetB = xlsx.utils.json_to_sheet(dataB);
// 创建一个新的工作簿并附加工作表
const newWorkbookB = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(newWorkbookB, newSheetB, 'Sheet1');
// 将新工作簿写入文件
const outputFilePath = path.join(__dirname, 'uploads', 'output_b.xlsx');
xlsx.writeFile(newWorkbookB, outputFilePath);
// 发送处理后的文件作为响应
res.download(outputFilePath, 'output_b.xlsx', (err) => {
if (err) {
console.error('Error sending file:', err);
}
// 删除临时文件
fs.unlinkSync(fileAPath);
fs.unlinkSync(fileBPath);
fs.unlinkSync(outputFilePath);
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
3、创建一个 index.html 文件(我这里放到了与server.js 同级的public文件夹中你可以新建一个):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Compare Excel Files</title>
</head>
<body>
<h1>Compare Excel Files</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="fileA">File A (a.xlsx): </label>
<input type="file" name="fileA" id="fileA" required><br><br>
<label for="fileB">File B (b.xlsx): </label>
<input type="file" name="fileB" id="fileB" required><br><br>
<button type="submit">对比导出</button>
</form>
</body>
</html>
4、在server.js同级目录中打开终端 运行服务
node server.js
5、我的项目结构:

535

被折叠的 条评论
为什么被折叠?



