Summary of common PHPExcel methods

Source: Internet
Author: User
Tags getcolor
& Lt ;? // Set includepathset_include_path (& amp; #39 ;. & amp; #39 ;. PATH_SEPARATOR. & amp; #39; D: \ Zeal \ PHP_LIBS & amp; #39 ;. PATH_SEPARATOR.get_include_path ();/** www.

 

  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. /** Www.2cto.com
  6. * The following is an example. the optional methods are different for rows starting //.
  7. * Open the comment of the corresponding row.
  8. * If Excel5 is used, the output content should be GBK encoded.
  9. */
  10. Require_once 'phpexcel. php ';
  11. // Uncomment
  12. /// Require_once 'phpexcel/Writer/excel5.php'; // used for other earlier versions of xls
  13. // Or
  14. /// Require_once 'phpexcel/Writer/excel2007.php'; // used for excel-2007 format
  15. // Create a processing object instance
  16. $ ObjExcel = new PHPExcel ();
  17. // Create a file format to write the object instance, uncomment
  18. //// $ ObjWriter = new PHPExcel_Writer_Excel5 ($ objExcel); // used for other version formats
  19. // Or
  20. //// $ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objExcel); // used in 2007 format
  21. // $ ObjWriter-> setOffice2003Compatibility (true );
  22. //*************************************
  23. // Set the basic attributes of the document
  24. $ ObjProps = $ objExcel-> getProperties ();
  25. $ ObjProps-> setCreator ("Zeal Li ");
  26. $ ObjProps-> setLastModifiedBy ("Zeal Li ");
  27. $ ObjProps-> setTitle ("Office XLS Test Document ");
  28. $ ObjProps-> setSubject ("Office XLS Test Document, Demo ");
  29. $ ObjProps-> setDescription ("Test document, generated by PHPExcel .");
  30. $ ObjProps-> setKeywords ("office excel PHPExcel ");
  31. $ ObjProps-> setCategory ("Test ");
  32. //*************************************
  33. // Set the current sheet index for subsequent content operations.
  34. // Display the call only when multiple sheets are used.
  35. // By default, PHPExcel will automatically create the first sheet and set SheetIndex = 0
  36. $ ObjExcel-> setActiveSheetIndex (0 );
  37. $ ObjActSheet = $ objExcel-> getActiveSheet ();
  38. // Set the name of the current active sheet
  39. $ ObjActSheet-> setTitle ('test Sheet ');
  40. //*************************************
  41. // Set the cell content
  42. //
  43. // PHPExcel automatically determines the cell content type based on the input content
  44. $ ObjActSheet-> setCellValue ('A1', 'string content'); // String content
  45. $ ObjActSheet-> setCellValue ('A2 ', 26); // value
  46. $ ObjActSheet-> setCellValue ('A3 ', true); // boolean value
  47. $ ObjActSheet-> setCellValue ('A4 ',' = SUM (A2: A2) '); // formula
  48. // Explicitly specify the content type
  49. $ ObjActSheet-> setCellValueExplicit ('a5 ', '123 ',
  50. PHPExcel_Cell_DataType: TYPE_STRING );
  51. // Merge cells
  52. $ ObjActSheet-> mergeCells ('b1: c22 ');
  53. // Separate cells
  54. $ ObjActSheet-> unmergeCells ('b1: c22 ');
  55. //*************************************
  56. // Set the cell style
  57. //
  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. //*************************************
  104. // Add an image
  105. $ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
  106. $ ObjDrawing-> setName ('zealimg ');
  107. $ ObjDrawing-> setDescription ('image inserted by zeal ');
  108. $ ObjDrawing-> setPath ('./zeali.net.logo.gif ');
  109. $ ObjDrawing-> setHeight (36 );
  110. $ ObjDrawing-> setCoordinates ('c23 ');
  111. $ ObjDrawing-> setOffsetX (10 );
  112. $ ObjDrawing-> setRotation (15 );
  113. $ ObjDrawing-> getShadow ()-> setVisible (true );
  114. $ ObjDrawing-> getShadow ()-> setDirection (36 );
  115. $ ObjDrawing-> setWorksheet ($ objActSheet );
  116. // Add a new worksheet
  117. $ ObjExcel-> createSheet ();
  118. $ ObjExcel-> getSheet (1)-> setTitle ('test 2 ');
  119. // Protect cells
  120. $ ObjExcel-> getSheet (1)-> getProtection ()-> setSheet (true );
  121. $ ObjExcel-> getSheet (1)-> protectCells ('A1: c22', 'phpexcel ');
  122. //*************************************
  123. // Output Content
  124. //
  125. $ OutputFileName = "output.xls ";
  126. // To the file
  127. /// $ ObjWriter-> save ($ outputFileName );
  128. // Or
  129. // Go to the browser
  130. /// Header ("Content-Type: application/force-download ");
  131. /// Header ("Content-Type: application/octet-stream ");
  132. /// Header ("Content-Type: application/download ");
  133. /// Header ('content-Disposition: inline; filename = "'. $ outputFileName .'"');
  134. /// Header ("Content-Transfer-Encoding: binary ");
  135. /// Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
  136. /// Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
  137. /// Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
  138. /// Header ("Pragma: no-cache ");
  139. //// $ ObjWriter-> save ('php: // output ');
  140. ?>
  141. From: zeroplace.cn

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.