如何修复从 iso8859-1 php 生成的文件到 Excel 程序的编码
我正在使用这段代码创建一个可从网页下载的 Excel 文件:
$sql = "SELECT * FROM my_table";
$ressource_sql = execute_sql($sql);
while ($row = mysql_fetch_assoc($ressource_sql)) {
$ligne_hve.=$row['id'] . ';';
$ligne_hve.=$row['name'] . ';';
$ligne_hve.=$row['products'] . ';';
$ligne_hve .= "\n";
}
$file_hve = fopen($file_export_hve, "w+");
fwrite($file_hve, $ligne_hve);
fclose($file_hve);
每次用户使用此代码查阅网页时都会重新生成 Excel 文件,所有网站都是 ISO8859-1,我知道 excel 使用的是奇怪的编码(windows-1252),但现在我没有找到任何技巧来将法国口音(在网站上很好地显示)输入到Excel中......
谢谢帮助
I'm creating an excel file downloadable from a web page with this piece of code:
$sql = "SELECT * FROM my_table";
$ressource_sql = execute_sql($sql);
while ($row = mysql_fetch_assoc($ressource_sql)) {
$ligne_hve.=$row['id'] . ';';
$ligne_hve.=$row['name'] . ';';
$ligne_hve.=$row['products'] . ';';
$ligne_hve .= "\n";
}
$file_hve = fopen($file_export_hve, "w+");
fwrite($file_hve, $ligne_hve);
fclose($file_hve);
The Excel file is regenerated each time the user consult the web page with this code, all the website is ISO8859-1,i know excel is using a strange encoding (windows-1252) but for now i didn' t find any tricks to get the french accent (well displayed on the website) into the excel...
Thx for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上并没有生成 Excel 文件。您的脚本使用分号生成 CSV 文件。要获得正确的解决方案,您应该查看 PHPExcel 或其他库。
但是您的简单 CSV 输出仍然可以转换为 UTF-8。建议添加 BOM,以便 Excel 在使用错误的内容类型发送时检测到它:
那里可能需要也可能不需要
utf8_encode()
。你没有展示任何具体的例子。如果文本已经是 UTF-8 格式,则 BOM 解决方法已经足够,不需要第二个 utf8_encode。You are not actually generating an Excel file. Your script generates a CSV file using semicolons. For a proper solution you should look into PHPExcel or another library.
But your simple CSV output can be converted into UTF-8 nevertheless. It's advisable to add a BOM so Excel detects it when it's sent with the wrong Content-Type:
The
utf8_encode()
may or may not be required there. You didn't show any concrete example. If the text was already in UTF-8, then the BOM workaround already suffices and a second utf8_encode is not necessary.输入字符集是 iso-8859-1。
The input charset is iso-8859-1.