PHPExcel simplest example tutorial

Source: Internet
Author: User
PHPExcel most simple example tutorial PHPExcel is quite powerful MS Office Excel document generation class library, when you need to output more complex format data, PHPExcel is a good choice. However, the method is relatively cumbersome. List to remember.

PHP code

  1. // Set the include path of the PHPExcel class library
  2. Set_include_path ('.'. PATH_SEPARATOR.
  3. 'D: \ Zeal \ PHP_LIBS '. PATH_SEPARATOR.
  4. Get_include_path ());
  5. /**
  6. * Open the comment of the corresponding row.
  7. * If Excel5 is used, the output content should be GBK encoded.
  8. */
  9. Require_once 'phpexcel. php ';
  10. // Uncomment
  11. // Or
  12. // Create a processing object instance
  13. $ ObjExcel = new PHPExcel ();
  14. // Create a file format to write the object instance, uncomment
  15. // Or
  16. // $ ObjWriter-> setOffice2003Compatibility (true );
  17. //*************************************
  18. // Set the basic attributes of the document
  19. $ ObjProps = $ objExcel-> getProperties ();
  20. $ ObjProps-> setCreator ("Zeal Li ");
  21. $ ObjProps-> setLastModifiedBy ("Zeal Li ");
  22. $ ObjProps-> setTitle ("Office XLS Test Document ");
  23. $ ObjProps-> setSubject ("Office XLS Test Document, Demo ");
  24. $ ObjProps-> setDescription ("Test document, generated by PHPExcel .");
  25. $ ObjProps-> setKeywords ("office excel PHPExcel ");
  26. $ ObjProps-> setCategory ("Test ");
  27. //*************************************
  28. // Set the current sheet index for subsequent content operations.
  29. // Display the call only when multiple sheets are used.
  30. // By default, PHPExcel will automatically create the first sheet and set SheetIndex = 0
  31. $ ObjExcel-> setActiveSheetIndex (0 );
  32. $ ObjActSheet = $ objExcel-> getActiveSheet ();
  33. // Set the name of the current active sheet
  34. $ ObjActSheet-> setTitle ('test Sheet ');
  35. //*************************************
  36. // Set the cell content
  37. //
  38. // PHPExcel automatically determines the cell content type based on the input content
  39. $ ObjActSheet-> setCellValue ('A1', 'string content'); // String content
  40. $ ObjActSheet-> setCellValue ('A2 ', 26); // value
  41. $ ObjActSheet-> setCellValue ('A3 ', true); // boolean value
  42. $ ObjActSheet-> setCellValue ('A4 ',' = SUM (A2: A2) '); // formula
  43. // Explicitly specify the content type
  44. $ ObjActSheet-> setCellValueExplicit ('a5 ', '123 ',
  45. PHPExcel_Cell_DataType: TYPE_STRING );
  46. // Merge cells
  47. $ ObjActSheet-> mergeCells ('b1: c22 ');
  48. // Separate cells
  49. $ ObjActSheet-> unmergeCells ('b1: c22 ');
  50. //*************************************
  51. // Set the cell style
  52. //
  53. // Set the width
  54. $ ObjActSheet-> getColumnDimension ('B')-> setAutoSize (true );
  55. $ ObjActSheet-> getColumnDimension ('A')-> setWidth (30 );
  56. $ ObjStyleA5 = $ objActSheet-> getStyle ('a5 ');
  57. // Set the numeric format of the cell content.
  58. //
  59. // If PHPExcel_Writer_Excel5 is used to generate the content,
  60. // Other types of custom formatting methods can be used normally, but when setFormatCode
  61. // When FORMAT_NUMBER is used, the actual effect is not set to "0 ". Yes
  62. // Line of code:
  63. // If ($ ifmt = '0') $ ifmt = 1;
  64. //
  65. // All are displayed based on the original content.
  66. $ ObjStyleA5
  67. -> GetNumberFormat ()
  68. -> SetFormatCode (PHPExcel_Style_NumberFormat: FORMAT_NUMBER );
  69. // Set the font
  70. $ ObjFontA5 = $ objStyleA5-> getFont ();
  71. $ ObjFontA5-> setName ('courier new ');
  72. $ ObjFontA5-> setSize (10 );
  73. $ ObjFontA5-> setBold (true );
  74. $ ObjFontA5-> setUnderline (PHPExcel_Style_Font: UNDERLINE_SINGLE );
  75. $ ObjFontA5-> getColor ()-> setARGB ('ff9999999999 ');
  76. // Set alignment
  77. $ ObjAlignA5 = $ objStyleA5-> getAlignment ();
  78. $ ObjAlignA5-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );
  79. $ ObjAlignA5-> setVertical (PHPExcel_Style_Alignment: VERTICAL_CENTER );
  80. // Set the border
  81. $ ObjBorderA5 = $ objStyleA5-> getBorders ();
  82. $ ObjBorderA5-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  83. $ ObjBorderA5-> getTop ()-> getColor ()-> setARGB ('ffff000000'); // color
  84. $ ObjBorderA5-> getBottom ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  85. $ ObjBorderA5-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  86. $ ObjBorderA5-> getRight ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  87. // Set the fill color
  88. $ ObjFillA5 = $ objStyleA5-> getFill ();
  89. $ ObjFillA5-> setFillType (PHPExcel_Style_Fill: FILL_SOLID );
  90. $ ObjFillA5-> getStartColor ()-> setARGB ('ffeeeeeee ');
  91. // Copy the style information from the specified cell.
  92. $ ObjActSheet-> duplicateStyle ($ objStyleA5, 'b1: C22 ');
  93. //*************************************
  94. // Add an image
  95. $ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
  96. $ ObjDrawing-> setName ('zealimg ');
  97. $ ObjDrawing-> setDescription ('image inserted by zeal ');
  98. $ ObjDrawing-> setPath ('./zeali.net.logo.gif ');
  99. $ ObjDrawing-> setHeight (36 );
  100. $ ObjDrawing-> setCoordinates ('c23 ');
  101. $ ObjDrawing-> setOffsetX (10 );
  102. $ ObjDrawing-> setRotation (15 );
  103. $ ObjDrawing-> getShadow ()-> setVisible (true );
  104. $ ObjDrawing-> getShadow ()-> setDirection (36 );
  105. $ ObjDrawing-> setWorksheet ($ objActSheet );
  106. // Add a new worksheet
  107. $ ObjExcel-> createSheet ();
  108. $ ObjExcel-> getSheet (1)-> setTitle ('test 2 ');
  109. // Protect cells
  110. $ ObjExcel-> getSheet (1)-> getProtection ()-> setSheet (true );
  111. $ ObjExcel-> getSheet (1)-> protectCells ('A1: c22', 'phpexcel ');
  112. //*************************************
  113. // Output Content
  114. //
  115. $ OutputFileName = "output.xls ";
  116. // To the file
  117. /// $ ObjWriter-> save ($ outputFileName );
  118. // Or
  119. // Go to the browser
  120. /// Header ("Content-Type: application/force-download ");
  121. /// Header ("Content-Type: application/octet-stream ");
  122. /// Header ("Content-Type: application/download ");
  123. /// Header ("Content-Transfer-Encoding: binary ");
  124. /// Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
  125. /// Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
  126. /// Header ("Pragma: no-cache ");
  127. //// $ ObjWriter-> save ('php: // output ');
  128. ?>

