是否可以使用任何HTML5的幻想将本地存储导出到Excel? [英] Is it possible to use any HTML5 fanciness to export local storage to Excel?

查看:114
本文介绍了是否可以使用任何HTML5的幻想将本地存储导出到Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是类似的,但不突出显示任何可能性>导出数据。想法?

This question is similar, but doesn't highlight any possibilities to export the data. Thoughts?

推荐答案

我认为你误解了你链接的问题的答案,这表示你使用数据URI导出。

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

Excel是一个复杂的目标,目标是文件格式本身是二进制(或OOXML )。如果您只想在Excel中打开的东西,那么您可以将更直接的CSV导出为数据URI。以下代码有点粗糙且准备好,仅在Firefox中进行了测试:

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);
}

这是页面标记:

<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>

演示这里。点击你获得链接的按钮,点击链接,你会得到一个文件。更改值,再次单击该链接,并获得不同的文件。 Firefox让我每次选择Excel打开它,但是我不知道这是我的配置还是一般问题。

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.

Excel 2007中的CSV http://www.boogdesign.com/images/external/data-uri-csv.png

像我所说的,只能在Firefox中测试,只能在支持数据URI 。您还需要 window.btoa()函数或实现您自己的 base64 encoder

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.

这篇关于是否可以使用任何HTML5的幻想将本地存储导出到Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