gpt4 book ai didi

javascript - 将 html 表导出为带有西类牙字符的 excel

转载 作者:行者123 更新时间:2023-11-30 15:36:22 24 4
gpt4 key购买 nike

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

关于javascript - 将 html 表导出为带有西类牙字符的 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41468764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com