Is it possible to get back workbook as a stream and upload it onto some server? #455

Closed
opened 2016-08-12 13:53:37 +00:00 by Sai1919 · 2 comments
Sai1919 commented 2016-08-12 13:53:37 +00:00 (Migrated from github.com)

Hi,

I am working with js-xlsx and my requirement is something like, I have json data out of which I will create a workbook and I want to convert workbook to a read stream and upload it onto some server. I couldn't figure out a way to do this with current functionality provided. Am I missing anything, is there a way to do this?. Some help needed. Thanks in advance.

Hi, I am working with js-xlsx and my requirement is something like, I have json data out of which I will create a workbook and I want to convert workbook to a read stream and upload it onto some server. I couldn't figure out a way to do this with current functionality provided. Am I missing anything, is there a way to do this?. Some help needed. Thanks in advance.
SheetJSDev commented 2017-05-16 16:56:06 +00:00 (Migrated from github.com)

Break it down to steps:

I have json data

[{a:1,b:2},{a:3,c:4},{b:5,c:6}]

out of which I will create a workbook:

var new_wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet( new_wb, XLSX.utils.json_to_sheet(data), "SheetJS" );

and I want to convert workbook to a read stream and upload it onto some server.

To upload, the most popular approach is probably the request module.

If you specifically need CSV, there is a CSV stream writer:

var stream = XLSX.stream.to_csv(new_wb);
stream.pipe(request.put(UPLOAD_URL));

For other formats you need to use XLSX.write or XLSX.writeFile. For example, the request documentation shows how to upload a file by filename. That is easily adapted:

XLSX.write(new_wb, "tmp.xlsx", {type:'buffer', bookType:'xlsx'});
fs.createReadStream('tmp.xlsx').pipe(request.put(UPLOAD_URL))

If you don't want to use a temporary file, you can get the raw buffer and use request:

var buffer = XLSX.write(new_wb, {type:'buffer', bookType:'xlsx'});
request({
  url: UPLOAD_URL,
  body: buffer
}, function() { /* callback function */ });
Break it down to steps: > I have json data ```js [{a:1,b:2},{a:3,c:4},{b:5,c:6}] ``` > out of which I will create a workbook: ```js var new_wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet( new_wb, XLSX.utils.json_to_sheet(data), "SheetJS" ); ``` > and I want to convert workbook to a read stream and upload it onto some server. To upload, the most popular approach is probably the [request module](https://www.npmjs.com/package/request). If you specifically need CSV, there is a CSV stream writer: ```js var stream = XLSX.stream.to_csv(new_wb); stream.pipe(request.put(UPLOAD_URL)); ``` For other formats you need to use `XLSX.write` or `XLSX.writeFile`. For example, the request documentation shows how to upload a file by filename. That is easily adapted: ```js XLSX.write(new_wb, "tmp.xlsx", {type:'buffer', bookType:'xlsx'}); fs.createReadStream('tmp.xlsx').pipe(request.put(UPLOAD_URL)) ``` If you don't want to use a temporary file, you can get the raw buffer and use request: ```js var buffer = XLSX.write(new_wb, {type:'buffer', bookType:'xlsx'}); request({ url: UPLOAD_URL, body: buffer }, function() { /* callback function */ }); ```
shubhod commented 2020-11-23 05:54:01 +00:00 (Migrated from github.com)

facing this issue still

I am facing some similar issue
In this particular code I am not able to display workbook on my screen in even binary form

 const DUMMY_DATA = [
    [{AACol:"AAVal"},{ABCol:"ABVal"}],
    [{BACol:"BAVal"},{BBCol:"BBVal"}],
    [{CACol:"CAVal"},{CBCol:"CBVal"}],
  ];

 const  WORK_SHEET_NAMES = ["Test Sheet-1", "Test Sheet-2", "Test `Sheet-3"];

let workBook = XLSX.utils.book_new();
dataFinal.forEach((data, index) => {
let workSheet = XLSX.utils.json_to_sheet(data);
XLSX.utils.book_append_sheet(workBook, workSheet, workSheetNames[index])});
var stream = XLSX.stream.to_csv(workBook);
stream.pipe(process.stdout);

and if I write it in file according to the documentation still the file is empty

  var stream = XLSX.stream.to_csv(workBook);
  stream.pipe(fs.createWriteStream(path.join(__dirname,"work-book.xlsx")));
### facing this issue still I am facing some similar issue In this particular code I am not able to display workbook on my screen in even binary form ``` javascript const DUMMY_DATA = [ [{AACol:"AAVal"},{ABCol:"ABVal"}], [{BACol:"BAVal"},{BBCol:"BBVal"}], [{CACol:"CAVal"},{CBCol:"CBVal"}], ]; const WORK_SHEET_NAMES = ["Test Sheet-1", "Test Sheet-2", "Test `Sheet-3"]; let workBook = XLSX.utils.book_new(); dataFinal.forEach((data, index) => { let workSheet = XLSX.utils.json_to_sheet(data); XLSX.utils.book_append_sheet(workBook, workSheet, workSheetNames[index])}); var stream = XLSX.stream.to_csv(workBook); stream.pipe(process.stdout); ``` and if I write it in file according to the documentation still the file is empty ```javascript var stream = XLSX.stream.to_csv(workBook); stream.pipe(fs.createWriteStream(path.join(__dirname,"work-book.xlsx"))); ```
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#455
No description provided.