-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertXlsxToJson.js
43 lines (35 loc) · 1.21 KB
/
convertXlsxToJson.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
33
34
35
36
37
38
39
40
41
42
43
// Import required libraries
const XLSX = require('xlsx');
const fs = require('fs');
const readline = require('readline');
// Function to convert XLSX to JSON
function xlsxToJson(inputFile, outputFile) {
try {
// Read the XLSX file
const workbook = XLSX.readFile(inputFile, { cellDates: true });
// Get the first sheet name
const sheetName = workbook.SheetNames[0];
// Convert sheet to JSON
const sheet = workbook.Sheets[sheetName];
const jsonData = XLSX.utils.sheet_to_json(sheet, { dateNF: 'DD/MM/YY h:mm A' });
// Save JSON to file
fs.writeFileSync(outputFile, JSON.stringify(jsonData, null, 2));
console.log(`Successfully converted '${inputFile}' to '${outputFile}'`);
} catch (error) {
console.error(`Error converting file: ${error}`);
}
}
// Set up readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Ask for input file path
rl.question('Please enter the path to the input.xlsx file: ', (inputFile) => {
// Define output file path
const outputFile = './output.json';
// Convert the XLSX file to JSON
xlsxToJson(inputFile, outputFile);
// Close the readline interface
rl.close();
});