在 jQuery 中导出到 csv
我正在动态生成一个 div,如下所示:
<div id='PrintDiv'>
<table id="mainTable">
<tr>
<td>
Col1
</td>
<td>
Col2
</td>
<td>
Col3
</td>
</tr>
<tr>
<td>
Val1
</td>
<td>
Val2
</td>
<td>
Val3
</td>
</tr>
<tr>
<td>
Val11
</td>
<td>
Val22
</td>
<td>
Val33
</td>
</tr>
<tr>
<td>
Val111
</td>
<td>
Val222
</td>
<td>
Val333
</td>
</tr>
</table>
</div>
页面上还有更多元素。 现在,我如何
Col1,Col2,Col3
Val1,Val2,Val3
Val11,Val22,Val33
Val111,Val222,Val333
使用 jQuery 获得这样的 csv 文件?
还需要一个文件保存对话框,如下所示:
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您只能在客户端、接受 数据 URI 的浏览器中执行此操作:
在您的示例中数据 URI 必须是:
您可以通过以下方式调用此 URI:
window.open
window.location
href
通过添加download
属性来锚定要进行测试,只需复制上面的 URI 并粘贴到浏览器地址栏中。或者在 HTML 页面中测试下面的锚点:
要创建内容,从表中获取值,您可以使用 table2CSV 和 do:
大多数(如果不是全部)版本的 IE 不支持导航到数据链接,因此必须实施黑客攻击,通常使用
iframe
。使用iFrame
与document.execCommand('SaveAs'..)
< /a>,您可以在当前使用的大多数 IE 版本上获得类似的行为。You can do that in the client side only, in browser that accept Data URIs:
In your example the Data URI must be:
You can call this URI by:
window.open
window.location
href
of an anchordownload
attribute it will work in chrome, still have to test in IE.To test, simply copy the URIs above and paste in your browser address bar. Or test the anchor below in a HTML page:
To create the content, getting the values from the table, you can use table2CSV and do:
Most, if not all, versions of IE don't support navigation to a data link, so a hack must be implemented, often with an
iframe
. Using aniFrame
combined withdocument.execCommand('SaveAs'..)
, you can get similar behavior on most currently used versions of IE.这是我的实现(基于:https://gist.github.com/3782074):
用法:
HTML:
JS:
代码:
注释:
额外。
更新:
我之前的实现工作正常,但没有设置 csv 文件名。代码已修改为使用文件名,但需要 << a>元素。看来你不能动态生成 < a>元素并触发“click”事件(也许是安全原因?)。
DEMO
http://jsfiddle.net/nLj74t0f/
(不幸的是jsfiddle无法生成文件,而是抛出错误:“请使用 POST 请求”,不要让该错误阻止您在应用程序中测试此代码)。
This is my implementation (based in: https://gist.github.com/3782074):
Usage:
HTML:
JS:
Code:
Notes:
added.
UPDATE:
My previous implementation worked fine but it didn't set the csv filename. The code was modified to use a filename but it requires an < a > element. It seems that you can't dynamically generate the < a > element and fire the "click" event (perhaps security reasons?).
DEMO
http://jsfiddle.net/nLj74t0f/
(Unfortunately jsfiddle fails to generate the file and instead it throws an error: 'please use POST request', don't let that error stop you from testing this code in your application).
我最近为此发布了一个免费软件库: "html5csv.js" -- GitHub
它的目的是帮助简化小型模拟器应用程序的创建
在 Javascript 中可能需要导入或导出 csv 文件、操作、显示、编辑
加载“html5csv.js”后,
扫描表格并创建 CSV 的问题就简单了:
这是一个 使用此代码的示例的 JSFiddle 演示。
在内部,对于 Firefox/Chrome,这是一个面向数据 URL 的解决方案,类似于 @italo、@lepe 和 @adeneo(在另一个问题上)提出的解决方案。对于 IE
CSV.begin()
调用设置系统将数据读入内部数组。然后就会发生该提取。然后.download()
在内部生成一个数据 URL 链接并使用链接点击器单击它。这会将文件推送给最终用户。根据 caniuse IE10 不支持
。因此,对于 IE,我的库在内部调用
navigator.msSaveBlob()
,如 @Manu Sharma 建议的那样I recently posted a free software library for this: "html5csv.js" -- GitHub
It is intended to help streamline the creation of small simulator apps
in Javascript that might need to import or export csv files, manipulate, display, edit
the data, perform various mathematical procedures like fitting, etc.
After loading "html5csv.js" the problem of scanning a table and creating a CSV is a one-liner:
Here is a JSFiddle demo of your example with this code.
Internally, for Firefox/Chrome this is a data URL oriented solution, similar to that proposed by @italo, @lepe, and @adeneo (on another question). For IE
The
CSV.begin()
call sets up the system to read the data into an internal array. That fetch then occurs. Then the.download()
generates a data URL link internally and clicks it with a link-clicker. This pushes a file to the end user.According to caniuse IE10 doesn't support
<a download=...>
. So for IE my library callsnavigator.msSaveBlob()
internally, as suggested by @Manu Sharma以下是针对仅从客户端触发下载问题的两个解决方法。在更高版本的浏览器中,您应该查看“blob”
1。拖放表格
您知道可以简单地将表格拖放到 Excel 中吗?
以下是如何选择要剪切和粘贴或拖动的表格
使用Javascript选择一个完整的表格(要复制到剪贴板)
2.从您的 div 创建弹出页面
虽然它不会生成保存对话框,但如果生成的弹出页面以扩展名 .csv,Excel 将正确处理它。
该字符串可以是
w.document.write("row1.1\trow1.2\trow1.3\nrow2.1\trow2.2\trow2.3");
例如,用制表符分隔行并使用换行符。
有些插件可以为您创建字符串 - 例如 http://plugins.jquery.com/project /table2csv
免责声明:这些是解决方法,并不能完全回答当前对大多数浏览器都有答案的问题:仅在客户端上不可能
Here are two WORKAROUNDS to the problem of triggering downloads from the client only. In later browsers you should look at "blob"
1. Drag and drop the table
Did you know you can simply DRAG your table into excel?
Here is how to select the table to either cut and past or drag
Select a complete table with Javascript (to be copied to clipboard)
2. create a popup page from your div
Although it will not produce a save dialog, if the resulting popup is saved with extension .csv, it will be treated correctly by Excel.
The string could be
w.document.write("row1.1\trow1.2\trow1.3\nrow2.1\trow2.2\trow2.3");
e.g. tab-delimited with a linefeed for the lines.
There are plugins that will create the string for you - such as http://plugins.jquery.com/project/table2csv
DISCLAIMER: These are workarounds, and does not fully answer the question which currently has the answer for most browser: not possible on the client only
仅使用 jQuery,您无法避免服务器调用。
但是,为了实现此结果,我使用 Downloadify,它可以让我保存文件而无需另一个服务器调用。这样做可以减少服务器负载并带来良好的用户体验。
要获得正确的 CSV,您只需删除所有不必要的标签并在数据之间添加“,”即可。
By using just jQuery, you cannot avoid a server call.
However, to achieve this result, I'm using Downloadify, which lets me save files without having to make another server call. Doing this reduces server load and makes a good user experience.
To get a proper CSV you just have to take out all the unnecessary tags and put a ',' between the data.
您无法避免此处的服务器调用,JavaScript 根本无法(出于安全原因)将文件保存到用户的文件系统。您必须将数据提交到服务器,并让服务器将
.csv
作为链接或附件直接发送。HTML5 有一些能力来做到这一点(尽管保存确实不是) t 指定 - 只是一个用例,如果需要,您可以读取该文件),但现在还没有跨浏览器解决方案。
You can't avoid a server call here, JavaScript simply cannot (for security reasons) save a file to the user's file system. You'll have to submit your data to the server and have it send the
.csv
as a link or an attachment directly.HTML5 has some ability to do this (though saving really isn't specified - just a use case, you can read the file if you want), but there's no cross-browser solution in place now.
希望下面的demo可以帮助到您。
Hope the following demo can help you out.
只需尝试以下编码...使用 HTML 表的值生成 CSV 非常简单。不会出现浏览器问题
Just try the following coding...very simple to generate CSV with the values of HTML Tables. No browser issues will come