1、使用composer安装PHPExcel
composer require phpoffice/phpexcel
2、创建对象
$objPHPExcel = new \PHPExcel();
3、设置Excel属性
显示错误信息
error_reporting(E_ALL);
设置文件属性
$objPHPExcel ->getProperties() //获得文件属性对象,给下文提供设置资源 ->setCreator("test") //设置文件的创建者 ->setLastModifiedBy("test") //设置最后修改者 ->setTitle("test excel") //设置标题 ->setSubject("test theme") //设置主题 ->setDescription("备注内容") //设置备注 ->setKeywords("excel") //设置标记 ->setCategory("result file"); //设置类别
设置表头
$objPHPExcel->getActiveSheet()->setCellValue('A1', '表头1'); $objPHPExcel->getActiveSheet()->setCellValue('B1', '表头2'); $objPHPExcel->getActiveSheet()->setCellValue('C1', '表头3');
4、设置样式
设置合并、写入内容
$objPHPExcel->getActiveSheet() ->mergeCells('A1:C1') //合并 ->setCellValue('A1', '内容');
设置字体样式
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(12);//默认字体大小 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);//单元格字体大小 $objPHPExcel->getActiveSheet()->getStyle('A1:C1')->getFont()->setSize(12);//单元区间字体大小 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//是否加粗
设置单元格行高、列宽
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//设置默认行高 $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);//设置第一行行高 $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setWidth(20);//设置默认列宽 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);//设置第一列列宽 $objActSheet->getColumnDimension('A')->setAutoSize(true); //设置内容自适应
设置文本样式
//垂直居中 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); //水平居中 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //左对齐 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); //右对齐 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
5、写入内容
//当前活动工作表中写入内容 $objPHPExcel->getActiveSheet()->setCellValue('A1', '内容'); //指定第二个工作表写入内容 $objPHPExcel->setActiveSheetIndex(1) ->setCellValue('A1', '内容1') ->setCellValue('B1', '内容2') ->setCellValue('C1', '内容3');
6、设置工作表
//当前活动工作表名称 $objPHPExcel->getActiveSheet()->setTitle('数据统计-1'); //指定第二个工作表名称 $objPHPExcel->setActiveSheetIndex(1)->setTitle('数据统计-1'); //设置第二个表为活动表 $objPHPExcel->setActiveSheetIndex(1);
7、输出Excel表格
直接输出文件
$filename = '数据统计.xls'; //清空缓冲区并关闭输出缓冲 ob_end_clean(); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-excel"); header('Content-disposition:attachment;filename="'.$filename.'"'); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header("Pragma: no-cache"); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('PHP://output'); exit();
保存到本地
$objWriter =PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('myexchel.xlsx');
生成PDF文件
$objWriter =PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); $objWriter->save('a.pdf');
生成CSV文件
$objWriter =PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV') ->setDelimiter(',' ) //设置分隔符 ->setEnclosure('"' ) //设置包围符 ->setLineEnding("\r\n" )//设置行分隔符 ->setSheetIndex(0) //设置活动表 ->save('a.csv');
生成HTML文件
$objWriter =PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML'); //将$objPHPEcel对象转换成html格式的 $objWriter->setSheetIndex(0); //设置活动表 $objWriter->save('a.html'); //保存文件
标签:objPHPExcel,表格,A1,设置,objWriter,PHPExcel,PHP,getActiveSheet From: https://www.cnblogs.com/www-php/p/16895160.html