简体   繁体   English

是否可以使用任何HTML5功能将本地存储导出到Excel?

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

This question is similar, but doesn't highlight any possibilities to export the data. 这个问题类似,但没有强调导出数据的任何可能性。 Thoughts? 思考?

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 ). Excel是一个复杂的目标,因为文件格式本身是二进制( 或OOXML )。 If you just want something that opens in Excel then you can export the more straightforward CSV as a data URI. 如果您只想 Excel中打开某些内容,则可以将更直接的CSV导出为数据URI。 The following code is a bit rough and ready and has only been tested in Firefox: 以下代码有点粗略,准备就绪,只在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来打开它,但我不知道这是我的配置还是一般问题。

Excel 2007中的CSV
(source: boogdesign.com ) (来源: boogdesign.com

Like I said, only tested in Firefox, and it will only work in browsers which support Data URIs . 就像我说的,只在Firefox中测试过,它只适用于支持数据URI的浏览器。 You also need the window.btoa() function or implement your own base64 encoder . 您还需要window.btoa()函数或实现自己的base64编码器

I'm not aware of any Javascript libraries which can make an Excel file. 我不知道任何可以制作Excel文件的Javascript库。 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/ 但你可以简单地将其导出为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM