我知道有很多方法可以从JavaScript(例如sheetjs等)下载/创建excel。我检查了所有与stackoverflow相关的问题,或者使用Activex(仅适用于IE),或者答案已经过时。但是,我仍然可以创建文件并下载它,但是当我尝试打开文件时,总是会出现以下错误:
如何更改格式和扩展名使其相同? /有办法绕过这些错误吗?
const data_type = 'data:application/vnd.ms-excel;base64,';
function createXLSFromJson() {
var xml = '<html xmlns:x="urn:schemas-microsoft-com:office:excel"><head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Error Messages</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';
xml += '<table><thead><tr>';
xml += '<td width="300" valign="top"><b>Location</b></td>'; // Location
xml += '<td width="180" valign="top"><b>Catchment area</b></td>'; // Catchment area
xml += '<td width="180" valign="top"><b>Towns</b></td>'; // Towns
xml += '<td width="180" valign="top"><b>Population</b></td>'; // Population
xml += '<td width="180" valign="top"><b>Average Income per inhabitant</b></td>'; // Average Income per inhabitant
xml += '</tr><tr>';
xml += '<td>Location</td><td>catchment time 1</td>';
xml += '</tr></thead></table></body></html>';
var link = document.createElement("a");
link.download = "export.xls";
link.href = data_type + btoa(xml);
link.click();
}
<button onclick="createXLSFromJson()">click here</button>