Javascript 使用西班牙语字符将html表格导出到excel

Javascript 使用西班牙语字符将html表格导出到excel,javascript,php,jquery,excel,utf-8,Javascript,Php,Jquery,Excel,Utf 8,我在JQUERY的帮助下使用脚本将html表导出到excel中。问题是html页面包含一些带有重音的西班牙语字符,这些字符在Excel文件中没有正确编码。但是在html文件上,它们是好的 以下是我的html页面脚本: <!DOCTYPE html> <html lang="es"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Co

我在JQUERY的帮助下使用脚本将html表导出到excel中。问题是html页面包含一些带有重音的西班牙语字符,这些字符在Excel文件中没有正确编码。但是在html文件上,它们是好的

以下是我的html页面脚本:

<!DOCTYPE html>
    <html lang="es">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>

<form action="exporttoexcel.php" method="post" 
onsubmit='$("#datatodisplay").val( $("<div>").append( $("#ReportTable").eq(0).clone() ).html() )'>
  <table id="ReportTable" width="600" cellpadding="2" cellspacing="2" class="myClass">
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Country</th>
    </tr>
    <tr>
      <td><center>
          investigación
        </center></td>
      <td><center>
          investigación
        </center></td>
      <td><center>
          investigación
        </center></td>
    </tr>   
  </table>
  <table width="600px" cellpadding="2" cellspacing="2" border="0">
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="center"><input type="hidden" id="datatodisplay" name="datatodisplay">
        <input type="submit" value="Export to Excel">
      </td>
    </tr>
  </table>
  </form>

名字
姓
国家
调查科
调查科
调查科
php表单处理代码如下所示:

<?php
header("Pragma: no-cache");
header('Content-Encoding: UTF-8');
header('Content-Type: application/force-download; charset=utf-8');
header('Content-disposition: attachment; filename=export.xls');
// Fix for IE bug in download.
header("Pragma: ");
header("Cache-Control: ");
echo $_REQUEST['datatodisplay'];
?>


任何解决这种情况的帮助都是受欢迎的。谢谢

这篇文章有点老了,但我有你问题的答案,希望能为其他有类似问题的人服务

<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">

您可以注意到,在我的例子中,我使用了一个Java函数,我在

我的Java脚本函数如下

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))
        }
    })()
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))
}
})()
此方法在HTML代码中调用,如下所示:

<!-- /.panel-body -->
            <div class="panel-body" id="tablaFechaSalida">
                <input hidden="hidden" id="exporTabla2" type="button" class="btn btn-success" onclick="tableToExcel('tablaFechaSalida', 'Reporte Decretos')" value="Exportar a Excel">                   
                <table id="tabla2" class="table table-bordered">
                    <thead id="Cabecera2">
                        <tr>
                            <th colspan="8" align="center">DECRETO ALCALDICIO</th>
                        </tr>
                        <tr>
                            <th><div style="width: 100px">N° Decreto</div></th>
                            <th><div style="width: 80px">Año</div></th>
                            <th><div style="width: 140px">Ingreso</div></th>
                            <th><div style="width: 140px">Salida</div></th>
                            <th><div style="width: 500px">Proveedor</div></th>
                            <th><div style="width: 140px">Unidad</div></th>
                            <th><div style="width: 1000px">Observacion</div></th>
                            <th><div style="width: 80px">Tipo</div></th>
                        </tr>
                    </thead>
                    <tbody id="Cuerpo2"></tbody>
                </table>
                <!-- /.table-responsive -->
            </div>
            <!-- /.panel-body -->

阿尔卡迪西奥法令
N°法令
阿诺
英格里斯
萨利达
校长
尤尼达
观察
蒂波

为什么不在表单中包含标签:accept charset='utf-8'?相关:使用php代码,您可以用任何语言导出excel。如果你有任何意见,请在下面发表
header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-type:   application/x-msexcel; charset=utf-8");
header("Pragma: no-cache");
header('Content-Encoding: UTF-8');
header ("Content-Disposition: attachment; filename=\"$file_name" );
header ("Content-Description: Generated Report" );

if (mb_detect_encoding($content) == 'UTF-8') {
   $content = mb_convert_encoding($content , "HTML-ENTITIES", "UTF-8");
}

echo $content;