以上文章作为参考
下面是PHPExcel样式设置的代码:
<span style="font-size:24px;"><?php $dir = dirname(__FILE__); require $dir."/db.php"; require $dir."/PHPExcel.php"; $db = new db($phpexcel); $objPHPExcel = new PHPExcel(); for($i=0; $i<3; $i++){ if($i>0){ $objPHPExcel->createSheet(); } $objPHPExcel->setActiveSheetIndex($i); $objSheet = $objPHPExcel->getActiveSheet(); $objSheet->getColumnDimension('D')->setWidth(21); //设置列宽 $objSheet->getColumnDimension('E')->setWidth(16); $objSheet->getColumnDimension('F')->setWidth(21); $objSheet->getRowDimension('1')->setRowHeight(80); //设置行高 $objSheet->getRowDimension('2')->setRowHeight(29); $objSheet->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //设置水平垂直居中 $objSheet->getDefaultStyle()->getFont()->setName("微软雅黑")->setSize(12); //设置默认字体大小 $objSheet->getStyle("A1:F1")->getFont()->setSize(20)->setBold(true); //标题字体 $objSheet->getStyle('A1:F1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor()->setARGB('FFFF0000'); //设置标题背景颜色 $objSheet->getStyle("A1:F1")->applyFromArray(getBorderStyle("#66FF99")); //设置标题边框 $data = $db->getUserinfo(); $j = 1; $objSheet->setCellValue("A".$j,"****\n****"); $objSheet->getStyle('A1')->getAlignment()->setWrapText(true); //设置换行 $objSheet->mergeCells("A".$j.":F".$j); //合并单元格 $j++; $objSheet->setCellValue("A".$j,"编号")->setCellValue("B".$j,"登陆名") ->setCellValue("C".$j,"昵称")->setCellValue("D".$j,"电子邮箱") ->setCellValue("E".$j,"学校")->setCellValue("F".$j,"最后登陆时间") ->setCellValue("G".$j,"随机数"); $j++; foreach ($data as $key => $value) { # code... $objSheet->setCellValue("A".$j,$value['id'])->setCellValue("B".$j,$value['user_login']) ->setCellValue("C".$j,$value['user_nicename'])->setCellValue("D".$j,$value['user_email']) ->setCellValue("E".$j,$value['sch_name'])->setCellValue("F".$j,$value['last_login_time']) //显示数字的方法 1.指定为字符串 2.设置格式 //->setCellValueExplicit("G".$j,rand(1000000000,9999999999),PHPExcel_Cell_DataType::TYPE_STRING); ->setCellValue("G".$j,rand(100000000,999999999)); //下面以文本格式显示数字 $objSheet->getStyle('G'.$j)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); $j++; } } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel5"); // $objWriter->save($dir.'/export.xls'); //生成excel文件 browser_export("Excel5","browser_excel03.xls"); //浏览器输出 $objWriter->save("php://output"); function browser_export($type, $filename){ if($type == "Excel5"){ header('Content-Type: application/vnd.ms-excel'); //excel2003 }else{ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //excel2007 } header('Content-Disposition: attachment;filename="'.$filename.'"'); header('Cache-Control: max-age=0'); } /* *获得不同颜色的边框 */ function getBorderStyle($color){ $styleArray = array( 'borders' => array( 'outline' => array( 'style' => PHPExcel_Style_Border::BORDER_THICK, 'color' => array('rgb' => $color), ), ), ); return $styleArray; } </span>
PHPExcel样式函数可以参考编程文档
比如我们上面用到的数字格式显示:
Ctrl + 鼠标左键打开
代码都以写好,我们只要用就行了,还是很方便的,文档里面有所有的设置函数,需要的时候可以在里面查找。
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-11 01:16:13