<?php
//定义头部
header("Content-Type: application/vnd.ms-excel;charset=gb2312");
//定义文件名
header("Content-Disposition:filename=test.xls");
//execl支持公式
$order_id='订单号';
$order_price=iconv('utf-8', 'gb2312', '订单金额');
$order_content=iconv('utf-8', 'gb2312', '订单内容');
?>
<table width="200" border="1">
<tr>
<td><strong>订单号</strong></td>
<td><strong>订单金额</strong></td>
<td><strong>订单内容</strong></td>
</tr>
<?php
for($i=0;$i<=100;$i++)
{
echo "<tr>";
echo " <td>".$i.";中文字符乱码</td>";
echo " <td>".$i.";中文字符乱码</td>";
echo " <td>".$i.";中文字符乱码</td>";
echo "</tr>";
}
?>
</tr>
</table>
//顺便贴一下乱码问题
Php导出Excel文件时有两个主要的过程:
1、定义文件名
2、填充Excel数据
这两个过程中可能会出现一些乱码问题,下面我来说一下解决办法:
解决文件名的乱码:
乱码原因:客户使用的中文版Windows系统平台,而Windows平台的文件名编码为gb2312(gbk),而我们网页编码为了跟进现存潮流一般都采用utf-8(国际化)编码,这时当我们:header(“Content-Disposition: inline; filename=/”" . $filename . “.xls/”")时就会出现乱码,假如你的网页编码就是gb2312那就不用考虑编码问题了。
解决办法:对$filename转码,执行:iconv(‘utf-8′, ‘gb2312′, $filename)。假如你的环境不支持iconv函数可以换别的函数,只要能将$filename的编码转为gbk就行。
但是这样问题又会来了,linux用户又会出现文件名乱码(因为linux平台文件名不是gbk编码)。
考虑到这个问题我采用两个办法:第一:放弃一部分客户,毕竟windows系统用户占绝大部分。第二:像gmail一样,提供两个下载地址。一个文件名gbk编码,一个文件名utf-8编码。
解决Excel内数据乱码:
乱码原因:网页编码与Excel编码不一致。
解决办法:既然编码不一致,那让其一致了就OK了。定义Excel的字符集:header(“Content-Type: application/vnd.ms-excel; charset=UTF-8″),看到charset=UTF-8了吧,让其与您网页编码一致即可解决Excel内数据乱码的问题了,这个比较简单!
二:
php导入到excel-支持utf8和gbk两种编码
php导入到excel乱码是因为utf8编码在xp系统不支持所有utf8编码转码一下就完美解决了
utf-8编码案例
<?php
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=11.xls ");
header("Content-Transfer-Encoding: binary ");
?>
<?
$filename="php导入到excel-utf-8编码";
$filename=iconv("utf-8", "gb2312", $filename);
echo $filename;
?>
gbk编码案例
<?php
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=11.xls ");
header("Content-Transfer-Encoding: binary ");
?>
<?
$filename="php导入到excel-utf-8编码";
echo $filename;
?>
访问网站的时候就下载到excel里面
要弄单元格区别的话
用table表格做网页的就可以了