PHPexcel的应用
PHPexcel 下载地址:http://phpexcel.codeplex.com/
/**
*PHP excel导出
* $data 数组
* $olumnOption excel 头标题
* $title excel标题</span>
* $columnWidth 每个小单元格的宽度
*/
public function excel($data,$columnOption,$title,$columnWidth){
$columnLen=count($columnOption);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("demo")
->setLastModifiedBy("ycsj")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$char="ABCDEFGHIJKLMNOPQSTUVWXYZ";
$optionObj=$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $title);
for ($i=0;$i<$columnLen;$i++){
$objPHPExcel->getActiveSheet()->getColumnDimension($char[$i])->setWidth($columnWidth);//设置宽度
$optionObj->setCellValue($char[$i].'2',$columnOption[$i] );
}
$dataLen=count($data);
$j=0;
foreach ($data as $key=>$value){
for ($i=0;$i<$columnLen;$i++){
$objPHPExcel->getActiveSheet(0)->setCellValue($char[$i] . ($j + 3), $value[$i]);
}
$j++;
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $title.date("Y-m-d",time()) . '.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}