How to Use Any HTML5 Fanciness to Export Local Storage to Excel

Is it possible to use any HTML5 fanciness to export local storage to Excel?

I think you're misunderstanding the answer to the question you linked to, it's suggesting you use a Data URI for export.

Excel is a bit of a complicated target to aim for as the file format is itself binary (or OOXML). If you just want something that opens in Excel then you can export the more straightforward CSV as a data URI. The following code is a bit rough and ready and has only been tested in Firefox:

function exportData() {
var data = '';
for (var i=1;i<=2;i++) {
var sep = '';
for (var j=1;j<=4;j++) {
data += sep + document.getElementById(i + '_' + j).value;
sep = ',';
}
data += '\r\n';
}
var exportLink = document.createElement('a');
exportLink.setAttribute('href', 'data:text/csv;base64,' + window.btoa(data));
exportLink.appendChild(document.createTextNode('test.csv'));
document.getElementById('results').appendChild(exportLink);
}

Here's the page markup:

<input type="number" id="1_1" value="2">,
<input type="number" id="1_2" value="1">,
<input type="number" id="1_3" value="4">,
<input type="number" id="1_4" value="3">
<br>
<input type="number" id="2_1" value="1">,
<input type="number" id="2_2" value="2">,
<input type="number" id="2_3" value="3">,
<input type="number" id="2_4" value="4">
<br>
<button onclick="exportData()">Export as CSV</button>
<div id="results"></div>

Demo here. Click the button you get a link, click the link and you get a file. Change the values, click the link again and you get a different file. Firefox made me select Excel every time to open it but I don't know whether that's my configuration or a general issue.

CSV in Excel 2007
(source: boogdesign.com)

Like I said, only tested in Firefox, and it will only work in browsers which support Data URIs. You also need the window.btoa() function or implement your own base64 encoder.

Import/Export HTML5 localStorage data

I think you can use the "HTML File API" to load from file and a "Data URI" (http://en.wikipedia.org/wiki/Data_URI_scheme) to save.

Write to CSV file locally with HTML5

Function:

function exportData() {
var data = '';
for (var i=1;i<=2;i++) {
var sep = '';
for (var j=1;j<=4;j++) {
data += sep + document.getElementById(i + '_' + j).value;
sep = ',';
}
data += '\r\n';
}
var exportLink = document.createElement('a');
exportLink.setAttribute('href', 'data:text/csv;base64,' + window.btoa(data));
exportLink.appendChild(document.createTextNode('test.csv'));
document.getElementById('results').appendChild(exportLink);
}

Markup:

<input type="number" id="1_1" value="2">,
<input type="number" id="1_2" value="1">,
<input type="number" id="1_3" value="4">,
<input type="number" id="1_4" value="3">
<br>
<input type="number" id="2_1" value="1">,
<input type="number" id="2_2" value="2">,
<input type="number" id="2_3" value="3">,
<input type="number" id="2_4" value="4">
<br>
<button onclick="exportData()">Export as CSV</button>
<div id="results"></div>

Demo:

Click the button you get a link, click the link and you get a file. Change the values, click the link again and you get a different file. Firefox made me select Excel every time to open it but I don't know whether that's my configuration or a general issue.

Live Demo

Demo

Reference:

Is it possible to use any HTML5 fanciness to export local storage to Excel?

Javascript Writing to PDF or Excel

Excellent Options for CSV Everyone:

http://snapshotmedia.co.uk/blog/jspdf

Is it possible to use any HTML5 fanciness to export local storage to Excel?

http://css-tricks.com/localstorage-examples/

Local Storage manipulation and possible sending

Thanks!

Data URI used to export to CSV/Excel (no server-side request) : browser support/limitations?

I have not been able to reliably make it work in IE. You could use server side resources for IE and data urls for everything else, but if you are writing that server side code anyway, then in most cases it is probably better to use it universally.

I found a related question at:

Data URI scheme and Internet Explorer 9 Errors

If it absolutely must be done on the client side and usage of flash is acceptable, you could try something like:

http://datatables.net/extras/tabletools/

Does HTML 5 allow to invoke save file dialog with file created in JavaScript?

I've tested data-uri approach. Currently it works only in Firefox.

Seems that for now it is better to stay with flash: https://github.com/dcneiner/Downloadify

Make a button that saves html page in Chrome (locally)

I don't think this is possible, as this would likely be a security violation.

You mention specifically that you are running this locally. Does that mean you are trying to create some sort of application? If so, you might throw it in to something like Brackets Shell. If you put it in there, you can implement your own native function to trigger the "Save As" dialog. It'd be a bit more complicated, but probably one of the only ways to accomplish it if you really needed an on-page Save As button.



Related Topics



Leave a reply



Submit