教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php导出数据到excel文件 php导出excel乱码问题

php导出数据到excel文件 php导出excel乱码问题

发布时间:2017-01-23   编辑:jiaochengji.com
本文分享一个php导出数据到excel文件的例子,并成功解决了导出时的乱码问题,有需要的朋友参考下。

php导出excel文件的例子。
代码:
 

复制代码 代码示例:
<?php
/**
* 导出excel文件
* by www.jbxue.com
*/
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_add  from 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(); 
//20110629晚上测试通过。列名显示不出来?其它可以。
?>

您可能感兴趣的文章:
php框架laravel excel包使用教程介绍
php导出数据到excel文件 php导出excel乱码问题
php导出word文档与excel表格文件
PHP Spreadsheet_Excel_Reader导入excel中文显示乱码
php导出数据为execel文件的实例分享
PHP不使用开源类库导出mysql数据到Excel文件
php导出excel(不断刷新缓冲区)的实例代码
解决php下载excel无法打开的问题
php导出excel并解决乱码问题的方法介绍
php导出excel时科学计数法的处理方法

关键词: php导出excel  导出excel  乱码   
[关闭]
~ ~