2017-03-20 09:02:25 +00:00
|
|
|
## Writing Workbooks
|
|
|
|
|
|
|
|
For writing, the first step is to generate output data. The helper functions
|
|
|
|
`write` and `writeFile` will produce the data in various formats suitable for
|
|
|
|
dissemination. The second step is to actual share the data with the end point.
|
|
|
|
Assuming `workbook` is a workbook object:
|
|
|
|
|
2017-04-30 20:37:53 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>nodejs write a file</b> (click to show)</summary>
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
/* output format determined by filename */
|
|
|
|
XLSX.writeFile(workbook, 'out.xlsx');
|
|
|
|
/* at this point, out.xlsx is a file that you can distribute */
|
|
|
|
```
|
|
|
|
|
2017-04-30 20:37:53 +00:00
|
|
|
</details>
|
|
|
|
|
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Browser download file</b> (click to show)</summary>
|
2017-04-30 20:37:53 +00:00
|
|
|
|
|
|
|
Note: browser generates binary blob and forces a "download" to client. This
|
2017-09-24 23:40:09 +00:00
|
|
|
example uses [FileSaver](https://github.com/eligrey/FileSaver.js/):
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
```js
|
2017-05-16 17:45:35 +00:00
|
|
|
/* bookType can be any supported output type */
|
2017-03-20 09:02:25 +00:00
|
|
|
var wopts = { bookType:'xlsx', bookSST:false, type:'binary' };
|
|
|
|
|
|
|
|
var wbout = XLSX.write(workbook,wopts);
|
|
|
|
|
|
|
|
function s2ab(s) {
|
|
|
|
var buf = new ArrayBuffer(s.length);
|
|
|
|
var view = new Uint8Array(buf);
|
|
|
|
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the saveAs call downloads a file on the local machine */
|
|
|
|
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "test.xlsx");
|
|
|
|
```
|
2017-04-30 20:37:53 +00:00
|
|
|
</details>
|
2017-03-20 09:02:25 +00:00
|
|
|
|
2017-09-05 05:26:50 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Browser upload to server</b> (click to show)</summary>
|
2017-09-05 05:26:50 +00:00
|
|
|
|
2017-09-24 23:40:09 +00:00
|
|
|
A complete example using XHR is [included in the XHR demo](demos/xhr/), along
|
2017-09-05 05:26:50 +00:00
|
|
|
with examples for fetch and wrapper libraries. This example assumes the server
|
|
|
|
can handle Base64-encoded files (see the demo for a basic nodejs server):
|
|
|
|
|
|
|
|
```js
|
|
|
|
/* in this example, send a base64 string to the server */
|
|
|
|
var wopts = { bookType:'xlsx', bookSST:false, type:'base64' };
|
|
|
|
|
|
|
|
var wbout = XLSX.write(workbook,wopts);
|
|
|
|
|
2017-09-24 23:40:09 +00:00
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
req.open("POST", "/upload", true);
|
2017-09-05 05:26:50 +00:00
|
|
|
var formdata = new FormData();
|
|
|
|
formdata.append('file', 'test.xlsx'); // <-- server expects `file` to hold name
|
|
|
|
formdata.append('data', wbout); // <-- `data` holds the base64-encoded data
|
2017-09-24 23:40:09 +00:00
|
|
|
req.send(formdata);
|
2017-09-05 05:26:50 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
2017-07-05 22:27:54 +00:00
|
|
|
### Writing Examples
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
- <http://sheetjs.com/demos/table.html> exporting an HTML table
|
2017-04-30 20:37:53 +00:00
|
|
|
- <http://sheetjs.com/demos/writexlsx.html> generates a simple file
|
2017-03-20 09:02:25 +00:00
|
|
|
|