phpExcel
Posted jackylee92
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了phpExcel相关的知识,希望对你有一定的参考价值。
1 include ‘phpExcel.php‘
include ‘PHPExcel/Writer/Excel2007.php‘;
2 实例化phpexcel对象:$objPHPExcel = new PHPExcel();
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
3 设置表格的参数
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/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header(‘Content-Disposition:attachment;filename="resume.xls"‘);//设置名字
header("Content-Transfer-Encoding:binary");
4 设置表格sheet页
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle(‘Simple‘);
5 设置第一行标题
$objPHPExcel->getActiveSheet()->setCellValue(‘A1‘, ‘ID‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘B1‘, ‘name‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘C1‘, ‘TEL‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘D1‘, ‘提交时间‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘E1‘, ‘来源‘);
宽度
$objPHPExcel->getActiveSheet()->getColumnDimension(‘A‘)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setWidth(12);
颜色
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFill()->getStartColor()->setARGB(‘FF808080‘);
$objPHPExcel->getActiveSheet()->getStyle(‘B1‘)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘B1‘)->getFill()->getStartColor()->setARGB(‘FF808080‘);
$objPHPExcel->getActiveSheet()->getStyle(‘C1‘)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘C1‘)->getFill()->getStartColor()->setARGB(‘FF808080‘);
$objPHPExcel->getActiveSheet()->getStyle(‘D1‘)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘D1‘)->getFill()->getStartColor()->setARGB(‘FF808080‘);
$objPHPExcel->getActiveSheet()->getStyle(‘E1‘)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘E1‘)->getFill()->getStartColor()->setARGB(‘FF808080‘);
循环插入
$num = 2;
foreach($data as $u){
$objPHPExcel->getActiveSheet()->setCellValue(‘A‘.$num, $u[‘id‘]);
$objPHPExcel->getActiveSheet()->setCellValue(‘B‘.$num, $u[‘name‘]);
$objPHPExcel->getActiveSheet()->setCellValue(‘C‘.$num, $u[‘phone‘]);
$objPHPExcel->getActiveSheet()->setCellValue(‘D‘.$num, date(‘y-m-d H:i:s‘,$u[‘create_time‘]));
$objPHPExcel->getActiveSheet()->setCellValue(‘E‘.$num, $type);
$num++;
}
导出
$objWriter->save(‘php://output‘);
以上是关于phpExcel的主要内容,如果未能解决你的问题,请参考以下文章