我發現了幾個可幫助我生成CSV文件的代碼。但我不能將它用的.csv將網格數據導出爲CSV文件,並使用擴展名.csv將其保存爲僅使用Javascript/JQuery
我使用下面的代碼來生成CSV文件的擴展名保存,但我需要的文件,保存的.csv作爲擴展
var ExportToCSV= function (gridData, fileName, humanize, ignore){
var csv = '',
data = [],
ignore = [];
if (!ignore) {
ignore = [];
}
//ignore added datasource properties
var commonIgnore = ["_events", "idField", "_defaultId", "constructor", "init", "get",
"_set", "wrap", "bind", "one", "first", "trigger",
"unbind", "uid", "dirty", "parent"];
ignore = $.merge($.merge([], ignore), commonIgnore);
var data= gridData._data;
//add the header row
if (data.length > 0) {
for (var col in data[0]) {
//do not include inherited properties
if (!data[0].hasOwnProperty(col) || ($.inArray(col, ignore) > -1)) {
continue;
}
if (humanize) {
col = col.split('_').join(' ').replace(/([A-Z])/g, ' $1');
}
col = col.replace(/"/g, '""');
csv += '"' + col + '"';
if (col != data[0].length - 1) {
csv += ",";
}
}
csv += "\n";
}
//add each row of data
for (var row in data) {
for (var col in data[row]) {
//do not include inherited properties
if (!data[row].hasOwnProperty(col) || ($.inArray(col, ignore) > -1)) {
continue;
}
var value = data[row][col];
if (value === null) {
value = "";
} else if (value instanceof Date) {
value = kendo.toString(kendo.parseDate(value),"dd/MM/yyyy");
} else {
value = value.toString();
}
value = value.replace(/"/g, '""');
csv += '"' + value + '"';
if (col != data[row].length - 1) {
csv += ",";
}
}
csv += "\n";
}
//TODO replace with downloadify so we can get proper file naming
window.open("data:application/csv;charset=utf-8," + escape(csv));
} ;
使用php,asp.net,java等服務器端代碼來解決問題的方法有很多。但任何人都可以通過使用客戶端腳本來實現這一點,即使用Javascript/JQuery。
非常感謝您的幫助。