jQuery表到CSV导出 [英] jQuery Table to CSV export
问题描述
我正在使用jQuery Table to CSV Plugin。我已经更改了弹出窗口,以便它告诉浏览器下载CSV文件。
I'm using the jQuery Table to CSV Plugin. I've altered the popup so that it tells the browser to download a CSV file.
它是:
function popup(data) {
var generator = window.open('', 'csv', 'height=400,width=600');
generator.document.write('<html><head><title>CSV</title>');
generator.document.write('</head><body >');
generator.document.write('<textArea cols=70 rows=15 wrap="off" >');
generator.document.write(data);
generator.document.write('</textArea>');
generator.document.write('</body></html>');
generator.document.close();
return true;
}
我已将其更改为:
function popup(data) {
window.location='data:text/csv;charset=utf8,' + encodeURIComponent(data);
return true;
}
大部分都有效。它仍然需要您找到您的电子表格软件,并创建自己的文件名...因为它会创建一个奇怪的文件名(例如:14YuskG_.csv.part)。
It works, for the most part. It still requires that you find your spreadsheet software, and create your own filename...because it creates a strange file name (Example: 14YuskG_.csv.part).
关于如何改进这个的任何建议?
Any suggestions on how to improve this?
推荐答案
找到一个有效的解决方案(在 http://www.topsemtips.com/2008/11/save-html- table-to-excel-using-jquery / ):
Found a solution that works (with help from http://www.topsemtips.com/2008/11/save-html-table-to-excel-using-jquery/):
我将函数更改为:
function popup(data) {
$("#main div.inner").append('<form id="exportform" action="export.php" method="post" target="_blank"><input type="hidden" id="exportdata" name="exportdata" /></form>');
$("#exportdata").val(data);
$("#exportform").submit().remove();
return true;
}
并创建了文件export.php:
And created the file export.php:
<?php
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: filename=export.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $_REQUEST['exportdata'];
?>
更新:
IE7友好版本更多:
Update: A more IE7 friendly version:
<?php
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=filename.csv');
print $_POST['exportdata'];
?>
这篇关于jQuery表到CSV导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!