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

PHPExcel 中文使用手册详解

发布:smiling 来源: PHP粉丝网  添加日期:2018-09-14 11:01:57 浏览: 评论:0 

PHPExcel 是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格。整体来说是一个不错的Excel操作类。

下面是这个类的一些操作说明。

  1. include 'PHPExcel.php'
  2. include 'PHPExcel/Writer/Excel2007.php'
  3. //或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的创建一个excel  
  4. $objPHPExcel = new PHPExcel(); 
  5.  
  6. // 输出Excel表格到浏览器下载 
  7. header('Content-Type: application/vnd.ms-excel'); 
  8. header('Content-Disposition: attachment;filename="abc.xls"'); 
  9. header('Cache-Control: max-age=0'); 
  10. // If you're serving to IE 9, then the following may be needed 
  11. header('Cache-Control: max-age=1'); 
  12.  
  13. // If you're serving to IE over SSL, then the following may be needed 
  14. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
  15. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified 
  16. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
  17. header('Pragma: public'); // HTTP/1.0 
  18.  
  19. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 
  20. $objWriter->save('php://output'); 

设置excel的属性:

创建人

$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");

最后修改人

$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");

标题

$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");

题目

$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");

描述

$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");

关键字

$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");

种类

$objPHPExcel->getProperties()->setCategory("Test result file");

设置当前的sheet

$objPHPExcel->setActiveSheetIndex(0);

设置sheet的name

$objPHPExcel->getActiveSheet()->setTitle('Simple');

设置单元格的值

  1. $objPHPExcel->getActiveSheet()->setCellValue('A1''String'); 
  2. $objPHPExcel->getActiveSheet()->setCellValue('A2', 12); 
  3. $objPHPExcel->getActiveSheet()->setCellValue('A3', true); 
  4. $objPHPExcel->getActiveSheet()->setCellValue('C5''=SUM(C2:C4)'); 
  5. $objPHPExcel->getActiveSheet()->setCellValue('B8''=MIN(B2:C5)'); 

合并单元格

$objPHPExcel->getActiveSheet()->mergeCells('A18:E22');

分离单元格

$objPHPExcel->getActiveSheet()->unmergeCells('A28:B28');

冻结窗口

$objPHPExcel->getActiveSheet()->freezePane('A2');

保护cell

  1. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection! 
  2. $objPHPExcel->getActiveSheet()->protectCells('A3:E13''PHPExcel'); 

设置格式

  1. // Set cell number formats 
  2. echo date('H:i:s') . " Set cell number formats\n"
  3. $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); 
  4. $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' ); 

设置宽width

  1. // Set column widths 
  2. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); 
  3. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12); 

设置单元格高度

所有单元格默认高度

$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);

第一行的默认高度

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

设置font:

  1. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara'); 
  2. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20); 
  3. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); 
  4. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE); 
  5. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); 
  6. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); 
  7. $objPHPExcel->getActiveSheet()->getStyle('D13')->getFont()->setBold(true); 
  8. $objPHPExcel->getActiveSheet()->getStyle('E13')->getFont()->setBold(true); 

设置align:

  1. $objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
  2. $objPHPExcel->getActiveSheet()->getStyle('D12')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
  3. $objPHPExcel->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
  4. $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); 

垂直居中

$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

设置column的border

  1. $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  2. $objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  3. $objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  4. $objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  5. $objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 

设置border的color

  1. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300'); 
  2. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300'); 
  3. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); 
  4. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB('FF993300'); 
  5. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); 
  6. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300'); 

设置填充颜色

  1. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); 
  2. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080'); 
  3. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); 
  4. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('FF808080'); 

加图片

  1. /*实例化插入图片类*/ 
  2. $objDrawing = new PHPExcel_Worksheet_Drawing(); 
  3. /*设置图片路径 切记:只能是本地图片*/ 
  4. $objDrawing->setPath($img_val); 
  5. /*设置图片高度*/ 
  6. $objDrawing->setWidth(200); 
  7. $img_height[] = $objDrawing->getHeight(); 
  8. /*设置图片要插入的单元格*/ 
  9. $objDrawing->setCoordinates($img_k[$j].$i); 
  10. /*设置图片所在单元格的格式*/ 
  11. $objDrawing->setOffsetX(10); 
  12. $objDrawing->setOffsetY(10); 
  13. $objDrawing->setRotation(0); 
  14. $objDrawing->getShadow()->setVisible(true); 
  15. $objDrawing->getShadow()->setDirection(50); 
  16. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 

