Phpexcel Class Library Instance support (excel2003 excel2007)

Source: Internet
Author: User
Tags getcolor
  1. Ini_set ("Display_errors", 1);//Whether an error message is displayed

  2. Ini_set (' include_path ', Ini_get (' include_path '). ';D: \ \php\\phpexcel 1.6.6\\tests\\classes\\ ');//Set this page to include a path
  3. Include "classes/phpexcel.php";
  4. Include "classes/phpexcel/writer/excel5.php";
  5. Create an Excel
  6. $objPHPExcel = new Phpexcel ();
  7. $objWriter = new Phpexcel_writer_excel5 ($objPHPExcel);
  8. $objWriter->save ("Xxx.xls");
  9. ?>

  10. To set the include path for the Phpexcel class library
  11. Set_include_path ('. '). Path_separator.
  12. ' D:\Zeal\PHP_LIBS '. Path_separator.
  13. Get_include_path ());
  14. /**
  15. * Using the example, for lines that start with////are different options, depending on the actual need
  16. * Open the comment for the corresponding line.
  17. * If you use Excel5, the content of the output should be GBK encoded.
  18. */
  19. Require_once ' phpexcel.php ';
  20. Uncomment
  21. Require_once ' phpexcel/writer/excel5.php '; For other low version xls
  22. Or
  23. Require_once ' phpexcel/writer/excel2007.php '; For excel-2007 format
  24. Create a Processing object instance
  25. $objExcel = new Phpexcel ();
  26. Create a file format to write to an object instance, uncomment
  27. $objWriter = new Phpexcel_writer_excel5 ($objExcel); For other version formats
  28. Or
  29. $objWriter = new phpexcel_writer_excel2007 ($objExcel); For 2007 formats
  30. $objWriter->setoffice2003compatibility (TRUE);
  31. //*************************************
  32. Set document basic Properties
  33. $objProps = $objExcel->getproperties ();
  34. $objProps->setcreator ("Zeal Li");
  35. $objProps->setlastmodifiedby ("Zeal Li");
  36. $objProps->settitle ("Office XLS Test Document");
  37. $objProps->setsubject ("Office XLS Test Document, Demo");
  38. $objProps->setdescription ("Test document, generated by Phpexcel.");
  39. $objProps->setkeywords ("Office Excel Phpexcel");
  40. $objProps->setcategory ("Test");
  41. //*************************************
  42. Sets the current sheet index for subsequent content operations.
  43. It is generally only necessary to display a call when multiple sheet are used.
  44. By default, Phpexcel automatically creates the first sheet to be set sheetindex=0
  45. $objExcel->setactivesheetindex (0);
  46. $objActSheet = $objExcel->getactivesheet ();
  47. Sets the name of the currently active sheet
  48. $objActSheet->settitle (' Test sheet ');
  49. //*************************************
  50. Set cell contents
  51. //
  52. Automatically determine cell content type by phpexcel based on incoming content
  53. $objActSheet->setcellvalue (' A1 ', ' string content '); String content
  54. $objActSheet->setcellvalue (' A2 ', 26); Numerical
  55. $objActSheet->setcellvalue (' A3 ', true); Boolean value
  56. $objActSheet->setcellvalue (' A4 ', ' =sum (A2:A2) '); Formula
  57. Explicitly specifying a content type
  58. $objActSheet->setcellvalueexplicit (' A5 ', ' 847475847857487584 ',
  59. phpexcel_cell_datatype::type_string);
  60. Merge cells
  61. $objActSheet->mergecells (' b1:c22 ');
  62. Separating cells
  63. $objActSheet->unmergecells (' b1:c22 ');
  64. //*************************************
  65. Set cell style
  66. //
  67. Set width
  68. $objActSheet->getcolumndimension (' B ')->setautosize (true);
  69. $objActSheet->getcolumndimension (' A ')->setwidth (30);
  70. $objStyleA 5 = $objActSheet->getstyle (' A5 ');
  71. Sets the number format for the contents of the cell.
  72. //
  73. If you use Phpexcel_writer_excel5 to generate content,
  74. It is important to note that the const variable in the Phpexcel_style_numberformat class defines the
  75. In various custom formats, other types can be used normally, but when the Setformatcode
  76. For Format_number, the actual effect was not formatted as "0". Need
  77. Modify the GETXF ($style) method in the source code of the Phpexcel_writer_excel5_format class,
  78. Add one in front of if ($this->_biff_version = = 0x0500) {(Near line No. 363)
  79. Line code:
  80. if ($ifmt = = = ' 0 ') $ifmt = 1;
  81. //
  82. Format as Phpexcel_style_numberformat::format_number to avoid some large numbers
  83. is displayed using scientific notation, with the following Setautosize method to let the contents of each line
  84. Are all displayed according to the original content.
  85. $objStyleA 5
  86. ->getnumberformat ()
  87. ->setformatcode (Phpexcel_style_numberformat::format_number);
  88. Set font
  89. $objFontA 5 = $objStyleA 5->getfont ();
  90. $objFontA 5->setname (' Courier New ');
  91. $objFontA 5->setsize (10);
  92. $objFontA 5->setbold (TRUE);
  93. $objFontA 5->setunderline (Phpexcel_style_font::underline_single);
  94. $objFontA 5->getcolor ()->setargb (' FF999999 ');
  95. Set alignment
  96. $objAlignA 5 = $objStyleA 5->getalignment ();
  97. $objAlignA 5->sethorizontal (phpexcel_style_alignment::horizontal_right);
  98. $objAlignA 5->setvertical (Phpexcel_style_alignment::vertical_center);
  99. Set border
  100. $objBorderA 5 = $objStyleA 5->getborders ();
  101. $objBorderA 5->gettop ()->setborderstyle (Phpexcel_style_border::border_thin);
  102. $objBorderA 5->gettop ()->getcolor ()->setargb (' FFFF0000 '); Color
  103. $objBorderA 5->getbottom ()->setborderstyle (Phpexcel_style_border::border_thin);
  104. $objBorderA 5->getleft ()->setborderstyle (Phpexcel_style_border::border_thin);
  105. $objBorderA 5->getright ()->setborderstyle (Phpexcel_style_border::border_thin);
  106. Set Fill Color
  107. $objFillA 5 = $objStyleA 5->getfill ();
  108. $objFillA 5->setfilltype (phpexcel_style_fill::fill_solid);
  109. $objFillA 5->getstartcolor ()->setargb (' ffeeeeee ');
  110. Copies the style information from the specified cell.
  111. $objActSheet->duplicatestyle ($objStyleA 5, ' b1:c22 ');
  112. //*************************************
  113. Add a picture
  114. $objDrawing = new phpexcel_worksheet_drawing ();
  115. $objDrawing->setname (' zealimg ');
  116. $objDrawing->setdescription (' Image inserted by Zeal ');
  117. $objDrawing->setpath ('./zeali.net.logo.gif ');
  118. $objDrawing->setheight (36);
  119. $objDrawing->setcoordinates (' C23 ');
  120. $objDrawing->setoffsetx (10);
  121. $objDrawing->setrotation (15);
  122. $objDrawing->getshadow ()->setvisible (true);
  123. $objDrawing->getshadow ()->setdirection (36);
  124. $objDrawing->setworksheet ($objActSheet);
  125. Add a new worksheet
  126. $objExcel->createsheet ();
  127. $objExcel->getsheet (1)->settitle (' Test 2 ');
  128. Protect cells
  129. $objExcel->getsheet (1)->getprotection ()->setsheet (true);
  130. $objExcel->getsheet (1)->protectcells (' A1:c22 ', ' phpexcel ');
  131. //*************************************
  132. Output content
  133. //
  134. $outputFileName = "Output.xls";
  135. to a file
  136. $objWriter->save ($outputFileName);
  137. Or
  138. to the browser
  139. Header ("Content-type:application/force-download");
  140. Header ("Content-type:application/octet-stream");
  141. Header ("Content-type:application/download");
  142. Header (' Content-disposition:inline;filename= '. $outputFileName. ' ');
  143. Header ("Content-transfer-encoding:binary");
  144. Header ("Expires:mon, Jul 1997 05:00:00 GMT");
  145. Header ("last-modified:".) Gmdate ("D, D M Y h:i:s"). "GMT");
  146. Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
  147. Header ("Pragma:no-cache");
  148. $objWriter->save (' php://output ');
  149. ?>

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.