我正在尝试refer to this code我们在点击链接时下载CSV文件。
$(document).ready(function () {
function exportTableToCSV($table, filename) {
var $rows = $table.find('tr:has(td)'),
// Temporary delimiter characters unlikely to be typed by keyboard
// This is to avoid accidentally splitting the actual contents
tmpColDelim = String.fromCharCode(11), // vertical tab character
tmpRowDelim = String.fromCharCode(0), // null character
// actual delimiter characters for CSV format
colDelim = '","',
rowDelim = '"\r\n"',
// Grab text from table into CSV formatted string
csv = '"' + $rows.map(function (i, row) {
var $row = $(row),
$cols = $row.find('td');
return $cols.map(function (j, col) {
var $col = $(col),
text = $col.text();
return text.replace('"', '""'); // escape double quotes
}).get().join(tmpColDelim);
}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim) + '"',
// Data URI
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
$(this)
.attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
}
// This must be a hyperlink
$(".export").on('click', function (event) {
// CSV
exportTableToCSV.apply(this, [$('#dvData>table'), 'export.csv']);
// IF CSV, don't do event.preventDefault() or return false
// We actually need this to be a typical hyperlink
});
});
但不知何故,下载的文件在Chrome v35.0.1916.114中没有扩展名的download
,其中一种解决方法是将数据更改为:application / csv to data:text / csv,但这只会有助于获取下载文件中的扩展名正确,即现在下载为download.csv
。
仍然存在下载属性的问题。我想将我的文件命名为export.csv
,但它仍然会给我download.csv
。
答案 0 :(得分:29)
所以你应该改变这个:
// Data URI
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
$(this)
.attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
到此
// Data URI
//csvData = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv), //old way
blob = new Blob([csv], { type: 'text/csv' }); //new way
var csvUrl = URL.createObjectURL(blob);
$(this)
.attr({
'download': filename,
'href': csvUrl
});
它应该有效!
答案 1 :(得分:9)
这是问题#377860中报告的Chrome 35中的错误。
更新:此错误已合并到issue #373182。您可以点击星号告诉开发人员您认为此问题应该得到修复,并获得更改通知。
铬团队通常会每两周发布一次更新,因此您可以很快得到修复,因为它被标记为 Pri-1 ,我认为这意味着最高优先级。
答案 2 :(得分:2)
对我来说工作:
var content = "some content";
var link = document.createElement('a');
var blob = new Blob(["\ufeff", content]);
var url = URL.createObjectURL(blob);
link.href = url;
link.setAttribute('download', 'file.csv');
link.click();
答案 3 :(得分:1)
这对我有用(在Chrome和Firefox中)。我正在从表中构建一个xls文件。
function downloadInnerHtml(filename,elId,mimeType){
var elHtml='<table border="1">'+document.getElementById(elId).innerHTML+'</table>';
var link=document.createElement('a');
mimeType=mimeType || 'application/xls';
var blob=new Blob([elHtml],{type:mimeType});
var url=URL.createObjectURL(blob);
link.href=url;
link.setAttribute('download', filename);
link.innerHTML = "Export to CSV";
document.body.appendChild(link);
link.click();
}
$(document).on("click","#exportButton",function(){
var date=new Date();
var mm=date.getMonth()+1;
var dd=date.getDate();
var yy=date.getFullYear();
var timeStamp=yy+""+((mm<10)?"0"+mm:mm)+""+((dd<10)?"0"+dd:dd);
var fileName=timeStamp+'_Employees.xls';
downloadInnerHtml(fileName,'mainEmployeeTable','application/xls');
});
希望能帮助别人......
- 查
答案 4 :(得分:0)
当我尝试使用此代码时,我无法在IE中获得结果,此代码仅迭代td
,它将跳过表中存在的任何th
。我修改了代码以解决我遇到的两个问题:
$(document).ready(function () {
function exportTableToCSV($table, filename) {
var $rows = $table.find('tr'),
tmpColDelim = String.fromCharCode(11),
tmpHeadDelim = String.fromCharCode(11),
tmpRowDelim = String.fromCharCode(0),
colDelim = '","',
headDelim = '","',
rowDelim = '"\r\n"',
csv = '"' + $rows.map(function (i, row) {
var $row = $(row),
$cols = $row.find('td');
$heads = $row.find('th');
var c = $heads.map(function (k, head) {
var $head = $(head),
text = $head.text();
return text.replace(/"/g, '""');
}).get().join(tmpHeadDelim);
var d = $cols.map(function (j, col) {
var $col = $(col),
text = $col.text();
return text.replace(/"/g, '""');
}).get().join(tmpColDelim);
return (c+d);
}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpHeadDelim).join(headDelim)
.split(tmpColDelim).join(colDelim) + '"';
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
// if Internet Explorer (10+)
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
var blob = new Blob([decodeURIComponent(csv)], {
type: 'text/csv;charset=utf8'
});
window.navigator.msSaveBlob(blob, filename);
}
else {
var link = document.createElement('a');
var blob = new Blob([csv],{type:'text/csv;charset=utf8'});
var url = URL.createObjectURL(blob);
link.href = url;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
}
}
$("#fnExcelReport").on('click', function (event) {
var args = [$('#tableContent'), 'Report.csv'];
exportTableToCSV.apply(this, args);
});
});
答案 5 :(得分:-1)
使用字符串中的字符串添加标记信息附加锚标记然后追加,它适用于我的谷歌浏览器