Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将HTML表格导出到Excel时编码UTF-8_Javascript_Html_Excel_Encoding_Utf 8 - Fatal编程技术网

Javascript 将HTML表格导出到Excel时编码UTF-8

Javascript 将HTML表格导出到Excel时编码UTF-8,javascript,html,excel,encoding,utf-8,Javascript,Html,Excel,Encoding,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-co

我正在尝试使用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> 

var tableToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称){
如果(!table.nodeType)table=document.getElementById(table)
var ctx={工作表:名称| |'工作表',表:table.innerHTML}
window.location.href=uri+base64(格式(模板,ctx))
}
})()
这是我的头球

<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>

何塞
玛丽亚
这是触发导出的按钮

<input type="button" onclick="tableToExcel('tblExport', 'W3C Example Table')" value="Export to Excel">

我不能正确地导出UTF-8字符,比如é或í。我试过了,但不管用。我有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> 

var tableToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称){
如果(!table.nodeType)table=document.getElementById(table)
var ctx={工作表:名称| |'工作表',表:table.innerHTML}
window.location.href=uri+base64(格式(模板,ctx))
}
})()

将表数据作为纯文本并另存为Excel而不存在编码问题

在变量uri中使用以下代码:

var uri = 'data:application/vnd.ms-excel;charset=UTF-8;base64,'

我将再次引用上面提到的一个方面。您需要在head标记中包含meta标记代码:

<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在下载时存在一些冲突。
但是,特殊字符的问题已得到纠正

如果要更改默认文件名,请尝试以下操作:

var link = document.createElement('a');
link.download = 'filename.xls';
...
link.href = uri + base64(format(template, ctx));
link.click();
...

将window.location.href替换为link.href

您能分享您找到的简单解决方案吗?Tks!感谢分享简单解决方案:Dso简单解决方案是什么?有关于“解决方案更简单”的新闻吗^_^你的答案很好,你写的函数对我很有帮助+1我很难做到这一点,Blob上还有一个“编码”属性。但是,每个excel程序都会以不同的方式解释“html另存为.xls文件”,所以谁知道呢。
var link = document.createElement('a');
link.download = 'filename.xls';
...
link.href = uri + base64(format(template, ctx));
link.click();
...