phpExecl导出

首先说下,如果导出出现乱码,在确定代码没有问题的情况下,可以试试在header()前加

ob_end_clean();试一试, 还有可能是bom的问题,大家注意一下

require_once './source/class/phpexecl/PHPExcel.php';  
require_once './source/class/phpexecl/phpExcel/Writer/Excel2007.php';  
require_once './source/class/phpexecl/phpExcel/Writer/Excel5.php';  
include_once './source/class/phpexecl/phpExcel/IOFactory.php';

$objExcel = new PHPExcel();
//设置属性
$objExcel->getProperties()->setCreator("andy");
$objExcel->getProperties()->setLastModifiedBy("andy");
$objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
$objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
$objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
$objExcel->getProperties()->setKeywords("office 2003 openxml php");
$objExcel->getProperties()->setCategory("Test result file");
$objExcel->setActiveSheetIndex(0);

$i=0;
//表头
$k1="项目名称";
$k2="客户名称";
$k3="客户电话";
$k4="单元";
$k5="面积";
$k6="关注重点";
$k7="价格(元/平米)";
$k8="备注";
$k9="邮箱";
$k10="金额";
$k11="地址";
$k12="身份证号";
$k13="性别";
$k14="意向";
$k15="状态";
$k16="来源";
$k17="户型";
$k18="修改时间";
$k19="录入时间";
//ABCDEFGHIJKLMNOPQRSTUVWXYZ
$objExcel->getActiveSheet()->setCellValue('a1', "$k1");
$objExcel->getActiveSheet()->setCellValue('b1', "$k2");
$objExcel->getActiveSheet()->setCellValue('c1', "$k3");
$objExcel->getActiveSheet()->setCellValue('d1', "$k4");
$objExcel->getActiveSheet()->setCellValue('e1', "$k5");
$objExcel->getActiveSheet()->setCellValue('f1', "$k6");
$objExcel->getActiveSheet()->setCellValue('g1', "$k7");
$objExcel->getActiveSheet()->setCellValue('h1', "$k8");
$objExcel->getActiveSheet()->setCellValue('i1', "$k9");
$objExcel->getActiveSheet()->setCellValue('j1', "$k10");
$objExcel->getActiveSheet()->setCellValue('k1', "$k11");
$objExcel->getActiveSheet()->setCellValue('l1', "$k12");
$objExcel->getActiveSheet()->setCellValue('m1', "$k13");
$objExcel->getActiveSheet()->setCellValue('n1', "$k14");
$objExcel->getActiveSheet()->setCellValue('o1', "$k15");
$objExcel->getActiveSheet()->setCellValue('p1', "$k16");
$objExcel->getActiveSheet()->setCellValue('q1', "$k17");
$objExcel->getActiveSheet()->setCellValue('r1', "$k18");
$objExcel->getActiveSheet()->setCellValue('s1', "$k19");
$customer=DB::fetch_all("select * from db_c_customer where uid=$_G[uid]");
$customerlist=array();
foreach ($customer as $v){
$v['utime']=date('Y-m-d',$v['utime']);
$v['ctime']=$v['cyear']."-".$v['cmonth']."-".$v['cday'];
if ($v['gender']==1) {
$v['gender']='男';
}elseif($v['gender']==2){
$v['gender']='女';
}else{
$v['gender']='保密';
}
$customerlist[]=$v;
}
// 高置列的宽度
$objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
$objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
$objExcel->getActiveSheet()->getColumnDimension('I')->setWidth(18);
$objExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('K')->setWidth(35);
$objExcel->getActiveSheet()->getColumnDimension('L')->setWidth(25);
$objExcel->getActiveSheet()->getColumnDimension('M')->setWidth(10);
$objExcel->getActiveSheet()->getColumnDimension('N')->setWidth(25);
$objExcel->getActiveSheet()->getColumnDimension('O')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('P')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('R')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('S')->setWidth(15);

//写入内容
foreach($customerlist as $k=>$v) {
$u1=$i+2;

$objExcel->getActiveSheet()->setCellValue('a'.$u1, (string)$v["pid"]);
$objExcel->getActiveSheet()->setCellValue('b'.$u1, (string)$v["name"]);
$objExcel->getActiveSheet()->setCellValue('c'.$u1, (string)$v["tel"]);
$objExcel->getActiveSheet()->setCellValue('d'.$u1, (string)$v["unit"]);
$objExcel->getActiveSheet()->setCellValue('e'.$u1, (string)$v["rsize"]);
$objExcel->getActiveSheet()->setCellValue('f'.$u1, (string)$v["rfollow"]);
$objExcel->getActiveSheet()->setCellValue('g'.$u1, (string)$v["rprice"]);
$objExcel->getActiveSheet()->setCellValue('h'.$u1, (string)$v["remark"]);
$objExcel->getActiveSheet()->setCellValue('i'.$u1, (string)$v["email"]);
$objExcel->getActiveSheet()->setCellValue('j'.$u1, (string)$v["price"]);
$objExcel->getActiveSheet()->setCellValue('k'.$u1, (string)$v["address"]);
$objExcel->getActiveSheet()->setCellValue('l'.$u1, (string)$v["rcode"]);
$objExcel->getActiveSheet()->setCellValue('m'.$u1, (string)$v["gender"]);
$objExcel->getActiveSheet()->setCellValue('n'.$u1, (string)$v["rwill"]);
$objExcel->getActiveSheet()->setCellValue('o'.$u1, (string)$v["rstatu"]);
$objExcel->getActiveSheet()->setCellValue('p'.$u1, (string)$v["rfrom"]);
$objExcel->getActiveSheet()->setCellValue('q'.$u1, (string)$v["rtype"]);
$objExcel->getActiveSheet()->setCellValue('r'.$u1, (string)$v["utime"]);
$objExcel->getActiveSheet()->setCellValue('s'.$u1, (string)$v["ctime"]);
$i++;
}
$timestamp = time();
if($ex == '2007') { //导出excel2007文档
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="客户列表'.$timestamp.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
} else {  //导出excel2003文档
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="客户列表'.$timestamp.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter->save('php://output');
exit;
} 


你可能感兴趣的:(PHP)