What are the common methods of Phpexcel, Phpexcel usage Example

Source: Internet
Author: User
Tags getcolor
  1. To set the include path for the Phpexcel class library

  2. Set_include_path ('. '). Path_separator.
  3. ' D:\Zeal\PHP_LIBS '. Path_separator.
  4. Get_include_path ());

  5. /**

  6. * The following are examples of use, for lines beginning with////are optional, depending on your needs
  7. * Open the comment for the corresponding line.
  8. * If you use Excel5, the content of the output should be GBK encoded.
  9. */
  10. Require_once ' phpexcel.php ';

  11. Uncomment

  12. Require_once ' phpexcel/writer/excel5.php '; For other low version xls
  13. Or
  14. Require_once ' phpexcel/writer/excel2007.php '; For excel-2007 format

  15. Create a Processing object instance

  16. $objExcel = new Phpexcel ();

  17. Create a file format to write to an object instance, uncomment

  18. $objWriter = new Phpexcel_writer_excel5 ($objExcel); For other version formats
  19. Or
  20. $objWriter = new phpexcel_writer_excel2007 ($objExcel); For 2007 formats
  21. $objWriter->setoffice2003compatibility (TRUE);

  22. //*************************************

  23. Set document basic Properties
  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. Sets the current sheet index for subsequent content operations.
  34. It is generally only necessary to display a call when multiple sheet are used.
  35. By default, Phpexcel automatically creates the first sheet to be set sheetindex=0
  36. $objExcel->setactivesheetindex (0);

  37. $objActSheet = $objExcel->getactivesheet ();

  38. Sets the name of the currently active sheet

  39. $objActSheet->settitle (' Test sheet ');

  40. //*************************************

  41. Set cell contents
  42. //
  43. Automatically determine cell content type by phpexcel based on incoming content
  44. $objActSheet->setcellvalue (' A1 ', ' string content '); String content
  45. $objActSheet->setcellvalue (' A2 ', 26); Numerical
  46. $objActSheet->setcellvalue (' A3 ', true); Boolean value
  47. $objActSheet->setcellvalue (' A4 ', ' =sum (A2:A2) '); Formula

  48. Explicitly specifying a content type

  49. $objActSheet->setcellvalueexplicit (' A5 ', ' 847475847857487584 ',
  50. phpexcel_cell_datatype::type_string);

  51. Merge cells

  52. $objActSheet->mergecells (' b1:c22 ');

  53. Separating cells

  54. $objActSheet->unmergecells (' b1:c22 ');

  55. //*************************************

  56. Set cell style
  57. //

  58. Set width

  59. $objActSheet->getcolumndimension (' B ')->setautosize (true);
  60. $objActSheet->getcolumndimension (' A ')->setwidth (30);

  61. $objStyleA 5 = $objActSheet->getstyle (' A5 ');

  62. Sets the number format for the contents of the cell.

  63. //
  64. If you use Phpexcel_writer_excel5 to generate content,
  65. It is important to note that the const variable in the Phpexcel_style_numberformat class defines the
  66. In various custom formats, other types can be used normally, but when the Setformatcode
  67. For Format_number, the actual effect was not formatted as "0". Need
  68. Modify the GETXF ($style) method in the source code of the Phpexcel_writer_excel5_format class,
  69. Add one in front of if ($this->_biff_version = = 0x0500) {(Near line No. 363)
  70. Line code:
  71. if ($ifmt = = = ' 0 ') $ifmt = 1;
  72. //
  73. Format as Phpexcel_style_numberformat::format_number to avoid some large numbers
  74. is displayed using scientific notation, with the following Setautosize method to let the contents of each line
  75. Are all displayed according to the original content.
  76. $objStyleA 5
  77. ->getnumberformat ()
  78. ->setformatcode (Phpexcel_style_numberformat::format_number);

  79. Set font

  80. $objFontA 5 = $objStyleA 5->getfont ();
  81. $objFontA 5->setname (' Courier New ');
  82. $objFontA 5->setsize (10);
  83. $objFontA 5->setbold (TRUE);
  84. $objFontA 5->setunderline (Phpexcel_style_font::underline_single);
  85. $objFontA 5->getcolor ()->setargb (' FF999999 ');

  86. Set alignment

  87. $objAlignA 5 = $objStyleA 5->getalignment ();
  88. $objAlignA 5->sethorizontal (phpexcel_style_alignment::horizontal_right);
  89. $objAlignA 5->setvertical (Phpexcel_style_alignment::vertical_center);

  90. Set border

  91. $objBorderA 5 = $objStyleA 5->getborders ();
  92. $objBorderA 5->gettop ()->setborderstyle (Phpexcel_style_border::border_thin);
  93. $objBorderA 5->gettop ()->getcolor ()->setargb (' FFFF0000 '); Color
  94. $objBorderA 5->getbottom ()->setborderstyle (Phpexcel_style_border::border_thin);
  95. $objBorderA 5->getleft ()->setborderstyle (Phpexcel_style_border::border_thin);
  96. $objBorderA 5->getright ()->setborderstyle (Phpexcel_style_border::border_thin);

  97. Set Fill Color

  98. $objFillA 5 = $objStyleA 5->getfill ();
  99. $objFillA 5->setfilltype (phpexcel_style_fill::fill_solid);
  100. $objFillA 5->getstartcolor ()->setargb (' ffeeeeee ');

  101. Copies the style information from the specified cell.

  102. $objActSheet->duplicatestyle ($objStyleA 5, ' b1:c22 ');

  103. //*************************************

  104. Add a picture
  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 a file
  127. $objWriter->save ($outputFileName);
  128. Or
  129. 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, 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. ?>

Copy Code
  • 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.