Javascript 是否可以使用任何 HTML5 幻想将本地存储导出到 Excel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3286423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use any HTML5 fanciness to export local storage to Excel?
提问by Alex Mcp
This questionis similar, but doesn't highlight any possibilities to exportthe data. Thoughts?
这个问题很相似,但没有强调导出数据的任何可能性。想法?
回答by robertc
I think you're misunderstanding the answer to the question you linked to, it's suggesting you use a Data URI for export.
我认为您误解了您链接到的问题的答案,建议您使用数据 URI 进行导出。
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 inExcel 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:
Excel 是一个有点复杂的目标,因为文件格式本身就是二进制(或 OOXML)。如果您只想在Excel中打开某些内容,那么您可以将更直接的 CSV 导出为数据 URI。下面的代码有点粗略,只在 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.
演示在这里。单击您获得链接的按钮,单击该链接并获得一个文件。更改值,再次单击该链接,您将获得一个不同的文件。Firefox 每次打开都让我选择 Excel,但我不知道这是我的配置问题还是一般问题。

(source: boogdesign.com)

(来源: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() functionor implement your own base64 encoder.
就像我说的,只在 Firefox 中测试过,它只能在支持Data URIs 的浏览器中工作。您还需要window.btoa() 函数或实现您自己的base64 编码器。
回答by Vincent McNabb
I'm not aware of any Javascript libraries which can make an Excel file. But you could simply export it as HTML or CSV - note that Javascript cannot make files (yet), but the working draft of HTML caters for this: http://www.w3.org/TR/file-writer-api/
我不知道任何可以制作 Excel 文件的 Javascript 库。但是您可以简单地将其导出为 HTML 或 CSV - 请注意,Javascript 无法创建文件(尚),但 HTML 的工作草案可满足此要求:http: //www.w3.org/TR/file-writer-api/
Excel is quite good at reading tables made in HTML, so you could simply do that and open the HTML file with Excel.
Excel 非常擅长读取 HTML 中的表格,因此您可以简单地执行此操作并使用 Excel 打开 HTML 文件。
You can create a file for download using Downloadify: https://github.com/dcneiner/Downloadify
您可以使用 Downloadify 创建一个下载文件:https: //github.com/dcneiner/Downloadify

