当前位置:首页 > PHP教程 > php应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-03 13:05:11 浏览: 评论:0 

这篇文章主要介绍了phpExcel中文帮助手册之常用功能指南以及示例分享,需要的朋友可以参考下。

PHPExcel基本操作:

定义EXCEL实体

即定义一个PHPEXCEL对象,并设置EXCEL对象内显示内容

  1. // Excel开始 
  2. // 准备EXCEL的包括文件 
  3. // Error reporting  
  4. error_reporting(0); 
  5. // PHPExcel  
  6. require_once dirname(__FILE__) . 'PHPExcel.php'
  7. // 生成新的excel对象 
  8. $objPHPExcel = new PHPExcel(); 
  9. // 设置excel文档的属性 
  10. $objPHPExcel->getProperties()->setCreator("Sam.c"
  11.              ->setLastModifiedBy("Sam.c Test"
  12.              ->setTitle("Microsoft Office Excel Document"
  13.              ->setSubject("Test"
  14.              ->setDescription("Test"
  15.              ->setKeywords("Test"
  16.              ->setCategory("Test result file"); 
  17. // 开始操作excel表 
  18. // 操作第一个工作表 
  19. $objPHPExcel->setActiveSheetIndex(0); 
  20. // 设置工作薄名称 
  21. $objPHPExcel->getActiveSheet()->setTitle(iconv('gbk''utf-8''phpexcel测试')); 
  22. // 设置默认字体和大小 
  23. $objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk''utf-8''宋体')); 
  24. $objPHPExcel->getDefaultStyle()->getFont()->setSize(10); 

三、输出文件

  1. // 如果需要输出EXCEL格式 
  2. if($m_exportType=="excel"){    
  3.     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel'Excel5'); 
  4.     // 从浏览器直接输出$filename 
  5.     header("Pragma: public"); 
  6.     header("Expires: 0"); 
  7.     header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); 
  8.     header("Content-Type:application/force-download"); 
  9.     header("Content-Type: application/vnd.ms-excel;"); 
  10.     header("Content-Type:application/octet-stream"); 
  11.     header("Content-Type:application/download"); 
  12.     header("Content-Disposition:attachment;filename=".$filename); 
  13.     header("Content-Transfer-Encoding:binary"); 
  14.     $objWriter->save("php://output");  
  15. // 如果需要输出PDF格式 
  16. if($m_exportType=="pdf"){ 
  17.     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel'PDF'); 
  18.     $objWriter->setSheetIndex(0); 
  19.     header("Pragma: public"); 
  20.     header("Expires: 0"); 
  21.     header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); 
  22.     header("Content-Type:application/force-download"); 
  23.     header("Content-Type: application/pdf"); 
  24.     header("Content-Type:application/octet-stream"); 
  25.     header("Content-Type:application/download"); 
  26.     header("Content-Disposition:attachment;filename=".$m_strOutputPdfFileName); 
  27.     header("Content-Transfer-Encoding:binary"); 
  28.     $objWriter->save("php://output");  

设置一列的宽度:

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

设置一行的高度:

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

合并单元格:

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

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

  1. $styleArray1 = array
  2.   'font' => array
  3.     'bold' => true, 
  4.     'size'=>12, 
  5.     'color'=>array
  6.       'argb' => '00000000'
  7.     ), 
  8.   ), 
  9.   'alignment' => array
  10.     'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 
  11.   ), 
  12. ); 
  13. // 将A1单元格设置为加粗,居中 
  14. $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1); 
  15.  
  16. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); 

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

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

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

  1. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  2.   $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');

给单元格中放入图片:

  1. // 将数据中心图片放在J1单元格内 
  2. $objDrawing = new PHPExcel_Worksheet_Drawing(); 
  3. $objDrawing->setName('Logo'); 
  4. $objDrawing->setDescription('Logo'); 
  5. $objDrawing->setPath('test.jpg'); 
  6. $objDrawing->setWidth(400); 
  7. $objDrawing->setHeight(123); 
  8. $objDrawing->setCoordinates('J1'); 
  9. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 

在单元格中设置超链接:

$objPHPExcel->getActiveSheet()->setCellValue('H8', iconv('gbk', 'utf-8', '燕南天'));

$objPHPExcel->getActiveSheet()->getCell('H8')->getHyperlink()->setUrl('https://www.phpfensi.com/');

设置单元格边框

  1. $styleThinBlackBorderOutline = array
  2.     'borders' => array ( 
  3.        'outline' => array ( 
  4.           'style' => PHPExcel_Style_Border::BORDER_THIN,  //设置border样式 
  5.           //'style' => PHPExcel_Style_Border::BORDER_THICK, 另一种样式 
  6.           'color' => array ('argb' => 'FF000000'),     //设置border颜色 
  7.       ), 
  8.    ), 
  9. ); 
  10. $objPHPExcel->getActiveSheet()->getStyle( 'A4:E10')->applyFromArray($styleThinBlackBorderOutline); 
  11.  
  12. //添加一个新的worksheet  
  13.           $objExcel->createSheet();  
  14.           $objActSheet = $objExcel->getSheet($s);  
  15.           $objActSheet->setTitle('表'.$GSheet); 

Tags: phpExcel常用功能

分享到: