@@ -13,7 +13,8 @@ const csv = require("csv-parser");
13
13
const crypto = require ( "crypto-js" ) ;
14
14
const Fuse = require ( "fuse.js" ) ;
15
15
const colors = require ( "./lib/colors" ) ;
16
- const XLSX = require ( "xlsx" ) ;
16
+ const ExcelJS = require ( "exceljs" ) ;
17
+ const json2xls = require ( "json2xls" ) ;
17
18
18
19
function formatDateTime ( date ) {
19
20
const year = date . getFullYear ( ) ;
@@ -335,6 +336,10 @@ class jsonverse {
335
336
const csvString = await this . convertToCSV ( csvData ) ;
336
337
await fs . writeFile ( filePath , csvString , "utf8" ) ;
337
338
this . logSuccess ( `Data exported to CSV: ${ filePath } ` ) ;
339
+ } else if ( format === "xlsx" ) {
340
+ // Export as XLSX using exceljs
341
+ const filePath = this . getFilePath ( dataName + ".xlsx" ) ;
342
+ await this . writeDataToXLSX ( filePath , data ) ;
338
343
}
339
344
}
340
345
@@ -351,15 +356,14 @@ class jsonverse {
351
356
const csvData = await this . readCSV ( filePath ) ;
352
357
await this . writeDataByFileName ( dataName , csvData ) ;
353
358
this . logSuccess ( `Data imported from CSV: ${ filePath } ` ) ;
354
- } else if ( fileExtension === ".xlsx" || ".xls" ) {
355
- // Import XLSX data
356
- const xlsData = await fs . promises . readFile ( filePath ) ;
357
- const newData = this . xlsToJSON ( xlsData ) ;
358
- await this . writeDataByFileName ( dataName , newData ) ;
359
+ } else if ( format === "xlsx" ) {
360
+ // Import XLSX data using exceljs
361
+ const xlsxData = await this . readXLSX ( filePath ) ;
362
+ await this . writeDataByFileName ( dataName , xlsxData ) ;
359
363
this . logSuccess ( `Data imported from XLSX: ${ filePath } ` ) ;
360
364
} else {
361
365
// Handle unsupported file format
362
- this . logError ( `Unsupported file format: ${ fileExtension } ` ) ;
366
+ this . logError ( `Unsupported file format: ${ format } ` ) ;
363
367
}
364
368
}
365
369
@@ -390,23 +394,71 @@ class jsonverse {
390
394
} ) ;
391
395
}
392
396
393
- // Function to read data from an XLSX file
397
+ // Function to read data from an XLSX file using exceljs
394
398
async readXLSX ( filePath ) {
395
399
try {
396
- const xlsData = await fs . promises . readFile ( filePath ) ;
397
- const parsedData = this . xlsToJSON ( xlsData ) ;
398
- return parsedData ;
400
+ const workbook = new ExcelJS . Workbook ( ) ;
401
+ await workbook . xlsx . readFile ( filePath ) ;
402
+ const worksheet = workbook . getWorksheet ( 1 ) ; // Assuming there's only one sheet
403
+
404
+ const data = [ ] ;
405
+ worksheet . eachRow ( ( row , rowNumber ) => {
406
+ if ( rowNumber !== 1 ) {
407
+ const rowData = row . values . map ( ( cell ) => cell ) ;
408
+ data . push ( rowData ) ;
409
+ }
410
+ } ) ;
411
+
412
+ return data ;
399
413
} catch ( error ) {
400
414
this . logError ( `Reading XLSX file: ${ error } ` ) ;
401
415
return null ;
402
416
}
403
417
}
404
418
405
- async jsonToXLS ( data , sheetName = "Sheet1" ) {
406
- const ws = XLSX . utils . json_to_sheet ( data ) ;
407
- const wb = XLSX . utils . book_new ( ) ;
408
- XLSX . utils . book_append_sheet ( wb , ws , sheetName ) ;
409
- return XLSX . write ( wb , { bookType : "xlsx" , type : "buffer" } ) ;
419
+ // Helper method to write data to an XLSX file using exceljs
420
+ async writeDataToXLSX ( filePath , data ) {
421
+ const workbook = new ExcelJS . Workbook ( ) ;
422
+ const worksheet = workbook . addWorksheet ( "Sheet1" ) ;
423
+
424
+ // Add headers to the worksheet
425
+ if ( data . length > 0 ) {
426
+ const headers = Object . keys ( data [ 0 ] ) ;
427
+ worksheet . addRow ( headers ) ;
428
+ }
429
+
430
+ // Add data rows to the worksheet
431
+ data . forEach ( ( row ) => {
432
+ worksheet . addRow ( Object . values ( row ) ) ;
433
+ } ) ;
434
+
435
+ await workbook . xlsx . writeFile ( filePath ) ;
436
+ this . logSuccess ( `Data exported to XLSX: ${ filePath } ` ) ;
437
+ }
438
+
439
+ // Export Data to XLSX
440
+ async exportDataToXLSX ( dataName ) {
441
+ const data = await this . readData ( dataName ) ;
442
+
443
+ // Check if the data is empty
444
+ if ( data . length === 0 ) {
445
+ this . logError ( `No data to export in ${ dataName } .` ) ;
446
+ return ;
447
+ }
448
+
449
+ // Create an XLSX object from the JSON data
450
+ const xls = json2xls ( data ) ;
451
+
452
+ // Specify the file path where the XLSX file will be saved
453
+ const filePath = this . getFilePath ( dataName + ".xlsx" ) ;
454
+
455
+ try {
456
+ // Save the XLSX data to the file
457
+ fs . writeFileSync ( filePath , xls , "binary" ) ;
458
+ this . logSuccess ( `Data exported to XLSX: ${ filePath } ` ) ;
459
+ } catch ( error ) {
460
+ this . logError ( `Failed to export data to XLSX: ${ error } ` ) ;
461
+ }
410
462
}
411
463
412
464
// Function to convert XLS data to JSON
0 commit comments