1.实例化
$objPHPExcel = new PHPExcel(); //实例化PHPExcel类库,相当于新建一个Excel表 $objSheet = $objPHPExcel->getActiveSheet(); //获取当前活动sheet $objSheet->setTitle('sheet的名字'); //给当前的活动sheet起个名称
2.单元格相关
$objSheet->setCellValue('A1','XXXXXX'); //设置单元格的值 $objSheet->getStyle('A1')->getFont()->setName('宋体')->setSize(16)->setBold(true); //设置单元格的字体,字体大小,是否加粗 $objSheet->mergeCells('A1:G1'); //合并单元格 $objSheet->unmergeCells('A1:H1'); //拆分单元格 $objSheet->setCellValue('A1',"咨询有限公司\n联系人:王小姐\nTEL:8888888\n"); //单元格内容自动根据要求换行,必须要用双引号 $objSheet->getStyle('A1')->getAlignment()->setWrapText(true); //长度不够显示的时候 是否自动换行 $objSheet->getStyle('B')->getAlignment()->setWrapText(true); //长度不够显示的时候 是否自动换行 $objSheet->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中 $objSheet->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中 $objSheet->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); //向右居中 $objSheet->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); //向左居中 $objSheet->getStyle('A6:G6')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF808080'); //设置单元格背景颜色 $styleThinBlackBorderOutline = array( //设置单元格边框 'borders' => array( 'allborders' => array( //设置全部边框 'style' => \PHPExcel_Style_Border::BORDER_THIN, //粗的是thick 'color' => array ('argb' => 'FF000000'), ), ), );
3.页面设置
$objSheet->getStyle('A6:G6')->applyFromArray($styleThinBlackBorderOutline); //设置打印 页面 方向与大小(此为横向) $objSheet->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); //方向 $objSheet->getPageSetup()->setPaperSize(\PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); //大小
4.冻结标题
//第一种方式 $objSheet->freezePane('A1'); $objSheet->freezePane('A2'); //第二种方式 $objSheet->freezePaneByColumnAndRow(8,4);//(列,行)冻结的行数比你设置的要多一行(例 我需要设置的 是 3行 那么 参数就要写 4)
参考:https://blog.csdn.net/zyj_15067066062/article/details/81485781
标签:总结,objSheet,Style,单元格,A1,getStyle,设置,PHPExcel,phpexcel From: https://www.cnblogs.com/mianbaoshu/p/17075647.html