How to use the Phpexcel class to share

Source: Internet
Author: User
Tags echo date getcolor
    1. Include ' phpexcel.php ';
    2. Include ' phpexcel/writer/excel2007.php ';
    3. Or include ' phpexcel/writer/excel5.php '; For the output. xls
    4. Create an Excel
    5. $objPHPExcel = new Phpexcel ();
    6. Save excel-2007 Format
    7. $objWriter = new phpexcel_writer_excel2007 ($objPHPExcel);
    8. or $objwriter = new Phpexcel_writer_excel5 ($objPHPExcel); Non-2007 format
    9. $objWriter->save ("xxx.xlsx");
Copy Code

2, direct output to browser

  1. $objWriter = new Phpexcel_writer_excel5 ($objPHPExcel);

  2. Header ("Pragma:public");
  3. Header ("Expires:0″");
  4. Header ("Cache-control:must-revalidate, post-check=0, Pre-check=0″);
  5. Header ("Content-type:application/force-download");
  6. Header ("Content-type:application/vnd.ms-execl");
  7. Header ("Content-type:application/octet-stream");
  8. Header ("Content-type:application/download");;
  9. Header (' content-disposition:attachment;filename= ' Resume.xls "');
  10. Header ("Content-transfer-encoding:binary");
  11. $objWriter->save (' php://output ');

  12. 3, set the properties of Excel:

  13. Create person
  14. $objPHPExcel->getproperties ()->setcreator ("Maarten Balliauw");
  15. Last modified person
  16. $objPHPExcel->getproperties ()->setlastmodifiedby ("Maarten Balliauw");
  17. Title
  18. $objPHPExcel->getproperties ()->settitle ("Office" XLSX Test Document ");
  19. Topic
  20. $objPHPExcel->getproperties ()->setsubject ("Office" XLSX Test Document ");
  21. Describe
  22. $objPHPExcel->getproperties ()->setdescription ("Test document for Office-XLSX, generated using PHP classes.");
  23. Key words
  24. $objPHPExcel->getproperties ()->setkeywords ("Office openxml PHP");
  25. Kinds
  26. $objPHPExcel->getproperties ()->setcategory ("Test result file");

Copy Code

4, other

  1. Set the current sheet
  2. $objPHPExcel->setactivesheetindex (0);
  3. Set the name of the sheet
  4. $objPHPExcel->getactivesheet ()->settitle (' simple ');
  5. Set the value of a cell
  6. $objPHPExcel->getactivesheet ()->setcellvalue (' a1′, ' String ');
  7. $objPHPExcel->getactivesheet ()->setcellvalue (' a2′, 12);
  8. $objPHPExcel->getactivesheet ()->setcellvalue (' a3′, true);
  9. $objPHPExcel->getactivesheet ()->setcellvalue (' c5′, ' =sum (C2:C4) ');
  10. $objPHPExcel->getactivesheet ()->setcellvalue (' b8′, ' =min (B2:C5) ');
  11. Merge cells
  12. $objPHPExcel->getactivesheet ()->mergecells (' a18:e22′);
  13. Separating cells
  14. $objPHPExcel->getactivesheet ()->unmergecells (' a28:b28′);
  15. Protect cell
  16. $objPHPExcel->getactivesheet ()->getprotection ()->setsheet (true); Needs to BES set to true in order to enable any worksheet protection!
  17. $objPHPExcel->getactivesheet ()->protectcells (' a3:e13′, ' phpexcel ');
  18. Set format
  19. Set Cell number formats
  20. echo Date (' H:i:s '). "Set cell number FORMATSN";
  21. $objPHPExcel->getactivesheet ()->getstyle (' e4′)->getnumberformat ()->setformatcode (phpexcel_style_ Numberformat::format_currency_eur_simple);
  22. $objPHPExcel->getactivesheet ()->duplicatestyle ($objPHPExcel->getactivesheet ()->getstyle (' e4′), ' E5: e13′);
  23. Setting width width
  24. Set column Widths
  25. $objPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setautosize (true);
  26. $objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (12);
  27. Set font
  28. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->setname (' Candara ');
  29. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->setsize (20);
  30. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->setbold (true);
  31. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->setunderline (Phpexcel_style_font:: Underline_single);
  32. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfont ()->getcolor ()->setargb (phpexcel_style_ Color::color_white);
  33. $objPHPExcel->getactivesheet ()->getstyle (' e1′)->getfont ()->getcolor ()->setargb (phpexcel_style_ Color::color_white);
  34. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getfont ()->setbold (true);
  35. $objPHPExcel->getactivesheet ()->getstyle (' e13′)->getfont ()->setbold (true);
  36. Set align
  37. $objPHPExcel->getactivesheet ()->getstyle (' d11′)->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_right);
  38. $objPHPExcel->getactivesheet ()->getstyle (' d12′)->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_right);
  39. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_right);
  40. $objPHPExcel->getactivesheet ()->getstyle (' a18′)->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_justify);
  41. Center vertically
  42. $objPHPExcel->getactivesheet ()->getstyle (' a18′)->getalignment ()->setvertical (phpexcel_style_ Alignment::vertical_center);
  43. Set the border of column
  44. $objPHPExcel->getactivesheet ()->getstyle (' a4′)->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
  45. $objPHPExcel->getactivesheet ()->getstyle (' b4′)->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
  46. $objPHPExcel->getactivesheet ()->getstyle (' c4′)->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
  47. $objPHPExcel->getactivesheet ()->getstyle (' d4′)->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
  48. $objPHPExcel->getactivesheet ()->getstyle (' e4′)->getborders ()->gettop ()->setborderstyle (PHPExcel _style_border::border_thin);
  49. Set the color of the border
  50. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getborders ()->getleft ()->getcolor () Setargb (' ff993300′);
  51. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getborders ()->gettop ()->getcolor ()->setARGB (' ff993300′);
  52. $objPHPExcel->getactivesheet ()->getstyle (' d13′)->getborders ()->getbottom ()->getcolor () Setargb (' ff993300′);
  53. $objPHPExcel->getactivesheet ()->getstyle (' e13′)->getborders ()->gettop ()->getcolor ()->setARGB (' ff993300′);
  54. $objPHPExcel->getactivesheet ()->getstyle (' e13′)->getborders ()->getbottom ()->getcolor () Setargb (' ff993300′);
  55. $objPHPExcel->getactivesheet ()->getstyle (' e13′)->getborders ()->getright ()->getcolor () Setargb (' ff993300′);
  56. Set Fill Color
  57. $objPHPExcel->getactivesheet ()->getstyle (' a1′)->getfill ()->setfilltype (Phpexcel_style_fill::fill_ SOLID);
  58. $objPHPExcel->getactivesheet ()->getstyle (' a1′)->getfill ()->getstartcolor ()->setargb (' FF808080′ ') ;
  59. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfill ()->setfilltype (Phpexcel_style_fill::fill_ SOLID);
  60. $objPHPExcel->getactivesheet ()->getstyle (' b1′)->getfill ()->getstartcolor ()->setargb (' FF808080′ ') ;
  61. Add Picture//bbs.it-home.org
  62. $objDrawing = new phpexcel_worksheet_drawing ();
  63. $objDrawing->setname (' Logo ');
  64. $objDrawing->setdescription (' Logo ');
  65. $objDrawing->setpath ('./images/officelogo.jpg ');
  66. $objDrawing->setheight (36);
  67. $objDrawing->setworksheet ($objPHPExcel->getactivesheet ());
  68. $objDrawing = new phpexcel_worksheet_drawing ();
  69. $objDrawing->setname (' Paid ');
  70. $objDrawing->setdescription (' Paid ');
  71. $objDrawing->setpath ('./images/paid.png ');
  72. $objDrawing->setcoordinates (' b15′);
  73. $objDrawing->setoffsetx (110);
  74. $objDrawing->setrotation (25);
  75. $objDrawing->getshadow ()->setvisible (true);
  76. $objDrawing->getshadow ()->setdirection (45);
  77. $objDrawing->setworksheet ($objPHPExcel->getactivesheet ());
  78. After the default sheet, create a worksheet
  79. echo Date (' H:i:s '). "Create new Worksheet objectn";
  80. $objPHPExcel->createsheet ();
  81. $objWriter = Phpexcel_iofactory::createwriter ($objExcel, ' Excel5 ');
  82. $objWriter-save (' php://output ');
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.