The following is an example of using PHPExcel to read an Excel file:

PHP code

  1. $ FileName = 'excel.xls ';
  2. $ Path = 'path ';
  3. $ FilePath = $ path. $ fileName;
  4. $ PHPExcel = new PHPExcel ();
  5. $ PHPReader = new PHPExcel_Reader_Excel2007 ();
  6. If (! $ PHPReader-> canRead ($ filePath )){
  7. $ PHPReader = new PHPExcel_Reader_Excel5 ();
  8. If (! $ PHPReader-> canRead ($ filePath )){
  9. Echo 'no Excel ';
  10. Return;
  11. }
  12. }
  13. $ PHPExcel = $ PHPReader-> load ($ filePath );
  14. $ CurrentSheet = $ PHPExcel-> getSheet (0 );
  15. /** Obtain the total number of columns */
  16. $ AllColumn = $ currentSheet-> getHighestColumn ();
  17. /** Obtain the total number of rows */
  18. $ AllRow = array ($ currentSheet-> getHighestRow ());
  19. For ($ currentRow = 1; $ currentRow <= $ allRow; $ currentRow ++ ){
  20. For ($ currentColumn = 'a'; $ currentColumn <= $ allColumn; $ currentColumn ++ ){
  21. $ Address = $ currentColumn. $ currentRow;
  22. Echo $ currentSheet-> getCell ($ address)-> getValue (). "\ t ";
  23. }
  24. Echo "\ n ";
  25. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.