//header("Content-type: text/html; charset=utf-8");
//include_once 'PHPExcel.php';
/**
* 生成excel
* @param $list
*/
public function excel($list,$excelName='brand') {
// 创建新的PHPExcel对象
$objPHPExcel = new PHPExcel();
// 设置属性
$objPHPExcel->getProperties()->setCreator("ctos")
->setLastModifiedBy("ctos")
->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");
// 字体宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
// 设置行高度
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
// 设置水平居中
$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 表头
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '品牌')
->setCellValue('B1', '系列');
// 内容
$i = 2;
foreach($list as $key => $item ){
foreach($item as $k => $v ){
$objPHPExcel->getActiveSheet(0)->setCellValue('A'.$i, $key);
$objPHPExcel->getActiveSheet(0)->setCellValue('B'.$i, $v['goname'].'__'.$v['name']);
++$i;
}
}
// 重命名工作表
$objPHPExcel->getActiveSheet()->setTitle('车信息');
// 第一张工作表组活跃表索引,所以Excel打开这是第一个表
$objPHPExcel->setActiveSheetIndex(0);
// 输出
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $excelName . '.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
以上就介绍了php将数据写入excel,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。