简体   繁体   English

将HTML表格导出为具有西班牙字符的Excel

[英]Export html table to excel with Spanish characters

I am using a script with the help of JQUERY to export html table into excel. 我在JQUERY的帮助下使用脚本将html表导出到excel。 The problem is that the html page contains some Spanish characters with accents which are not getting encoded correctly inside the Excel file. 问题是html页面包含一些带有重音符号的西班牙语字符,这些字符在Excel文件中未正确编码。 But on the html file they are fine. 但是在html文件上,它们很好。

Here is my html page script: 这是我的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>

and the php form processing code is the following: 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'];
?>

Any help resolving this situation is welcome. 欢迎解决此问题的任何帮助。 Thanks! 谢谢!

this post is somewhat old, but I have the answer to your question that I hope will serve others who have a similar problem. 这篇文章有些过时,但是我对您的问题有一个答案,希望可以为其他有类似问题的人提供帮助。

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

You can notice that in my case I used a Java function, and I mentioned it inside the 您可能会注意到,就我而言,我使用了Java函数,并且在

My Java Script function is as follows 我的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))
        }
    })()

This method is invoked in the HTML code as follows: 在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 -->
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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM