Basic usage of phpexcel class library

Source: Internet
Author: User
Tags getcolor
Basic usage of phpexcel class library

  1. // Write an excel file

  2. // Include class
  3. Require_once ('classes/PHPExcel. php ');
  4. Require_once ('classes/PHPExcel/Writer/excel2007.php ');
  5. $ ObjPHPExcel = new PHPExcel ();

  6. // Set properties

  7. $ ObjPHPExcel-> getProperties ()-> setCreator ("Maarten Balliauw ");
  8. $ ObjPHPExcel-> getProperties ()-> setLastModifiedBy ("Maarten Balliauw ");
  9. $ ObjPHPExcel-> getProperties ()-> setTitle ("Office 2007 XLSX Test Document ");
  10. $ ObjPHPExcel-> getProperties ()-> setSubject ("Office 2007 XLSX Test Document ");
  11. $ ObjPHPExcel-> getProperties ()-> setDescription ("Test document for Office 2007 XLSX, generated using PHP classes .");
  12. $ ObjPHPExcel-> getProperties ()-> setKeywords ("office 2007 openxml php ");
  13. $ ObjPHPExcel-> getProperties ()-> setCategory ("Test result file ");

  14. // Add some data Add data

  15. $ ObjPHPExcel-> setActiveSheetIndex (0 );
  16. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('A1', 'Hello'); // you can specify the location
  17. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('A2 ', true );
  18. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('A3 ', false );
  19. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('B2', 'World! ');
  20. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('b3', 2 );
  21. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('C1', 'Hello ');
  22. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('D2 ', 'World! ');

  23. // Loop

  24. For ($ I = 1; I I <200; $ I ++ ){
  25. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('A'. $ I, $ I );
  26. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('B'. $ I, 'Test value ');
  27. }

  28. // Format the date

  29. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('d1 ', time ());
  30. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d1 ')-> getNumberFormat ()-> setFormatCode (PHPExcel_Style_NumberFormat: FORMAT_DATE_YYYYMMDDSLASH );

  31. // Add comment

  32. $ ObjPHPExcel-> getActiveSheet ()-> getComment ('e11')-> setAuthor ('phpexcel ');
  33. $ ObjCommentRichText = $ objPHPExcel-> getActiveSheet ()-> getComment ('e11')-> getText ()-> createTextRun ('phpexcel :');
  34. $ ObjCommentRichText-> getFont ()-> setBold (true );
  35. $ ObjPHPExcel-> getActiveSheet ()-> getComment ('e11')-> getText ()-> createTextRun ("\ r \ n ");
  36. $ ObjPHPExcel-> getActiveSheet ()-> getComment ('e11')-> getText ()-> createTextRun ('total amount on the current invoice, excluding VAT .');

  37. // Add rich-text string Add text to set the style

  38. $ ObjRichText = new PHPExcel_RichText ($ objPHPExcel-> getActiveSheet ()-> getCell ('a18 '));
  39. $ ObjRichText-> createText ('This invoice is ');
  40. $ ObjPayable = $ objRichText-> createTextRun ('Payable within thirty days after the end of the month ');
  41. $ ObjPayable-> getFont ()-> setBold (true );
  42. $ ObjPayable-> getFont ()-> setItalic (true );
  43. $ ObjPayable-> getFont ()-> setColor (new PHPExcel_Style_Color (PHPExcel_Style_Color: COLOR_DARKGREEN ));
  44. $ ObjRichText-> createText (', unless specified otherwise on the invoice .');

  45. // Merge cells Merge and separate cells

  46. $ ObjPHPExcel-> getActiveSheet ()-> mergeCells ('a18: e22 ');
  47. $ ObjPHPExcel-> getActiveSheet ()-> unmergeCells ('a18: e22 ');

  48. // Protect cells

  49. $ ObjPHPExcel-> getActiveSheet ()-> getProtection ()-> setSheet (true); // Needs to be set to true in order to enable any worksheet protection!
  50. $ ObjPHPExcel-> getActiveSheet ()-> protectCells ('A3: e13', 'phpexcel ');

  51. // Set cell number formats numeric formatting

  52. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e4 ')-> getNumberFormat ()-> setFormatCode (PHPExcel_Style_NumberFormat: FORMAT_CURRENCY_EUR_SIMPLE );
  53. $ ObjPHPExcel-> getActiveSheet ()-> duplicateStyle ($ objPHPExcel-> getActiveSheet ()-> getStyle ('e4 '), 'e5: e13 ');

  54. // Set column widths to Set the column width

  55. $ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('B')-> setAutoSize (true );
  56. $ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('D')-> setWidth (12 );

  57. // Set fonts to Set the font

  58. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setName ('candara ');
  59. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setSize (20 );
  60. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setBold (true );
  61. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setUnderline (PHPExcel_Style_Font: UNDERLINE_SINGLE );
  62. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color: COLOR_WHITE );

  63. // Set alignments to Set alignments

  64. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d11')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );
  65. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a18')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_JUSTIFY );
  66. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a18')-> getAlignment ()-> setVertical (PHPExcel_Style_Alignment: VERTICAL_CENTER );
  67. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('A3 ')-> getAlignment ()-> setWrapText (true );

  68. // Set column borders to Set the column border

  69. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('A4 ')-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  70. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a10')-> getBorders ()-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  71. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e10')-> getBorders ()-> getRight ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
  72. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getBorders ()-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THICK );
  73. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e13')-> getBorders ()-> getBottom ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THICK );

  74. // Set border colors to Set the border color

  75. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getBorders ()-> getLeft ()-> getColor ()-> setARGB ('ff993300 ');
  76. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getBorders ()-> getTop ()-> getColor ()-> setARGB ('ff993300 ');
  77. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getBorders ()-> getBottom ()-> getColor ()-> setARGB ('ff993300 ');
  78. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e13')-> getBorders ()-> getRight ()-> getColor ()-> setARGB ('ff993300 ');

  79. // Set fills to Set filling

  80. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('A1')-> getFill ()-> setFillType (PHPExcel_Style_Fill: FILL_SOLID );
  81. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('A1')-> getFill ()-> getStartColor ()-> setARGB ('ff808080 ');

  82. // Add a hyperlink to the sheet Add link

  83. $ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('e26', 'www .phpexcel.net ');
  84. $ ObjPHPExcel-> getActiveSheet ()-> getCell ('e26')-> getHyperlink ()-> setUrl ('http: // www.phpexcel.net ');
  85. $ ObjPHPExcel-> getActiveSheet ()-> getCell ('e26')-> getHyperlink ()-> setTooltip ('navigate to website ');
  86. $ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e26')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );

  87. // Add a drawing to the worksheet to Add an image

  88. $ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
  89. $ ObjDrawing-> setName ('logo ');
  90. $ ObjDrawing-> setDescription ('logo ');
  91. $ ObjDrawing-> setPath ('./images/officelogo.jpg ');
  92. $ ObjDrawing-> setHeight (36 );
  93. $ ObjDrawing-> setCoordinates ('b15 ');
  94. $ ObjDrawing-> setOffsetX (110 );
  95. $ ObjDrawing-> setRotation (25 );
  96. $ ObjDrawing-> getShadow ()-> setVisible (true );
  97. $ ObjDrawing-> getShadow ()-> setDirection (45 );
  98. $ ObjDrawing-> setWorksheet ($ objPHPExcel-> getActiveSheet ());

  99. // Play around with inserting and removing rows and columns

  100. $ ObjPHPExcel-> getActiveSheet ()-> insertNewRowBefore (6, 10 );
  101. $ ObjPHPExcel-> getActiveSheet ()-> removeRow (6, 10 );
  102. $ ObjPHPExcel-> getActiveSheet ()-> insertNewColumnBefore ('e', 5 );
  103. $ ObjPHPExcel-> getActiveSheet ()-> removeColumn ('e', 5 );

  104. // Add conditional formatting

  105. $ ObjConditional1 = new PHPExcel_Style_Conditional ();
  106. $ ObjConditional1-> setConditionType (PHPExcel_Style_Conditional: CONDITION_CELLIS );
  107. $ ObjConditional1-> setOperatorType (PHPExcel_Style_Conditional: OPERATOR_LESSTHAN );
  108. $ ObjConditional1-> setCondition ('0 ');
  109. $ ObjConditional1-> getStyle ()-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color: COLOR_RED );
  110. $ ObjConditional1-> getStyle ()-> getFont ()-> setBold (true );

  111. // Set autofilter automatic filtering

  112. $ ObjPHPExcel-> getActiveSheet ()-> setAutoFilter ('A1: C9 ');

  113. // Hide the column in Hide "Phone" and "fax" column

  114. $ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('C')-> setVisible (false );
  115. $ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('D')-> setVisible (false );

  116. // Set document security to Set document security

  117. $ ObjPHPExcel-> getSecurity ()-> setLockWindows (true );
  118. $ ObjPHPExcel-> getSecurity ()-> setLockStructure (true );
  119. $ ObjPHPExcel-> getSecurity ()-> setWorkbookPassword ("PHPExcel ");

  120. // Set sheet security to Set worksheet security

  121. $ ObjPHPExcel-> getActiveSheet ()-> getProtection ()-> setPassword ('phpexcel ');
  122. $ ObjPHPExcel-> getActiveSheet ()-> getProtection ()-> setSheet (true); // This shocould be enabled in order to enable any of the following!
  123. $ ObjPHPExcel-> getActiveSheet ()-> getProtection ()-> setSort (true );
  124. $ ObjPHPExcel-> getActiveSheet ()-> getProtection ()-> setInsertRows (true );
  125. $ ObjPHPExcel-> getActiveSheet ()-> getProtection ()-> setFormatCells (true );

  126. // Calculated data calculation

  127. Echo 'value of B14 [= COUNT (B2: B12)]: '. $ objPHPExcel-> getActiveSheet ()-> getCell ('b14')-> getCalculatedValue (). "\ r \ n ";

  128. // Set outline levels

  129. $ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('e')-> setOutlineLevel (1 );
  130. $ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('e')-> setVisible (false );
  131. $ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('e')-> setCollapsed (true );

  132. // Freeze panes

  133. $ ObjPHPExcel-> getActiveSheet ()-> freezePane ('A2 ');

  134. // Rows to repeat top

  135. $ ObjPHPExcel-> getActiveSheet ()-> getPageSetup ()-> setRowsToRepeatAtTopByStartAndEnd (1, 1 );

  136. // Set data validation input value

  137. $ ObjValidation = $ objPHPExcel-> getActiveSheet ()-> getCell ('b3')-> getDataValidation ();
  138. $ ObjValidation-> setType (PHPExcel_Cell_DataValidation: TYPE_WHOLE );
  139. $ ObjValidation-> setErrorStyle (PHPExcel_Cell_DataValidation: STYLE_STOP );
  140. $ ObjValidation-> setAllowBlank (true );
  141. $ ObjValidation-> setShowInputMessage (true );
  142. $ ObjValidation-> setShowErrorMessage (true );
  143. $ ObjValidation-> setErrorTitle ('input error ');
  144. $ ObjValidation-> setError ('number is not allowed! ');
  145. $ ObjValidation-> setPromptTitle ('allowed input ');
  146. $ ObjValidation-> setPrompt ('only numbers between 10 and 20 are allowed .');
  147. $ ObjValidation-> setFormula1 (10 );
  148. $ ObjValidation-> setFormula2 (20 );
  149. $ ObjPHPExcel-> getActiveSheet ()-> getCell ('b3')-> setDataValidation ($ objValidation );

  150. // Create a new worksheet, after the default sheet creates a new work Tag

  151. $ ObjPHPExcel-> createSheet ();
  152. $ ObjPHPExcel-> setActiveSheetIndex (1 );

  153. // Set header and footer. When no different headers for odd/even are used, odd header is assumed. header and footer

  154. $ ObjPHPExcel-> getActiveSheet ()-> getHeaderFooter ()-> setOddHeader ('& C & HPlease treat this document as confidential! ');
  155. $ ObjPHPExcel-> getActiveSheet ()-> getHeaderFooter ()-> setOddFooter ('& L & B '. $ objPHPExcel-> getProperties ()-> getTitle (). '& RPage & P of & N ');

  156. // Set page orientation and size

  157. $ ObjPHPExcel-> getActiveSheet ()-> getPageSetup ()-> setOrientation (PHPExcel_Worksheet_PageSetup: ORIENTATION_LANDSCAPE );
  158. $ ObjPHPExcel-> getActiveSheet ()-> getPageSetup ()-> setPaperSize (PHPExcel_Worksheet_PageSetup: PAPERSIZE_A4 );

  159. // Rename sheet Rename the worksheet label

  160. $ ObjPHPExcel-> getActiveSheet ()-> setTitle ('simple ');

  161. // Set active sheet index to the first sheet, so Excel opens this as the first sheet

  162. $ ObjPHPExcel-> setActiveSheetIndex (0 );

  163. // Save Excel 2007 file Save

  164. $ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel );
  165. $ ObjWriter-> save (str_replace ('. php', '.xlsx', _ FILE __));
  166. // Save Excel 5 file Save
  167. Require_once ('classes/PHPExcel/Writer/excel5.php ');
  168. $ ObjWriter = new PHPExcel_Writer_Excel5 ($ objPHPExcel );
  169. $ ObjWriter-> save (str_replace ('. php', '.xls', _ FILE __));

  170. // Save the new version 1.6.2

  171. Require_once ('classes/PHPExcel/IOFactory. php ');
  172. $ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 ');
  173. $ ObjWriter-> save (str_replace ('. php', '.xls', _ FILE __));
  174. Read excel

  175. // Include class

  176. Require_once ('classes/PHPExcel/Reader/excel2007.php ');
  177. $ ObjReader = new PHPExcel_Reader_Excel2007;
  178. $ ObjPHPExcel = $ objReader-> load ("05featuredemo.xlsx ");
  179. Read/write csv

  180. Require_once ("05featuredemo. inc. php ");

  181. Require_once ('classes/PHPExcel/Writer/CSV. php ');
  182. Require_once ('classes/PHPExcel/Reader/CSV. php ');
  183. Require_once ('classes/PHPExcel/Writer/excel2007.php ');
  184. // Write to CSV format
  185. $ ObjWriter = new PHPExcel_Writer_CSV ($ objPHPExcel );
  186. $ ObjWriter-> setDelimiter (';');
  187. $ ObjWriter-> setEnclosure ('');
  188. $ ObjWriter-> setLineEnding ("\ r \ n ");
  189. $ ObjWriter-> setSheetIndex (0 );
  190. $ ObjWriter-> save (str_replace ('. php', '.csv', _ FILE __));
  191. // Read from CSV format
  192. $ ObjReader = new PHPExcel_Reader_CSV ();
  193. $ ObjReader-> setDelimiter (';');
  194. $ ObjReader-> setEnclosure ('');
  195. $ ObjReader-> setLineEnding ("\ r \ n ");
  196. $ ObjReader-> setSheetIndex (0 );
  197. $ ObjPHPExcelFromCSV = $ objReader-> load (str_replace ('. php', '.csv', _ FILE __));
  198. // Write to Excel2007 format
  199. $ Ob1_riter2007 = new PHPExcel_Writer_Excel2007 ($ objPHPExcelFromCSV );
  200. $ Ob1_riter2007-> save (str_replace ('. php', '.xlsx', _ FILE __));
  201. Write html

  202. Require_once ("05featuredemo. inc. php ");

  203. Require_once ('classes/PHPExcel/Writer/HTML. php ');
  204. // Write to HTML format
  205. $ ObjWriter = new PHPExcel_Writer_HTML ($ objPHPExcel );
  206. $ ObjWriter-> setSheetIndex (0 );
  207. $ ObjWriter-> save (str_replace ('. php', '.htm', _ FILE __));
  208. Write pdf

  209. Require_once ("05featuredemo. inc. php ");

  210. Require_once ('classes/PHPExcel/IOFactory. php ');
  211. // Write to PDF format
  212. $ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'PDF ');
  213. $ ObjWriter-> setSheetIndex (0 );
  214. $ ObjWriter-> save (str_replace ('. php', 'clerk', _ FILE __));
  215. // Echo memory peak usage
  216. Echo date ('H: I: s'). "Peak memory usage:". (memory_get_peak_usage (true)/1024/1024). "MB \ r \ n ";

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.