Phpexcel exporting an Excel plus hyperlink instance code

Source: Internet
Author: User
Tags border color echo date getcolor
  1. Write Excel

  2. Include class

  3. Require_once (' classes/phpexcel.php ');
  4. Require_once (' classes/phpexcel/writer/excel2007.php ');
  5. $objPHPExcel = new Phpexcel ();

  6. /**

  7. * Phpexcel Export Excel
  8. * by bbs.it-home.org
  9. */

  10. Set properties settings file attributes

  11. $objPHPExcel->getproperties ()->setcreator ("Maarten Balliauw");
  12. $objPHPExcel->getproperties ()->setlastmodifiedby ("Maarten Balliauw");
  13. $objPHPExcel->getproperties ()->settitle ("Office" XLSX Test Document ");
  14. $objPHPExcel->getproperties ()->setsubject ("Office" XLSX Test Document ");
  15. $objPHPExcel->getproperties ()->setdescription ("Test document for Office-XLSX, generated using PHP classes.");
  16. $objPHPExcel->getproperties ()->setkeywords ("Office openxml PHP");
  17. $objPHPExcel->getproperties ()->setcategory ("Test result file");

  18. Add some data

  19. $objPHPExcel->setactivesheetindex (0);
  20. $objPHPExcel->getactivesheet ()->setcellvalue (' a1′, ' Hello ');//can specify location
  21. $objPHPExcel->getactivesheet ()->setcellvalue (' a2′, true);
  22. $objPHPExcel->getactivesheet ()->setcellvalue (' a3′, false);
  23. $objPHPExcel->getactivesheet ()->setcellvalue (' b2′, ' world! ');
  24. $objPHPExcel->getactivesheet ()->setcellvalue (' b3′, 2);
  25. $objPHPExcel->getactivesheet ()->setcellvalue (' c1′, ' Hello ');
  26. $objPHPExcel->getactivesheet ()->setcellvalue (' d2′, ' world! ');
  27. Cycle
  28. for ($i = 1; $i <200; $i + +) {
  29. $objPHPExcel->getactivesheet ()->setcellvalue (' A '. $i, $i);
  30. $objPHPExcel->getactivesheet ()->setcellvalue (' B '. $i, ' Test value ');
  31. }
  32. Date formatting
  33. $objPHPExcel->getactivesheet ()->setcellvalue (' d1′, Time ());
  34. $objPHPExcel->getactivesheet ()->getstyle (' d1′)->getnumberformat ()->setformatcode (phpexcel_style_ Numberformat::format_date_yyyymmddslash);

  35. Add Comment Comment

  36. $objPHPExcel->getactivesheet ()->getcomment (' e11′)->setauthor (' Phpexcel ');
  37. $objCommentRichText = $objPHPExcel->getactivesheet ()->getcomment (' e11′)->gettext ()->createtextrun (' Phpexcel: ');
  38. $objCommentRichText->getfont ()->setbold (true);
  39. $objPHPExcel->getactivesheet ()->getcomment (' e11′)->gettext ()->createtextrun ("\ r \ n");
  40. $objPHPExcel->getactivesheet ()->getcomment (' e11′)->gettext ()->createtextrun (' Total amount on the Current invoice, excluding VAT. ');

  41. Add Rich-text string adds text to set style

  42. $objRichText = new Phpexcel_richtext ($objPHPExcel->getactivesheet ()->getcell (' a18′));
  43. $objRichText->createtext (' This invoice is ');
  44. $objPayable = $objRichText->createtextrun (' payable within thirty days after the end of the month ');
  45. $objPayable->getfont ()->setbold (true);
  46. $objPayable->getfont ()->setitalic (true);
  47. $objPayable->getfont ()->setcolor (new Phpexcel_style_color (Phpexcel_style_color::color_darkgreen));
  48. $objRichText->createtext (', unless specified otherwise on the invoice. ');

  49. Merge cells merges detached cells

  50. $objPHPExcel->getactivesheet ()->mergecells (' a18:e22′);
  51. $objPHPExcel->getactivesheet ()->unmergecells (' a18:e22′);

  52. Protect Cells Cell protection

  53. $objPHPExcel->getactivesheet ()->getprotection ()->setsheet (true);//needs to BES set to true in order to enable any worksheet protection!
  54. $objPHPExcel->getactivesheet ()->protectcells (' a3:e13′, ' phpexcel ');

  55. Set cell number formats numeric formatting

  56. $objPHPExcel->getactivesheet ()->getstyle (' e4′)->getnumberformat ()->setformatcode (phpexcel_style_ Numberformat::format_currency_eur_simple);
  57. $objPHPExcel->getactivesheet ()->duplicatestyle ($objPHPExcel->getactivesheet ()->getstyle (' e4′), ' E5: e13′);

  58. Set column widths setting columns width

  59. $objPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setautosize (true);
  60. $objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (12);

  61. Set Fonts Font

  62. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->setname (' Candara ');
  63. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->setsize (20);
  64. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->setbold (true);
  65. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->setunderline (Phpexcel_style_font:: Underline_single);
  66. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->getcolor ()->setargb (phpexcel_style_ Color::color_white);

  67. Set alignments setting alignment

  68. $objPHPExcel->getactivesheet ()->getstyle (' d11′)->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_right);
  69. $objPHPExcel->getactivesheet ()->getstyle (' a18′)->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_justify);
  70. $objPHPExcel->getactivesheet ()->getstyle (' a18′)->getalignment ()->setvertical (phpexcel_style_ Alignment::vertical_center);
  71. $objPHPExcel->getactivesheet ()->getstyle (' a3′)->getalignment ()->setwraptext (true);

  72. Set column borders setting columns border

  73. $objPHPExcel->getactivesheet ()->getstyle (' a4′)->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
  74. $objPHPExcel->getactivesheet ()->getstyle (' a10′)->getborders ()->getleft ()->setborderstyle ( Phpexcel_style_border::border_thin);
  75. $objPHPExcel->getactivesheet ()->getstyle (' e10′)->getborders ()->getright ()->setborderstyle ( Phpexcel_style_border::border_thin);
  76. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getborders ()->getleft ()->setborderstyle ( Phpexcel_style_border::border_thick);
  77. $objPHPExcel->getactivesheet ()->getstyle (' e13′)->getborders ()->getbottom ()->setborderstyle ( Phpexcel_style_border::border_thick);

  78. Set border colors setting border color

  79. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getborders ()->getleft ()->getcolor () Setargb (' ff993300′);
  80. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getborders ()->gettop ()->getcolor ()->setARGB (' ff993300′);
  81. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getborders ()->getbottom ()->getcolor () Setargb (' ff993300′);
  82. $objPHPExcel->getactivesheet ()->getstyle (' e13′)->getborders ()->getright ()->getcolor () Setargb (' ff993300′);

  83. Set fills setting padding

  84. $objPHPExcel->getactivesheet ()->getstyle (' a1′)->getfill ()->setfilltype (Phpexcel_style_fill::fill_ SOLID);
  85. $objPHPExcel->getactivesheet ()->getstyle (' a1′)->getfill ()->getstartcolor ()->setargb (' FF808080′ ') ;

  86. Add a hyperlink to the sheet link

  87. $objPHPExcel->getactivesheet ()->setcellvalue (' e26′, ' www.phpexcel.net ');
  88. $objPHPExcel->getactivesheet ()->getcell (' e26′)->gethyperlink ()->seturl (' http://www.phpexcel.net ');
  89. $objPHPExcel->getactivesheet ()->getcell (' e26′)->gethyperlink ()->settooltip (' Navigate to website ');
  90. $objPHPExcel->getactivesheet ()->getstyle (' e26′)->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_right);

  91. Add a drawing to the worksheet adding a picture

  92. $objDrawing = new phpexcel_worksheet_drawing ();
  93. $objDrawing->setname (' Logo ');
  94. $objDrawing->setdescription (' Logo ');
  95. $objDrawing->setpath ('./images/officelogo.jpg ');
  96. $objDrawing->setheight (36);
  97. $objDrawing->setcoordinates (' b15′);
  98. $objDrawing->setoffsetx (110);
  99. $objDrawing->setrotation (25);
  100. $objDrawing->getshadow ()->setvisible (true);
  101. $objDrawing->getshadow ()->setdirection (45);
  102. $objDrawing->setworksheet ($objPHPExcel->getactivesheet ());

  103. Play around with inserting and removing rows and columns

  104. $objPHPExcel->getactivesheet ()->insertnewrowbefore (6, 10);
  105. $objPHPExcel->getactivesheet ()->removerow (6, 10);
  106. $objPHPExcel->getactivesheet ()->insertnewcolumnbefore (' E ', 5);
  107. $objPHPExcel->getactivesheet ()->removecolumn (' E ', 5);

  108. ADD Conditional formatting

  109. $objConditional 1 = new phpexcel_style_conditional ();
  110. $objConditional 1->setconditiontype (Phpexcel_style_conditional::condition_cellis);
  111. $objConditional 1->setoperatortype (Phpexcel_style_conditional::operator_lessthan);
  112. $objConditional 1->setcondition (' 0′);
  113. $objConditional 1->getstyle ()->getfont ()->getcolor ()->setargb (phpexcel_style_color::color_red);
  114. $objConditional 1->getstyle ()->getfont ()->setbold (true);

  115. Set AutoFilter Automatic Filtering

  116. $objPHPExcel->getactivesheet ()->setautofilter (' a1:c9′);

  117. Hide "Phone" and "Fax" column hidden columns

  118. $objPHPExcel->getactivesheet ()->getcolumndimension (' C ')->setvisible (false);
  119. $objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setvisible (false);

  120. Set Document security

  121. $objPHPExcel->getsecurity ()->setlockwindows (true);
  122. $objPHPExcel->getsecurity ()->setlockstructure (true);
  123. $objPHPExcel->getsecurity ()->setworkbookpassword ("Phpexcel");

  124. Set Sheet Security Setup worksheet safety

  125. $objPHPExcel->getactivesheet ()->getprotection ()->setpassword (' Phpexcel ');
  126. $objPHPExcel->getactivesheet ()->getprotection ()->setsheet (TRUE);//This should is enabled in order to enable Any of the following!
  127. $objPHPExcel->getactivesheet ()->getprotection ()->setsort (true);
  128. $objPHPExcel->getactivesheet ()->getprotection ()->setinsertrows (true);
  129. $objPHPExcel->getactivesheet ()->getprotection ()->setformatcells (true);

  130. Calculated data calculation

  131. Echo ' Value of B14 [=count (B2:B12)]: '. $objPHPExcel->getactivesheet ()->getcell (' b14′)->getcalculatedvalue (). "\ r \ n";

  132. Set outline Levels

  133. $objPHPExcel->getactivesheet ()->getcolumndimension (' E ')->setoutlinelevel (1);
  134. $objPHPExcel->getactivesheet ()->getcolumndimension (' E ')->setvisible (false);
  135. $objPHPExcel->getactivesheet ()->getcolumndimension (' E ')->setcollapsed (true);

  136. Freeze panes

  137. $objPHPExcel->getactivesheet ()->freezepane (' a2′);

  138. Rows to repeat at top

  139. $objPHPExcel->getactivesheet ()->getpagesetup ()->setrowstorepeatattopbystartandend (1, 1);

  140. Set data validation Validate input values

  141. $objValidation = $objPHPExcel->getactivesheet ()->getcell (' b3′)->getdatavalidation ();
  142. $objValidation->settype (Phpexcel_cell_datavalidation::type_whole);
  143. $objValidation->seterrorstyle (phpexcel_cell_datavalidation::style_stop);
  144. $objValidation->setallowblank (TRUE);
  145. $objValidation->setshowinputmessage (TRUE);
  146. $objValidation->setshowerrormessage (TRUE);
  147. $objValidation->seterrortitle (' Input error ');
  148. $objValidation->seterror (' number is not allowed! ');
  149. $objValidation->setprompttitle (' allowed input ');
  150. $objValidation->setprompt (' only numbers between and allowed. ');
  151. $objValidation->setformula1 (10);
  152. $objValidation->SETFORMULA2 (20);
  153. $objPHPExcel->getactivesheet ()->getcell (' b3′)->setdatavalidation ($objValidation);

  154. Create a new worksheet, after the default sheet, creates a work label

  155. $objPHPExcel->createsheet (); bbs.it-home.org
  156. $objPHPExcel->setactivesheetindex (1);

  157. Set Header and footer. When no different headers to Odd/even is used, odd header is assumed. Header Footer

  158. $objPHPExcel->getactivesheet ()->getheaderfooter ()->setoddheader (' &c&hplease treat this document As confidential! ');
  159. $objPHPExcel->getactivesheet ()->getheaderfooter ()->setoddfooter (' &l&b '. $objPHPExcel GetProperties ()->gettitle (). ' &rpage &p of &n ');

  160. Set page orientation and size direction sizes

  161. $objPHPExcel->getactivesheet ()->getpagesetup ()->setorientation (phpexcel_worksheet_pagesetup::o Rientation_landscape);
  162. $objPHPExcel->getactivesheet ()->getpagesetup ()->setpapersize (phpexcel_worksheet_pagesetup::P apersize_ A4);

  163. Rename Sheet Renaming sheet tabs

  164. $objPHPExcel->getactivesheet ()->settitle (' simple ');

  165. Set Active sheet Index to the first sheet, so Excel opens this as the first sheet

  166. $objPHPExcel->setactivesheetindex (0);

  167. Save Excel file Saved

  168. $objWriter = new phpexcel_writer_excel2007 ($objPHPExcel);
  169. $objWriter->save (Str_replace ('. php ', '. xlsx ', __file__));

  170. Save Excel 5 File

  171. Require_once (' classes/phpexcel/writer/excel5.php ');
  172. $objWriter = new Phpexcel_writer_excel5 ($objPHPExcel);
  173. $objWriter->save (Str_replace ('. php ', '. xls ', __file__));

  174. 1.6.2 New Save

  175. Require_once (' classes/phpexcel/iofactory.php ');
  176. $objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' excel2007′ ');
  177. $objWriter->save (Str_replace ('. php ', '. xls ', __file__));

  178. Read Excel

  179. Include class
  180. Require_once (' classes/phpexcel/reader/excel2007.php ');
  181. $objReader = new phpexcel_reader_excel2007;

  182. $objPHPExcel = $objReader->load ("05featuredemo.xlsx");

  183. Read/write CSV

  184. Require_once ("05featuredemo.inc.php");
  185. Require_once (' classes/phpexcel/writer/csv.php ');
  186. Require_once (' classes/phpexcel/reader/csv.php ');
  187. Require_once (' classes/phpexcel/writer/excel2007.php ');

  188. Write to CSV format writes

  189. $objWriter = new Phpexcel_writer_csv ($objPHPExcel);
  190. $objWriter->setdelimiter (';');
  191. $objWriter->setenclosure (");
  192. $objWriter->setlineending ("\ r \ n");
  193. $objWriter->setsheetindex (0);
  194. $objWriter->save (Str_replace ('. php ', '. csv ', __file__));

  195. Read from CSV format reading

  196. $objReader = new Phpexcel_reader_csv ();
  197. $objReader->setdelimiter (';');
  198. $objReader->setenclosure (");
  199. $objReader->setlineending ("\ r \ n");
  200. $objReader->setsheetindex (0);
  201. $objPHPExcelFromCSV = $objReader->load (str_replace ('. php ', '. csv ', __file__));

  202. Write to Excel2007 format

  203. $objWriter = new phpexcel_writer_excel2007 ($objPHPExcelFromCSV);
  204. $objWriter 2007->save (Str_replace ('. php ', '. xlsx ', __file__));

  205. Write HTML

  206. Require_once ("05featuredemo.inc.php");
  207. Require_once (' classes/phpexcel/writer/html.php ');

  208. Write to HTML format

  209. $objWriter = new phpexcel_writer_html ($objPHPExcel);
  210. $objWriter->setsheetindex (0);
  211. $objWriter->save (Str_replace ('. php ', '. htm ', __file__));

  212. Write a PDF

  213. Require_once ("05featuredemo.inc.php");
  214. Require_once (' classes/phpexcel/iofactory.php ');

  215. Write to PDF format

  216. $objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' PDF ');
  217. $objWriter->setsheetindex (0);
  218. $objWriter->save (Str_replace ('. php ', '. pdf ', __file__));
  219. Echo Memory Peak usage
  220. echo Date (' H:i:s '). "Peak Memory Usage:". (Memory_get_peak_usage (TRUE)/1024/1024). "Mb\r\n";

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.