Examples of common PHPExcel methods

Source: Internet
Author: User
Examples of common PHPExcel methods

  1. /**

  2. * Phpexcel usage example
  3. * By bbs.it-home.org
  4. *################
  5. */
  6. // Set the include path of the PHPExcel class library
  7. Set_include_path ('.'. PATH_SEPARATOR.
  8. 'D: \ Zeal \ PHP_LIBS '. PATH_SEPARATOR.
  9. Get_include_path ());
  10. /**
  11. * The following is an example. the optional methods are different for rows starting //.
  12. * Open the comment of the corresponding row.
  13. * If Excel5 is used, the output content should be GBK encoded.
  14. */
  15. Require_once 'phpexcel. php ';
  16. // Uncomment
  17. /// Require_once 'phpexcel/Writer/excel5.php'; // used for other earlier versions of xls
  18. // Or
  19. /// Require_once 'phpexcel/Writer/excel2007.php'; // used for excel-2007 format
  20. // Create a processing object instance
  21. $ ObjExcel = new PHPExcel ();
  22. // Create a file format to write the object instance, uncomment
  23. //// $ ObjWriter = new PHPExcel_Writer_Excel5 ($ objExcel); // used for other version formats
  24. // Or
  25. //// $ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objExcel); // used in 2007 format
  26. // $ ObjWriter-> setOffice2003Compatibility (true );
  27. // Set the basic attributes of the document
  28. $ ObjProps = $ objExcel-> getProperties ();
  29. $ ObjProps-> setCreator ("Zeal Li ");
  30. $ ObjProps-> setLastModifiedBy ("Zeal Li ");
  31. $ ObjProps-> setTitle ("Office XLS Test Document ");
  32. $ ObjProps-> setSubject ("Office XLS Test Document, Demo ");
  33. $ ObjProps-> setDescription ("Test document, generated by PHPExcel .");
  34. $ ObjProps-> setKeywords ("office excel PHPExcel ");
  35. $ ObjProps-> setCategory ("Test ");
  36. // Set the current sheet index for subsequent content operations.
  37. // Display the call only when multiple sheets are used.
  38. // By default, PHPExcel will automatically create the first sheet and set SheetIndex = 0
  39. $ ObjExcel-> setActiveSheetIndex (0 );
  40. $ ObjActSheet = $ objExcel-> getActiveSheet ();
  41. // Set the name of the current active sheet
  42. $ ObjActSheet-> setTitle ('test Sheet ');
  43. // Set the cell content
  44. //
  45. // PHPExcel automatically determines the cell content type based on the input content
  46. $ ObjActSheet-> setCellValue ('A1', 'string content'); // String content
  47. $ ObjActSheet-> setCellValue ('A2 ', 26); // value
  48. $ ObjActSheet-> setCellValue ('A3 ', true); // boolean value
  49. $ ObjActSheet-> setCellValue ('A4 ',' = SUM (A2: A2) '); // formula
  50. // Explicitly specify the content type
  51. $ ObjActSheet-> setCellValueExplicit ('a5 ', '123 ',
  52. PHPExcel_Cell_DataType: TYPE_STRING );
  53. // Merge cells
  54. $ ObjActSheet-> mergeCells ('b1: c22 ');
  55. // Separate cells
  56. $ ObjActSheet-> unmergeCells ('b1: c22 ');
  57. // Set the cell style

  58. // Set the width

  59. $ ObjActSheet-> getColumnDimension ('B')-> setAutoSize (true );
  60. $ ObjActSheet-> getColumnDimension ('A')-> setWidth (30 );
  61. $ ObjStyleA5 = $ objActSheet-> getStyle ('a5 ');
  62. // Set the numeric format of the cell content.
  63. //
  64. // If PHPExcel_Writer_Excel5 is used to generate the content,
  65. // Note that the const variable defined in the PHPExcel_Style_NumberFormat class
  66. // Other types of custom formatting methods can be used normally, but when setFormatCode
  67. // When FORMAT_NUMBER is used, the actual effect is not set to "0 ". Yes
  68. // Modify the getXf ($ style) method in the source code of the PHPExcel_Writer_Excel5_Format class,
  69. // Add one before if ($ this-> _ BIFF_version = 0x0500) {(near row 363rd)
  70. // Line of code:
  71. // If ($ ifmt = '0') $ ifmt = 1;
  72. //
  73. // Set the format to PHPExcel_Style_NumberFormat: FORMAT_NUMBER to avoid large numbers.
  74. // The data is displayed in scientific notation. the following setAutoSize method can be used to display the content of each row.
  75. // All are displayed based on the original content.
  76. $ ObjStyleA5
  77. -> GetNumberFormat ()
  78. -> SetFormatCode (PHPExcel_Style_NumberFormat: FORMAT_NUMBER );
  79. // Set the font
  80. $ ObjFontA5 = $ objStyleA5-> getFont ();
  81. $ ObjFontA5-> setName ('courier new ');
  82. $ ObjFontA5-> setSize (10 );
  83. $ ObjFontA5-> setBold (true );
  84. $ ObjFontA5-> setUnderline (PHPExcel_Style_Font: UNDERLINE_SINGLE );
  85. $ ObjFontA5-> getColor ()-> setARGB ('ff9999999999 ');
  86. // Set alignment
  87. $ ObjAlignA5 = $ objStyleA5-> getAlignment ();
  88. $ ObjAlignA5-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );
  89. $ ObjAlignA5-> setVertical (PHPExcel_Style_Alignment: VERTICAL_CENTER );
  90. // Set the border
  91. $ ObjBorderA5 = $ objStyleA5-> getBorders ();
  92. $ ObjBorderA5-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  93. $ ObjBorderA5-> getTop ()-> getColor ()-> setARGB ('ffff000000'); // color
  94. $ ObjBorderA5-> getBottom ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  95. $ ObjBorderA5-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  96. $ ObjBorderA5-> getRight ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  97. // Set the fill color
  98. $ ObjFillA5 = $ objStyleA5-> getFill ();
  99. $ ObjFillA5-> setFillType (PHPExcel_Style_Fill: FILL_SOLID );
  100. $ ObjFillA5-> getStartColor ()-> setARGB ('ffeeeeeee ');
  101. // Copy the style information from the specified cell.
  102. $ ObjActSheet-> duplicateStyle ($ objStyleA5, 'b1: C22 ');
  103. // Add an image
  104. $ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
  105. $ ObjDrawing-> setName ('zealimg ');
  106. $ ObjDrawing-> setDescription ('image inserted by zeal ');
  107. $ ObjDrawing-> setPath ('./zeali.net.logo.gif ');
  108. $ ObjDrawing-> setHeight (36 );
  109. $ ObjDrawing-> setCoordinates ('c23 ');
  110. $ ObjDrawing-> setOffsetX (10 );
  111. $ ObjDrawing-> setRotation (15 );
  112. $ ObjDrawing-> getShadow ()-> setVisible (true );
  113. $ ObjDrawing-> getShadow ()-> setDirection (36 );
  114. $ ObjDrawing-> setWorksheet ($ objActSheet );
  115. // Add a new worksheet
  116. $ ObjExcel-> createSheet ();
  117. $ ObjExcel-> getSheet (1)-> setTitle ('test 2 ');
  118. // Protect cells
  119. $ ObjExcel-> getSheet (1)-> getProtection ()-> setSheet (true );
  120. $ ObjExcel-> getSheet (1)-> protectCells ('A1: c22', 'phpexcel ');
  121. // Output Content
  122. //
  123. $ OutputFileName = "output.xls ";
  124. // To the file
  125. /// $ ObjWriter-> save ($ outputFileName );
  126. // Or
  127. // Go to the browser
  128. /// Header ("Content-Type: application/force-download ");
  129. /// Header ("Content-Type: application/octet-stream ");
  130. /// Header ("Content-Type: application/download ");
  131. /// Header ('content-Disposition: inline; filename = "'. $ outputFileName .'"');
  132. /// Header ("Content-Transfer-Encoding: binary ");
  133. /// Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
  134. /// Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
  135. /// Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
  136. /// Header ("Pragma: no-cache ");
  137. //// $ ObjWriter-> save ('php: // output ');
  138. ?>

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.