php导出excel出现乱码,php导出数据到excel出现乱码的解决办法

代码如下: 代码示例:

/**

* 导出数据到excel 解决乱码问题

* Edit www.#

*/

function xlsBOF() {

echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);

return;

}

function xlsEOF() {

echo pack("ss", 0x0A, 0x00);

return;

}

function xlsWriteNumber($Row, $Col, $Value) {

echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);

echo pack("d", $Value);

return;

}

function xlsWriteLabel($Row, $Col, $Value ) {

$Value = iconv("UTF-8", "gb2312", $Value); //加上本语句,解决导出excel文件乱码问题20110629

$L = strlen($Value);

echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);

echo $Value;

return;

}

include "connection.php";

$sql = "select ledger_name,ledger_sex ,ledger_age ,ledger_addfrom ps_ledger_11";

$query = mysql_query($sql);

// 文件头

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-Type: application/vnd.ms-excel; charset=UTF-8");

header("Content-Disposition: attachment;filename=警务室辅助警力统计表.xls ");

//header("Content-Disposition: inline;filename=\"" . $filename . ".xls\"");

//iconv("utf-8", "gb2312", $filename);//解决文件引起的乱码".xls\"");

header("Content-Transfer-Encoding: binary ");

// 向表中添加数据

xlsBOF();

xlsWriteLabel(1,0,"列名");

xlsWriteLabel(1,1,"列名");

xlsWriteLabel(1,2,"列名");

xlsWriteLabel(1,3,"列名");

xlsWriteLabel(1,4,"列名");

$xlsRow = 1;

while($array = mysql_fetch_array($query)) {

++$i;

xlsWriteNumber($xlsRow,0,"$i");

xlsWriteNumber($xlsRow,0,"$array[0]");

xlsWriteLabel($xlsRow,1,"$array[1]");

xlsWriteLabel($xlsRow,2,"$array[2]");

xlsWriteLabel($xlsRow,3,"$array[3]");

xlsWriteLabel($xlsRow,4,"$array[4]");

$xlsRow++;

}

xlsEOF();

exit();

//测试能用,列名显示不出来,有待解决

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值