-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx.js
32 lines (30 loc) · 1.16 KB
/
xlsx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const xlsx = require("xlsx");
const fs = require("fs");
function convertTxtToExcel(req, res) {
const txtFilePath = req.file.path;
try {
const [fileOriginalName] = req.file.originalname.split(".txt");
const excelFilePath = `${fileOriginalName}.xlsx`;
const txtFileContent = fs.readFileSync(txtFilePath, "utf-8");
const apiData = JSON.parse(txtFileContent);
const firstObject = apiData[0];
const keys = Object.keys(firstObject);
const worksheet = xlsx.utils.json_to_sheet(apiData, { header: keys });
const workbook = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(workbook, worksheet, "Sheet1");
xlsx.writeFile(workbook, excelFilePath);
res.download(excelFilePath, fileOriginalName, () => {
fs.unlinkSync(txtFilePath);
fs.unlinkSync(excelFilePath);
});
console.log(`${fileOriginalName}.xlsx created successfully✔️`);
} catch (error) {
console.log("An error occurred in convertTxtToExcel function❗", error);
fs.unlinkSync(txtFilePath);
res.json({
statusCode: 400,
message: "Not a JSON format❗ check the API in https://jsonlint.com/",
});
}
}
module.exports = convertTxtToExcel;