Home  >  Article  >  Backend Development  >  Sharing how to use phpExcel class

Sharing how to use phpExcel class

WBOY
WBOYOriginal
2016-07-25 08:56:031104browse
  1. include 'PHPExcel.php';
  2. include 'PHPExcel/Writer/Excel2007.php';
  3. //or include 'PHPExcel/Writer/Excel5.php'; for outputting .xls
  4. Create an excel
  5. $objPHPExcel = new PHPExcel();
  6. Save excel—2007 format
  7. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  8. //or $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); non-2007 format
  9. $objWriter- >save( "xxx.xlsx");
Copy the code

2 and output it directly to the browser

  1. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

  2. header("Pragma: public");
  3. header("Expires: 0″);
  4. header("Cache-Control:must -revalidate, post-check=0, pre-check=0″);
  5. header("Content-Type:application/force-download");
  6. header("Content-Type:application/vnd.ms-execl") ;
  7. header("Content-Type:application/octet-stream"); ​​
  8. header("Content-Type:application/download");;
  9. header('Content-Disposition:attachment;filename="resume.xls"' );
  10. header("Content-Transfer-Encoding:binary");
  11. $objWriter->save('php://output');

  12. 3, set excel attributes:

  13. Created by
  14. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
  15. Last modified by
  16. $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
  17. Title
  18. $objPHPExcel->getProperties()->setTitle("office 2007 XLSX Test Document");
  19. Title
  20. $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
  21. Description
  22. $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
  23. Keywords
  24. $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
  25. Category
  26. $objPHPExcel->getProperties()->setCategory("Test result file");

Copy code

4, other

  1. Set the current sheet
  2. $objPHPExcel->setActiveSheetIndex(0);
  3. Set the name of the sheet
  4. $objPHPExcel->getActiveSheet()->setTitle('Simple');
  5. Set the value of the cell
  6. $objPHPExcel->getActiveSheet()->setCellValue('A1′, 'String');
  7. $objPHPExcel->getActiveSheet()->setCellValue('A2′, 12);
  8. $objPHPExcel-> getActiveSheet()->setCellValue('A3′, true);
  9. $objPHPExcel->getActiveSheet()->setCellValue('C5′, '=SUM(C2:C4)');
  10. $objPHPExcel-> getActiveSheet()->setCellValue('B8′, '=MIN(B2:C5)');
  11. Merge cells
  12. $objPHPExcel->getActiveSheet()->mergeCells('A18:E22′);
  13. Separate Cell
  14. $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28′);
  15. Protect cell
  16. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); / / Needs to be set to true in order to enable any worksheet protection!
  17. $objPHPExcel->getActiveSheet()->protectCells('A3:E13′, 'PHPExcel');
  18. Set format
  19. // Set cell number formats
  20. echo date('H:i:s') . " Set cell number formatsn";
  21. $objPHPExcel->getActiveSheet()->getStyle('E4′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat ::FORMAT_CURRENCY_EUR_SIMPLE);
  22. $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4′), 'E5:E13′ );
  23. Set the width
  24. / / Set column widths
  25. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  26. $objPHPExcel->getActiveSheet()->getColumnDimension('D')-> ;setWidth(12);
  27. Set font
  28. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setName('Candara');
  29. $objPHPExcel->getActiveSheet ()->getStyle('B1′)->getFont()->setSize(20);
  30. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()- >setBold(true);
  31. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  32. $objPHPExcel->getActiveSheet( )->getStyle('B1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
  33. $objPHPExcel->getActiveSheet()->getStyle('E1′ )->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
  34. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getFont()-> ;setBold(true);
  35. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getFont()->setBold(true);
  36. Set align
  37. $objPHPExcel->getActiveSheet() ->getStyle('D11′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  38. $objPHPExcel->getActiveSheet()->getStyle('D12′)->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  39. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  40. $objPHPExcel- >getActiveSheet()->getStyle('A18′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  41. //Vertical centering
  42. $objPHPExcel->getActiveSheet()->getStyle( 'A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  43. Set the column's border
  44. $objPHPExcel->getActiveSheet()->getStyle('A4′)->getBorders() ->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  45. $objPHPExcel->getActiveSheet()->getStyle('B4′)->getBorders()->getTop()-> setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  46. $objPHPExcel->getActiveSheet()->getStyle('C4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  47. $objPHPExcel->getActiveSheet()->getStyle('D4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  48. $objPHPExcel->getActiveSheet()- >getStyle('E4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  49. Set the border color
  50. $objPHPExcel->getActiveSheet()->getStyle( 'D13′)->getBorders()->getLeft()->getColor()->setARGB('FF993300′);
  51. $objPHPExcel->getActiveSheet()->getStyle('D13′) ->getBorders()->getTop()->getColor()->setARGB('FF993300′);
  52. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders ()->getBottom()->getColor()->setARGB('FF993300′);
  53. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getTop()->getColor()->setARGB('FF993300′);
  54. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getBottom()->getColor()->setARGB('FF993300′);
  55. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getRight()->getColor()->setARGB('FF993300′);
  56. 设置填充颜色
  57. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  58. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->getStartColor()->setARGB('FF808080′);
  59. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  60. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFill()->getStartColor()->setARGB('FF808080′);
  61. 加图片 //bbs.it-home.org
  62. $objDrawing = new PHPExcel_Worksheet_Drawing();
  63. $objDrawing->setName('Logo');
  64. $objDrawing->setDescription('Logo');
  65. $objDrawing->setPath('./images/officelogo.jpg');
  66. $objDrawing->setHeight(36);
  67. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  68. $objDrawing = new PHPExcel_Worksheet_Drawing();
  69. $objDrawing->setName('Paid');
  70. $objDrawing->setDescription('Paid');
  71. $objDrawing->setPath('./images/paid.png');
  72. $objDrawing->setCoordinates('B15′);
  73. $objDrawing->setOffsetX(110);
  74. $objDrawing->setRotation(25);
  75. $objDrawing->getShadow()->setVisible(true);
  76. $objDrawing->getShadow()->setDirection(45);
  77. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  78. 在默认sheet后,创建一个worksheet
  79. echo date('H:i:s') . " Create new Worksheet objectn";
  80. $objPHPExcel->createSheet();
  81. $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
  82. $objWriter-save('php://output');
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn