How can I download CSV data from variable to a csv file? - javascript

I am using papa parse library. It helped me in converting JSON to CSV but how can I download the same data into a excel file. My data is huge

If I'm understanding your question correctly, you have a CSV file which you now want to tell the browser to download?
Have you looked at https://github.com/eligrey/FileSaver.js/ ?
Allows you to tell the browser to download a wide variety of files.
An example in using it:
var blob = new Blob(myBigCSVFile, {type: "text/csv;charset=utf-8"});
saveAs(blob, "file.csv");
The browser, upon reaching the saveAs function will download the file you specified.

Checkout SheetJS/js-xlsx library.
Supported read formats:
Excel 2007+ XML Formats (XLSX/XLSM),
Excel 2007+ Binary Format (XLSB),
Excel 2003-2004 XML Format (XML "SpreadsheetML"),
Excel 97-2004 (XLS BIFF8),
Excel 5.0/95 (XLS BIFF5),
OpenDocument Spreadsheet (ODS)
Supported write formats:
XLSX, CSV (and general DSV), JSON and JS objects (various styles)

Related

Convert the an Excel generated file to PDF in React

I want to convert the excel file into pdf. Likewise, I had previously generated the excel file with excelJs and save it on pc with file-saver plugin.
Now I just want to convert that same excel file blob into the Pdf file and then save to the pdf.
Here is my code snippet for saving the excel file.
const buf = await workbook.xlsx.writeBuffer();
const data = new Blob([buf], { type: fileType })
FileSaver.saveAs(data, 'User Listing Report' + fileExtension);
i think you need to use a combination of the following JavaScript libraries there's this project https://github.com/react-csv/react-csv
but i think in the server there is some problems with this method
i hope i help u !!!

JavaScript convert UTF-8 written data to ISO-8859. (Don't think URL/HTML context, but data file context)

I am currently creating a CSV as a downloadable attachment that gets attached to a record in a table. The framework itself just writes the data content in UTF-8 into the created file. It doesn't give me an buildin tool to chose another encoding. What JavaScript options do I have to encode the output stream written into the attachment with ISO-8859-1 instead of UTF-8?

Use Html5 to read Excel file

I need to read excel file data when upload the file .
Is there any way that can use html5 to read excel file data when file uploading in client side??
heard about
var reader = new FileReader();
any way that we can use this
Referring to #mituw16 comment on the question, take a look at the following question:
How to parse Excel file in Javascript/HTML5
As a seperate response to work from, I would suggest using a plugin like:
https://github.com/SheetJS/js-xlsx
To iterate through the spreadsheet and save the information in your database.

Warning when opening xls file which is exported from html table

I used the following code. I can download the file with extension .xls. When I open this downloaded file I receive this warning:
The file you are trying to open, 'Statement.xls', is in a different
format than specified by the file extension. Verify that the file is
not corrupted and is from a trusted source before opening the file. Do
you want to open the file now?
Javascript Code:
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.ms-excel;" });
saveAs(blob, "Statement.xls");`
HTML code:
`<table id="exportable">
<tbody>
<tr><th>ColumnOne</th><th>ColumnTwo</th><th>ColumnThree</th></tr>
<tr><td>row1Col1</td><td>row1Col2</td><td>row1Col3</td></tr>
<tr><td>row2Col1</td><td>row2Col2</td><td>row2Col3</td></tr>
<tr><td>row3Col1</td><td>row3Col2</td><td>test</td></tr>
</tbody>
</table>`
External library:
https://rawgithub.com/eligrey/FileSaver.js/master/FileSaver.js
Office and Excel are able to cope with two different file formats. As those formats are totally different from the inside (the old one is binary, the new one is simply zipped up XML) different extensions and mimetypes were assigned to these formats.
Obviously you are creating a file with the new format and assign it the old extension. Excel complains about that.
Change your code to look like this:
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
saveAs(blob, "Statement.xlsx");
For the sake of learning you might want to have a look at your created file with a hex editor.
Others had similar issues, have a look here: https://github.com/eligrey/FileSaver.js/issues/139
When saving file with *.xls extension, file type should be set to "application/xls"
Since file type and extension will be matching, you shouldn't get any warning.

How to create UTF-8 compliant CVS file with javascript that open automatically in Mac Excel 2011

How to you get this chrome API to download a CSV file that can be opened in Excel.
var csv = '123, 123, 美国'
chrome.downloads.download({
url: 'data:attachment/csv;charset=UTF-8,' + encodeURI(csv),
filename: name + '.csv',
saveAs: true
}
The root cause of the difficulty is that CSV file does not have any notion of encoding built in. And Excel when encounters a file with extension CSV it assumes it is single byte order and opens it that way. If you have double byte characters encoded in UTF-8 in your file Excel opens that file with garbage characters.
The answers in this question suggest that adding BOM to for UTF-8 will clue in Excel that this is a UTF-8 file.
Microsoft Excel mangles Diacritics in .csv files?
However no matter what we tried we could not get Excel to recognize the file Automatically. Using text import wizard or other text editors it works.
Here is a tool that lets you try various combinations. But none of the worked for us: http://jsfiddle.net/kimiliini/HM4rW/show/light/

Categories