phpExcel中文帮助手册之常用功能指南

phpExcel基本操作:
定义EXCEL实体
即定义一个phpEXCEL对象,并设置EXCEL对象内显示内容

// Excel开始// 准备EXCEL的包括文件// Error reporting error_reporting(0);// phpExcel require_once dirname(__FILE__) . 'phpExcel.php';// 生成新的excel对象$objphpExcel = new phpExcel();// 设置excel文档的属性$objphpExcel->getProperties()->setCreator("Sam.c")             ->setLastModifiedBy("Sam.c Test")             ->setTitle("Microsoft Office Excel Document")             ->setSubject("Test")             ->setDescription("Test")             ->setKeywords("Test")             ->setCategory("Test result file");// 开始操作excel表// 操作第一个工作表$objphpExcel->setActiveSheetIndex(0);// 设置工作薄名称$objphpExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'phpexcel测试'));// 设置默认字体和大小$objphpExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', '宋体'));$objphpExcel->getDefaultStyle()->getFont()->setSize(10);

三、输出文件

// 如果需要输出EXCEL格式if($m_exportType=="excel"){       $objWriter = phpExcel_IOFactory::createWriter($objphpExcel, 'Excel5');    // 从浏览器直接输出$filename    header("Pragma: public");    header("Expires: 0");    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");    header("Content-Type:application/force-download");    header("Content-Type: application/vnd.ms-excel;");    header("Content-Type:application/octet-stream");    header("Content-Type:application/download");    header("Content-Disposition:attachment;filename=".$filename);    header("Content-Transfer-Encoding:binary");    $objWriter->save("php://output"); }// 如果需要输出PDF格式if($m_exportType=="pdf"){    $objWriter = phpExcel_IOFactory::createWriter($objphpExcel, 'PDF');    $objWriter->setSheetIndex(0);    header("Pragma: public");    header("Expires: 0");    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");    header("Content-Type:application/force-download");    header("Content-Type: application/pdf");    header("Content-Type:application/octet-stream");    header("Content-Type:application/download");    header("Content-Disposition:attachment;filename=".$m_strOutputPdfFileName);    header("Content-Transfer-Encoding:binary");    $objWriter->save("php://output"); }

设置一列的宽度:

$objphpExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);

设置一行的高度:

$objphpExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);

合并单元格:

$objphpExcel->getActiveSheet()->mergeCells('A1:P1');

设置A1单元格加粗,居中:

$styleArray1 = array(  'font' => array(    'bold' => true,    'size'=>12,    'color'=>array(      'argb' => '00000000',    ),  ),  'alignment' => array(    'horizontal' => phpExcel_Style_Alignment::HORIZONTAL_CENTER,  ),);// 将A1单元格设置为加粗,居中$objphpExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1);$objphpExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);

给特定单元格中写入内容:

$objphpExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby');

设置单元格样式(水平/垂直居中):
 

$objphpExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_CENTER);  $objphpExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(phpExcel_Style_Alignment::VERTICAL_CENTER);

设置单元格样式(黑色字体):

$objphpExcel->getActiveSheet()->getStyle('H5')->getFont()->getColor()->setARGB(phpExcel_Style_Color::COLOR_BLACK); // 黑色

设置单元格格式(背景):

$objphpExcel->getActiveSheet()->getStyle('H5')->getFill()->getStartColor()->setARGB('00ff99cc'); // 将背景设置为浅粉色

设置单元格格式(数字格式):

$objphpExcel->getActiveSheet()->getStyle('F'.$iLineNumber)->getNumberFormat()->setFormatCode('0.000');

给单元格中放入图片:

// 将数据中心图片放在J1单元格内$objDrawing = new phpExcel_Worksheet_Drawing();$objDrawing->setName('Logo');$objDrawing->setDescription('Logo');$objDrawing->setPath('test.jpg');$objDrawing->setWidth(400);$objDrawing->setHeight(123);$objDrawing->setCoordinates('J1');$objDrawing->setWorksheet($objphpExcel->getActiveSheet());


在单元格中设置超链接:

$objphpExcel->getActiveSheet()->setCellValue('H8', iconv('gbk', 'utf-8', '燕南天'));$objphpExcel->getActiveSheet()->getCell('H8')->getHyperlink()->setUrl('http://www.jb51.NET/');

设置单元格边框

$styleThinBlackBorderOutline = array(    'borders' => array (       'outline' => array (          'style' => phpExcel_Style_Border::BORDER_THIN,  //设置border样式          //'style' => phpExcel_Style_Border::BORDER_THICK, 另一种样式          'color' => array ('argb' => 'FF000000'),     //设置border颜色      ),   ),);$objphpExcel->getActiveSheet()->getStyle( 'A4:E10')->applyFromArray($styleThinBlackBorderOutline);//添加一个新的worksheet           $objExcel->createSheet();           $objActSheet = $objExcel->getSheet($s);           $objActSheet->setTitle('表'.$GSheet);

php技术phpExcel中文帮助手册之常用功能指南,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。