Examples of common methods of Phpexcel

Source: Internet
Author: User
Tags getcolor
  1. /**

  2. * Phpexcel Usage Example
  3. * by bbs.it-home.org
  4. * ################
  5. */
  6. To set the include path for 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 are examples of use, for lines beginning with////are optional, depending on your needs
  12. * Open the comment for the corresponding line.
  13. * If you use Excel5, the content of the output should be GBK encoded.
  14. */
  15. Require_once ' phpexcel.php ';
  16. Uncomment
  17. Require_once ' phpexcel/writer/excel5.php '; For other low version xls
  18. Or
  19. Require_once ' phpexcel/writer/excel2007.php '; For excel-2007 format
  20. Create a Processing object instance
  21. $objExcel = new Phpexcel ();
  22. Create a file format to write to an object instance, uncomment
  23. $objWriter = new Phpexcel_writer_excel5 ($objExcel); For other version formats
  24. Or
  25. $objWriter = new phpexcel_writer_excel2007 ($objExcel); For 2007 formats
  26. $objWriter->setoffice2003compatibility (TRUE);
  27. Set document basic Properties
  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. Sets the current sheet index for subsequent content operations.
  37. It is generally only necessary to display a call when multiple sheet are used.
  38. By default, Phpexcel automatically creates the first sheet to be set sheetindex=0
  39. $objExcel->setactivesheetindex (0);
  40. $objActSheet = $objExcel->getactivesheet ();
  41. Sets the name of the currently active sheet
  42. $objActSheet->settitle (' Test sheet ');
  43. Set cell contents
  44. //
  45. Automatically determine cell content type by phpexcel based on incoming content
  46. $objActSheet->setcellvalue (' A1 ', ' string content '); String content
  47. $objActSheet->setcellvalue (' A2 ', 26); Numerical
  48. $objActSheet->setcellvalue (' A3 ', true); Boolean value
  49. $objActSheet->setcellvalue (' A4 ', ' =sum (A2:A2) '); Formula
  50. Explicitly specifying a content type
  51. $objActSheet->setcellvalueexplicit (' A5 ', ' 847475847857487584 ',
  52. phpexcel_cell_datatype::type_string);
  53. Merge cells
  54. $objActSheet->mergecells (' b1:c22 ');
  55. Separating cells
  56. $objActSheet->unmergecells (' b1:c22 ');
  57. Set cell style

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

Copy Code
  • Related Article

    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.