【发布时间】:2014-08-27 22:35:47
【问题描述】:
我有以下功能,通过使用这个我将我的 html 导出到 Csv 文件。几天/几个月前,它在 google chrome 浏览器中运行良好(在 FF 中仍然运行良好)。现在突然停止将数据转换为 csv 格式。
当我点击 export 按钮时,我可以下载文件,但是当我尝试在 ms excel/Libre office calc 中打开时,它并没有在其中打开。
我甚至可以看到导出的数据,但它的显示相同,分开。
谁能建议我在 google chrome 浏览器中的代码出了什么问题。
function exportReportTableToCSV($table, filename) {
var dumpd='';
var csvData ='';
$table.each(function(){
var $rows = $(this).find('tr:has(td)');
var $header = $(this).find('tr:has(th)');
tmpColDelim = String.fromCharCode(11), // vertical tab character
tmpRowDelim = String.fromCharCode(0), // null character
colDelim = '","',
rowDelim = '"\r\n"',
csv = '"' + $header.map(function (i, head) {
var $head = $(head),
$cols = $head.find('th');
return $cols.map(function (j, col) {
var $col = $(col),
text = $col.text();
if(text == " ")
text = "";
if(text == "PROGRAMS")
text = "";
console.log(text);
return text.replace('"', '""');
}).get().join(tmpColDelim);
}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim) + '"' ;
csv+='\r\n';
csv+= '"' + $rows.map(function (i, row) {
var $row = $(row),
$cols = $row.find('td');
return $cols.map(function (j, col) {
var $col = $(col);
var text;
if($($(col)).find("input:checkbox").length > 0)
text = $($(col)).find("input:checkbox").prop('checked') ? 'Yes' : 'No';
else
text = $col.text();
if(text === "")
{
text = "";
}
return text.replace('"', '""');
}).get().join(tmpColDelim);
}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim) + '"';
dumpd+=csv+"\n\n";
});
csvData ='data:application/csv;charset=utf-8,' + encodeURIComponent(dumpd);
$(this)
.attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
}
【问题讨论】:
标签: javascript php jquery google-chrome csv