记-纯前端excle下载
生活随笔
收集整理的這篇文章主要介紹了
记-纯前端excle下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
var fileName = '下載的文件名';
var label = [{'labelName1':'excle頭1'},{'labelName2':'excle頭2'},{'labelName3':'excle頭3'},{'labelName4':'excle頭4'}];
var data = [{'labelName1':'excle內容1'},{'labelName2':'excle內容2'},{'labelName3':'excle內容3'},{'labelName4':'excle內容4'}]
data = typeof data !== 'boject' ? JOSN.parse(data) : data;
var excle = '<table>';
#設置表頭
var row = '<tr align="left">';//設置表格中的樣式左居中
for(var i = 0; i < label.length; i++){
for(var key in label[i]){
row += '<td>'+label[i][key]+'</td>';
}
}
excle = row + '</tr>';
//設置數據
for(var i = 0; i < data.length; i++){
var rowData = '<tr align="left">';
for(var y =0; y < label.length; y++){
for(var k in label[y]){
if(label[y].hasOwnProperty(k)){
rowData += '<td style="vnd.ms-excel.numberformat:@">'+(data[i][k] === null ? "" : data[i][k]) + '</td>';//vnd.ms-excel.numberformat:@ 輸出為文本 }
}
}
excle += rowData + '</tr>';
}
excle += '</table>';
#excel文件的內容 var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>"; //以excle格式文件下載到本地 var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download =fileName + ".xls"; document.body.appendChild(link);
link.click();
document.body.removeChild(link);
var label = [{'labelName1':'excle頭1'},{'labelName2':'excle頭2'},{'labelName3':'excle頭3'},{'labelName4':'excle頭4'}];
var data = [{'labelName1':'excle內容1'},{'labelName2':'excle內容2'},{'labelName3':'excle內容3'},{'labelName4':'excle內容4'}]
data = typeof data !== 'boject' ? JOSN.parse(data) : data;
var excle = '<table>';
#設置表頭
var row = '<tr align="left">';//設置表格中的樣式左居中
for(var i = 0; i < label.length; i++){
for(var key in label[i]){
row += '<td>'+label[i][key]+'</td>';
}
}
excle = row + '</tr>';
//設置數據
for(var i = 0; i < data.length; i++){
var rowData = '<tr align="left">';
for(var y =0; y < label.length; y++){
for(var k in label[y]){
if(label[y].hasOwnProperty(k)){
rowData += '<td style="vnd.ms-excel.numberformat:@">'+(data[i][k] === null ? "" : data[i][k]) + '</td>';//vnd.ms-excel.numberformat:@ 輸出為文本 }
}
}
excle += rowData + '</tr>';
}
excle += '</table>';
#excel文件的內容 var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>"; //以excle格式文件下載到本地 var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download =fileName + ".xls"; document.body.appendChild(link);
link.click();
document.body.removeChild(link);
轉載于:https://www.cnblogs.com/leonchan/p/11496099.html
總結
以上是生活随笔為你收集整理的记-纯前端excle下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bios怎么光盘启动设置 如何设置bio
- 下一篇: hp笔记本怎么改u盘启动不了 HP笔记本