php导出excel中文乱码解决办法总结
1、定义文件名
2、填充Excel数据
这两个过程中可能会出现一些PHP导出Excel乱码问题,下面我来说一下解决办法:
解决Excel内数据的PHP导出Excel乱码:
PHP导出Excel乱码原因:网页编码与Excel编码不一致。
解决办法:既然编码不一致,那让其一致了就OK了。定义Excel的字符集:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9921')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9921>header(“Content-Type: application/vnd.ms-excel; charset=UTF-8″),看到charset=UTF-8了吧,让其与您网页编码一致即可解决Excel内数据乱码的问题了,这个比较简单!
附一个php导出Excel类
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7942')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7942><?php
/*
* Created on 2012-2-26
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=php100.xls");
?>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<table>
<tr>
<td colspan="4"><font color="red" align="center">class infomation</font></td>
</tr>
<tr>
<td><?php echo iconv("UTF-8", "GBK", "编号"); ?></td>
<td><?php echo iconv("UTF-8", "GBK", "姓名"); ?></td>
<td><?php echo iconv("UTF-8", "GBK", "学号"); ?></td>
<td><?php echo iconv("UTF-8", "GBK", "性别"); ?></td>
</tr>
</table>
总结
导出之前需要转换为GBK
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8106')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8106>iconv("UTF-8", "GBK", $value);
其它方法
最后我采用phpMyAdmin的做法.用HTMLExcel, HTML我们比较熟悉,格式如下.
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1971')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1971><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">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<style id="Classeur1_16681_Styles"></style>
</head>
<body>
<div id="Classeur1_16681" align=center x:publishsource="Excel">
<table x:str border=0 cellpadding=0 cellspacing=0 width=100% style="border-collapse: collapse">
<tr><td class=xl2216681 nowrap>1234</td><td class=xl2216681 nowrap>Robbin会吐口水</td></tr>
<tr><td class=xl2216681 nowrap>5678</td><td class=xl2216681 nowrap>javaeye网站</td></tr>
</table>
</div>
</body>
</html>
这下可以直接echo了,又不需要iconv转码,只要设置好HTML里的Content-type(这里用的是UTF-8),是不是有舒服的感觉呢? 当然header还是要加上
Php代码
header("Content-Disposition:attachment;filename=export_data.xls");
如果是导出中文名字也有可能出现乱码
解决文件名的PHP导出Excel乱码:
乱码原因:客户使用的中文版Windows系统平台,而Windows平台的文件名编码为gb2312(gbk),而我们网页编码为了跟进现存潮流一般都采用utf-8(国际化)编码,这时当我们:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2677')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2677>header(“Content-Disposition: inline; filename=”" . $filename . “.xls”")时就会出现乱码,假如你的网页编码就是gb2312那就不用考虑编码问题了。
解决办法:
对$filename转码,执行:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6822')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6822>iconv(‘utf-8″, “gb2312″, $filename)。假如你的环境不支持iconv函数可以换别的函数,只要能将$filename的编码转为gbk就行。
但是这样问题又会来了,linux用户又会出现文件名乱码(因为linux平台文件名不是gbk编码)。
考虑到这个问题我采用两个办法:第一:放弃一部分客户,毕竟windows系统用户占绝大部分。第二:像gmail一样,提供两个下载地址。一个文件名gbk编码,一个文件名utf-8编码。
您可能感兴趣的文章: