var str = "\uFEFF"+JSON2CSV(jsonobj);
var dataUri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(str);
var link = document.getElementById('link').href = dataUri;
<a href="#" id="link" download="download.csv">下載</a>
function JSON2CSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var line = '';
//if ($("#labels").is(':checked')) {
var head = array[0];
if ($("#quote").is(':checked')) {
for (var index in array[0]) {
var value = index + "";
line += '"' + value.replace(/"/g, '""') + '",';
}
} else {
for (var index in array[0]) {
line += index + ',';
}
}
line = line.slice(0, -1);
str += line + '\r\n';
//}
for (var i = 0; i < array.length; i++) {
var line = '';
if ($("#quote").is(':checked')) {
for (var index in array[i]) {
var value = array[i][index] + "";
line += '"' + value.replace(/"/g, '""') + '",';
}
} else {
for (var index in array[i]) {
if (typeof array[i][index] == 'object' && array[i][index] != null) {
line += "\"";
for (var j = 0; j < array[i][index].length; j++) line += array[i][index][j] + '\n';
line += "\",";
}
else line += array[i][index] + ',';
}
}
line = line.slice(0, -1);
str += line + '\r\n';
}
return str;
}
動態產生HTML 元件達到下載效果
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
訂閱:
張貼留言 (Atom)
vue3-simple-alert 學習心得
官網 顯示提示輸入訊息並於按下確定時檢查是否有輸入,防止未輸入就按確定,且和按取消用不同邏輯處理 VueSimpleAlert.fire({ title: '請輸入原因', input: 'text', showCancel...
-
1. 設定檔案下載儲存位置為 C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default\Cache 2. 勾選"下載每個檔案前詢問儲存位置" 3. 針對不要下載的檔案類型於第一...
-
自動設定欄寬 sheet.Cells.AutoFitColumns(3, 20); // 必須設定 min 跟 max 才會正常作用 凍結欄位 sheet.View.FreezePanes(4, 4); 標題列 ws.PrinterSettings.RepeatRo...
-
使用 FreeSpire.XLS ... ep.Save(); using (var workbook = new Workbook()) using (var memStream = new MemoryStream()) { workbook.LoadFromSt...
沒有留言:
張貼留言