如果你有在服務器端是如何工作的無法控制,這裏是我在另一個SO問題都提供了一個客戶端解決方案,爲OP的接受待定:Export to CSV using jQuery and html
有有一些限制或限制,你必須考慮,正如我在那裏的答案中提到的那樣,它有更多的細節。
這是相同的演示我已經提供: http://jsfiddle.net/terryyounghk/KPEGU/
而且給你什麼樣的劇本看起來像一個大概的瞭解。
你需要改變的是你如何迭代你的數據(在另一個問題的情況下,它是表格單元格)來構造一個有效的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
});
});
_so爲此問題提供解決方案_聽起來非常苛刻: – dbf
你好dbf,我不要求我的朋友只是尋求我的問題的解決方案。你能幫忙嗎? – Praveen
當您將數據發送到服務器時,應該在asp.net中處理它,併發送帶有MIME類型「text/csv」的響應頭 - 這將使其成爲可下載的電子表格 – 2013-04-18 13:02:55