create and save excel file with nodejs - javascript

I am trying to create and save an excel file. I used this module
and followed their example. In my case I want to save the result locally. Somehow like this:
var fs = require('fs');
fs.writeFile(filename, result,"binary");
I cannot open the file via excel, because it's saying that the file is corrupted or broken. Looking again at the example of the module, I know that result is in a binary format.
What can I do to parse result correctly for saving the excel file?
Thanks in Advance.

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: Use an object, instead of path to it for specific function

I'm using the excel-as-json module (https://github.com/stevetarver/excel-as-json) and I have set it up so that it translates my .xlsx files to .json, but I now changed it so that the .xlsx is uploaded by the user in the front-end of the app.
I would like to run the convert on the uploaded Excel file, but since I am getting the user to upload it - I don't actually have a path to it, only the file object itself. So excel-as-json tells me that it cannot find the src file [Object object]
The excel as json function call is:
convertExcel(src, dst, options, callback);
What is the best way to pass the object as src? What if I store the .xlsx in my mongoDB, could I pass in its path easier then?
Not solution, but workaround: Get user to copy in path to file and use this. Based on answer from: Getting file full path when uploading file in html in firefox
Note this workaround only works for localhost
Solution I solved my issue by using a different module - sheet.js. I get the user to upload their file using an <input> tag and then use sheet.js to parse it into json, before sending it to the server where it will be stored.

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.

Creating a pdf file from mpdf string output?

I'm using mPDF server side to create a pdf file. It works okay if I output the file to the server, however, I would like to return a string back to the client and build a pdf file from it which I can then use like any normal file from a file input.
server side, the (simplified) code is
$output_dest = 'S';
$content = $mpdf->Output($post_data->fileName, $output_dest);
//$mpdf->Output($post_data->fileName, 'F'); //just to check that the output should be correct
$response->setContent($content);
and client side i've tried using Blobs to create a file
var fileObj = new Blob([offerString], {type : 'application/pdf'});
but there are 2 problems. First, the blob, when sent to the server, doesn't have the required name. Secondly, the pdf file created (using window.saveAs to save the blob) is blank. It has the correct number of pages and author information, but it's completely blank.
If I use mPDF's file output, the resulting file is correct, so the problem must lie somewhere within the string->blob process.
Edit: The solution is to create the Blob not straight from the string but from an arrayBuffer. I've created the arrayBuffer using the solution suggested in another answer here

Extracting files with jszip and saving to local disk using npapi-file-io. Binary file save very slow

I'm using JSZip to open a zip file. Looping and saving each file as a binary file using the npapi-file-io plugin. The problem is; it's very slow. Extracting a zip locally takes a couple of minutes even for a 2mb zip file. If I save the files as text files it's very fast. But they must be saved as binary or they become corrupt. Any ideas? Please tell me I'm doing this wrong or the hard way.
//read contents of zip file
var zip = new JSZip(e.target.result);
$.each(zip.files, function (index, zipEntry) {
var filename = zipEntry.name;
//create directory else create file
var path = getPath(filename);
if (filename.match(/\/$/)) {
//plugin is the embeded npapi-file-io plugin
plugin.createDirectory(path);
} else {
//problem is here
plugin.saveBinaryFile(path, zipEntry.asUint8Array());
//this is faster and works with the txt files but not images ect.
//plugin.saveTextFile(path, zipEntry.data);
}
There is no performant way to send binary data to a plugin; basically to send something using a uint8array will always be very slow because of how npapi works, so you'd be better off if you could send it base64 encoded. What it comes down to is that the only way to really do this in a performant way is likely to create your own plugin.

Categories