我正在尝试创建一个将HTML表导出到Excel的JavaScript函数。下面的代码适用于Chrome,但在IE中,我收到一个弹出窗口,上面写着“没有安装应用程序来打开这种类型的链接(数据)。”我需要在客户端上进行导出并使用ActiveXObject不是一个选项。任何有关在IE和Chrome中使用此工作的帮助将不胜感激。
var uri = "data:application/vnd.ms-excel;base64,"
var template = "<!DOCTYPE html><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> " +
" <meta http-equiv=Content-Type content='text/html;charset=windows-1252'><meta name=Generator content='Microsoft Excel 11'> " +
" <meta name=ProgId content=Excel.Sheet> <meta name=Generator content='Microsoft Excel 11'> " +
"<style> " +
"<!--table @page{}-->" +
"</style>" +
"<!--[if gte mso 9]><xml> <x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet> <x:Name>Sheet1</x:Name> <x:WorksheetOptions><x:Panes> " +
"</x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets><x:ExcelWorkbook></xml><![endif]--> " +
"</head>" +
"<body><form method='POST' action='PRJEdetail-ClientExportTest.asp' name='frmExcel'></form>" +
tableHTML +
"</body>" +
"</html>"
var base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
window.location.href = uri + base64(template)