前端頁面 json數據 直接前端頁面下載

order  爲後臺去的值List<Order> 集合值  OrderL裏面有3個字段, 分別循環取出,該下載睡覺了不能過大,少量下載可用
//錯誤下載
function errorExportExcel(order) {

    //列標題
    let str = '<tr><td>訂單編號</td><td>唯一標識</td><td>錯誤原因</td></tr>';
    //循環遍歷,每行加入tr標籤,每個單元格加td標籤
    for(let i = 0 ; i < order.length ; i++ ){
        str+='<tr>';
        for(let item in order[i]){
            //增加\t爲了不讓表格顯示科學計數法或者其他格式
            str+=`<td>${ order[i][item] + '\t'}</td>`;
        }
        str+='</tr>';
    }
    //Worksheet名
    let worksheet = 'Sheet1'
    let uri = 'data:application/vnd.ms-excel;base64,';

    //下載的表格模板數據
    let template = `<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">
      <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
        <x:Name>${worksheet}</x:Name>
        <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
        </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
        </head><body><table>${str}</table></body></html>`;
    //下載模板
    window.location.href = uri + base64(template)
}
//輸出base64編碼
function base64 (s) { return window.btoa(unescape(encodeURIComponent(s))) }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章