将HTML表格导出到Excel时编码UTF-8
我正在尝试使用JavaScript将HTML表格导出到Excel。 这是JavaScript代码
<script type="text/javascript"> var tableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,' , 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>{table}</table></body></html>' , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) } , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) } return function(table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} window.location.href = uri + base64(format(template, ctx)) } })() </script>
这是我的标题
<meta http-equiv="content-type" content="application/vnd.ms-excel;" charset="UTF-8"> <meta charset="UTF-8">
这是我的桌子
<table id="tblExport"> <tr> <td>José</td> <td>María</td> </tr> </table>
这是触发输出的button
<input type="button" onclick="tableToExcel('tblExport', 'W3C Example Table')" value="Export to Excel">
我无法正确导出UTF-8字符,如é或í。 我尝试这个导入到OO Calc的HTML表格作为UTF8而不转换为实体,但不工作。 我有MS-Excel 2010和Win7 64位。
我怎样才能正确导出UTF-8字符?
谢谢!
首先:您的标题格式不正确。 它应该是:
<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">
第二:它应该放入您的模板中,因为它包含Excel的字符集信息。
<script type="text/javascript"> var tableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,' , 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>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>' , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) } , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) } return function(table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} window.location.href = uri + base64(format(template, ctx)) } })() </script>
问候
阿克塞尔
function exportData(report_id){ var blob = new Blob([document.getElementById(report_id).innerHTML], { type: "text/plain;charset=utf-8;" }); saveAs(blob, "Report.xls");
将表格数据作为纯文本并保存为Excel,而不会出现编码问题
在var uri中使用下面的代码:
var uri = 'data:application/vnd.ms-excel;charset=UTF-8;base64,'
产量
我会再次引用上面指出的一个方面。 您需要在头标记中包含元标记代码:
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8" /> <!--This is what you should include--> <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"> <!----> <title>Ver Listado Pago</title> <link href="/Images/Decretos.ico" rel="shortcut icon" type="image/x-icon" /> <meta name="viewport" content="width=device-width" /> <link href="/Content/site.css" rel="stylesheet" /> <link href="~/Content/booostrap/bootstrap.min.css" rel="stylesheet" /> <link href="~/Content/booostrap/bootstrap-theme.css" rel="stylesheet" /> <link href="~/Content/booostrap/bootstrap-theme.min.css" rel="stylesheet" />
这对我有效。 但是由于xls扩展名,IE和WIN10下载有冲突。 但是,特殊字符的问题被纠正了