xlsx package Write excel file to a custom path

0 votes

Using the xlsx package, I'm attempting to write an array of objects to Excel. Instead of the current directory, I want to write the file to a specific path.

 const fileName ='ouptputs/test.xlsx'
const workSheet = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();
                
 XLSX.utils.book_append_sheet(wb, workSheet,fileName);
 const s = XLSX.writeFile(wb, fileName,{
     type:"file"
 });

But executing this I m getting the error

Sheet name cannot contain : \ / ? *

So how do I specify the path instead of generating the file to the current dir?

Dec 11, 2022 in Others by Kithuzzz
• 28,700 points
187 views

1 answer to this question.

0 votes

The issue is that when you send a string to the writeFile method as fileName, it attempts to locate the file with the name you gave in the current directory but it contains the character "/" that is not allowed in file names and is therefore creating the error.

So the solution is to create the path by using the "path" library, See below code for example

const path = require('path');

// You may need to use relative path in join function depending upon the working file location
const filePath = path.join(__dirname, 'ouptputs/test.xlsx'); // __dirname -> returns the current path of the working file
const workSheet = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();
                
XLSX.utils.book_append_sheet(wb, workSheet, 'Sheet1'); // Sheet1 is the name of sheet that is created inside workbook
const s = XLSX.writeFile(wb, filePath, {
        bookType: 'xlsx',
        type: 'file'
});

I hope this helps you.

answered Dec 11, 2022 by narikkadan
• 53,160 points

Related Questions In Others

0 votes
1 answer

Uipath(RPA) : read data from the PDF file and write to Excel file

If you want to use UiPath and ...READ MORE

answered Oct 17, 2022 in Others by narikkadan
• 53,160 points
176 views
0 votes
1 answer

How to open a huge excel file efficiently

A good example is using PIA's/Interop or ...READ MORE

answered Oct 27, 2022 in Others by narikkadan
• 53,160 points
95 views
0 votes
1 answer

Android: how to write a file to internal storage

Android offers openFileInput and openFileOutput from the ...READ MORE

answered Nov 4, 2022 in Others by gaurav
• 22,940 points
147 views
0 votes
1 answer
0 votes
1 answer

ExcelJS wrapText in Table columns

I implemented a workaround by detecting what ...READ MORE

answered Sep 30, 2022 in Others by narikkadan
• 53,160 points
551 views
0 votes
1 answer

How to merge two cells in excel with same field name

Insert 2 new columns, G & H. Enter ...READ MORE

answered Oct 7, 2022 in Others by narikkadan
• 53,160 points
168 views
0 votes
1 answer

Convert Excel to PDF using JavaScript

You're clobbering objExcel on line 15: var objExcel ...READ MORE

answered Oct 9, 2022 in Others by narikkadan
• 53,160 points
738 views
0 votes
1 answer

Export HTML Table to Excel- Doesn't Open in Office 2010

We had the same issue too many ...READ MORE

answered Oct 21, 2022 in Others by narikkadan
• 53,160 points
151 views
0 votes
1 answer

Convert csv- or Excel-file (xlsx) to kml with custom markers

With some help from the community, I ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 53,160 points
217 views
0 votes
1 answer

How to create a drop-down in excel with custom values

You can accomplish that using code rather ...READ MORE

answered Sep 25, 2022 in Others by narikkadan
• 53,160 points
148 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP