JS直接將頁面的內容作爲excel下載

  做個筆記,後續自己可以看看,將頁面的一個Table直接輸出爲excel文件,親測有用。

 

 //下載excel
        function downloadExcel() {
            var uri = 'data:application/vnd.ms-excel;base64,';
            var 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">
                    <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">
                    <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
                    <x:Name>sheet1</x:Name>
                    <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
                    </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
                    </head><body><table>`+ $('#idData').html() + `</table></body></html>`;
            window.location.href = uri + window.btoa(unescape(encodeURIComponent(template)));
        };

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章