2017-01-04 8 views
1

Я использую скрипт с помощью JQUERY для экспорта таблицы html в excel. Проблема в том, что html-страница содержит некоторые испанские символы с акцентами, которые не получают правильную кодировку внутри файла Excel. Но в html-файле они прекрасны.Экспорт таблицы html для Excel с испанскими символами

Вот мой 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']; 
?> 

Любая помощь в решении этой ситуации можно только приветствовать. Благодаря!

+0

почему бы не включить в тег формы: accept-charset = 'utf-8'? – roetnig

+0

related: [Кодирование UTF-8 при экспорте HTML-таблицы в Excel] (http://stackoverflow.com/questions/25730008/encoding-utf-8-when-exporting-html-table-to-excel/25730640#25730640) – roetnig

ответ

0
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; 
+0

Используя php-код, вы можете экспортировать Excel с любого языка. Если у вас есть какие-либо комментарии, напишите ниже –

1

Этот пост несколько стар, но у меня есть ответ на ваш вопрос, который, я надеюсь, послужит другим, у кого есть аналогичная проблема.

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

Вы можете заметить, что в моем случае я использовал функцию Java, и я упомянул об этом в функции

My Java Script выглядит следующим образом

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)) 
     } 
    })() 

Этот метод вызывается в 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 --> 

 Смежные вопросы

  • Нет связанных вопросов^_^