导出Excel表格例子:

  1. <?php 
  2.  
  3. $datas = array
  4.  array('王城''男''18''1997-03-13''18948348924'), 
  5.  array('李飞虹''男''21''1994-06-13''159481838924'), 
  6.  array('王芸''女''18''1997-03-13''18648313924'), 
  7.  array('郭瑞''男''17''1998-04-13''15543248924'), 
  8.  array('李晓霞''女''19''1996-06-13''18748348924'), 
  9. ); 
  10.  
  11. include 'PHPExcel.php'
  12. include 'PHPExcel/Writer/Excel2007.php'
  13. //或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的 
  14. // 创建一个excel 
  15. $objPHPExcel = new PHPExcel(); 
  16.  
  17. // Set document properties 
  18. $objPHPExcel->getProperties()->setCreator("Phpmarker")->setLastModifiedBy("Phpmarker")->setTitle("Phpmarker")->setSubject("Phpmarker")->setDescription("Phpmarker")->setKeywords("Phpmarker")->setCategory("Phpmarker"); 
  19.  
  20. $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1''名字')->setCellValue('B1''性别')->setCellValue('C1''年龄')->setCellValue('D1''出生日期')->setCellValue('E1''电话号码'); 
  21.    
  22. // Rename worksheet 
  23. $objPHPExcel->getActiveSheet()->setTitle('Phpmarker-' . date('Y-m-d')); 
  24.    
  25. // Set active sheet index to the first sheet, so Excel opens this as the first sheet 
  26. $objPHPExcel->setActiveSheetIndex(0); 
  27. $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15); 
  28. $objPHPExcel->getActiveSheet()->freezePane('A2'); 
  29. $i = 2; 
  30. foreach($datas as $data){ 
  31.  $objPHPExcel->getActiveSheet()->setCellValue('A' . $i$data[0])->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER); 
  32.  $objPHPExcel->getActiveSheet()->setCellValue('B' . $i$data[1]); 
  33.  $objPHPExcel->getActiveSheet()->setCellValue('C' . $i$data[2]); 
  34.  
  35.  $objPHPExcel->getActiveSheet()->setCellValueExplicit('D'$i$data[3],PHPExcel_Cell_DataType::TYPE_STRING); 
  36.  $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getNumberFormat()->setFormatCode("@"); 
  37.  
  38.  // 设置文本格式 
  39.  $objPHPExcel->getActiveSheet()->setCellValueExplicit('E'$i$data[4],PHPExcel_Cell_DataType::TYPE_STRING); 
  40.  $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setWrapText(true); 
  41.  $i ++; 
  42.  
  43. $objActSheet = $objPHPExcel->getActiveSheet(); 
  44.      
  45. // 设置CELL填充颜色 
  46. $cell_fill = array
  47.   'A1'
  48.   'B1'
  49.   'C1'
  50.   'D1'
  51.   'E1'
  52. ); 
  53. foreach($cell_fill as $cell_fill_val){ 
  54.  $cellstyle = $objActSheet->getStyle($cell_fill_val); 
  55.  // background 
  56.  // $cellstyle->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('fafa00'); 
  57.  // set align 
  58.  $cellstyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT)->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); 
  59.  // font 
  60.  $cellstyle->getFont()->setSize(12)->setBold(true); 
  61.  // border 
  62.  $cellstyle->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000'); 
  63.  $cellstyle->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000'); 
  64.  $cellstyle->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000'); 
  65.  $cellstyle->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000'); 
  66.  
  67. $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); 
  68.  
  69. $objActSheet->getColumnDimension('A')->setWidth(18.5); 
  70. $objActSheet->getColumnDimension('B')->setWidth(23.5); 
  71. $objActSheet->getColumnDimension('C')->setWidth(12); 
  72. $objActSheet->getColumnDimension('D')->setWidth(12); 
  73. $objActSheet->getColumnDimension('E')->setWidth(12); 
  74.  
  75. $filename = '2015030423'
  76. ob_end_clean();//清除缓冲区,避免乱码  
  77. header('Content-Type: application/vnd.ms-excel'); 
  78. header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); 
  79. header('Cache-Control: max-age=0'); 
  80. // If you're serving to IE 9, then the following may be needed 
  81. header('Cache-Control: max-age=1'); 
  82. //phpfensi.com 
  83. // If you're serving to IE over SSL, then the following may be needed 
  84. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
  85. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified 
  86. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
  87. header('Pragma: public'); // HTTP/1.0 
  88.  
  89. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 
  90. $objWriter->save('php://output'); 
  91. ?> 

到处中文时,会出现乱码,记得在Header 之前加 ob_end_clean() 清除缓冲区,避免乱码.

Tags: PHPExcel PHPExcel手册

分享到: