合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[TOC] ### 一、单元格值设置 ~~~ //Add some data 添加数据 $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello');//可以指定位置 $objPHPExcel->getActiveSheet()->setCellValue('A2', true); $objPHPExcel->getActiveSheet()->setCellValue('A3', false); $objPHPExcel->getActiveSheet()->setCellValue('B2', 'world!'); $objPHPExcel->getActiveSheet()->setCellValue('B3', 2); $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Hello'); $objPHPExcel->getActiveSheet()->setCellValue('D2', 'world!'); //循环 for($i = 1;$i<200;$i++) { $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i); $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, 'Test value'); } //日期格式化 $objPHPExcel->getActiveSheet()->setCellValue('D1', time()); $objPHPExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH); //Add comment 添加注释 $objPHPExcel->getActiveSheet()->getComment('E11')->setAuthor('PHPExcel'); $objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun('PHPExcel:'); $objCommentRichText->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun("\r\n"); $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun('Total amount on the current invoice, excluding VAT.'); ~~~ ### 二、单元格格式设置 ~~~ //实例化对象 $objPHPExcel = new \PHPExcel(); //设置工作簿的名称 $objPHPExcel->getActiveSheet()->setTitle('预算经费表'); //合并单元格 $objPHPExcel->getActiveSheet()->mergeCells('A1:H1'); //拆分单元格 $objPHPExcel->getActiveSheet()->unmergeCells('A1:H1'); //设置单元格字体 $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体') //字体 ->setSize(12) //字体大小 ->setBold(true); //字体加粗 //设置行的高度 $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(31.5); $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(31.5); $objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(69.75); //长度不够显示的时候 是否自动换行 $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setWrapText(true); //自动转换显示字体大小,使内容能够显示 $objPHPExcel->getActiveSheet()->getStyle('B5')->getAlignment()->setShrinkToFit(true); //冻结标题 //第一种方式 //$objPHPExcel->getActiveSheet()->freezePane('A1'); //$objPHPExcel->getActiveSheet()->freezePane('A2'); //第二种方式 $objPHPExcel->getActiveSheet()->freezePaneByColumnAndRow(8,4);//(列,行)冻结的行数比你设置的要多一行(例 我需要设置的 是 3行 那么 参数就要写 4) //设置水平居中 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //垂直居中 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); //左对齐 $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); //右对齐 $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Align ~~~ ### 三、单元格边框设置 ~~~ //设置边框 $objPHPExcel->getActiveSheet()->getStyle('A1:H8')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN); ~~~ ### 四、单元格综合样式设置 ~~~ $style_Array=array( 'font' => array ( 'bold' => true ), 'alignment' => array ( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT , ), 'borders' => array ( 'top' => array ( 'style' => PHPExcel_Style_Border::BORDER_THIN ) ), 'fill' => array ( 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR ,//填充样式 'rotation' => 90,//填充颜色的方向 'startcolor' => array ( 'argb' => 'FFA0A0A0' //起始颜色 ), 'endcolor' => array ( 'argb' => 'FFFFFFFF' //结束颜色 ) ) ); $objPHPExcel->getActiveSheet()->getStyle( 'A3:E3')->applyFromArray( $style_Array ); ~~~ 最终样式结果为![](https://box.kancloud.cn/3f5e7397cf304e8845ec4b8ee5bb1bcf_306x55.PNG) 单色填充 ~~~ $fill_Array=array( 'fill' => array ( 'type'=>PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array ( 'argb' => 'FFD9E1F9' ), ); ~~~ ![](https://box.kancloud.cn/bb2a4684eea181bbd6f46c60fbba9b53_301x52.PNG)