Phpexcel Quick Development Guide (good)

Source: Internet
Author: User
  1. Include ("./class/class.php"); Basic header file containing class

  2. Include ("./class/phpexcel/phpexcel.php"); Generate basic class definitions for Excel (note the case of file names)

  3. If you are outputting an Excel file directly, include this file

  4. Include ("./class/phpexcel/phpexcel/iofactory.php");

  5. Creates a Phpexcel object that contains the contents and format of the output

  6. $m _objphpexcel = new Phpexcel ();

  7. Template file, in order to achieve the separation of format and content, the specific content of the output file implemented in the template file

  8. Template file $m_objphpexcel object to be manipulated
  9. Include ("./include/excel.php");

  10. Type of output file, Excel or PDF

  11. $m _exporttype = "Excel";

  12. $m _stroutputexcelfilename = Date (' y-m-j_h_i_s '). ". XLS "; Output Excel file name

  13. $m _stroutputpdffilename = Date (' y-m-j_h_i_s '). ". PDF "; Output PDF file name

  14. Phpexcel_iofactory, Output Excel

  15. Require_once dirname (__file__). ' /classes/phpexcel/iofactory.php ';

  16. If you need to export Excel format

  17. if ($m _exporttype== "Excel") {
  18. $objWriter = Phpexcel_iofactory::createwriter ($m _objphpexcel, ' Excel5 ');

  19. Output $m_stroutputexcelfilename directly from the browser

  20. Header ("Pragma:public");
  21. Header ("expires:0");
  22. Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
  23. Header ("Content-type:application/force-download");
  24. Header ("content-type:application/vnd.ms-excel;");
  25. Header ("Content-type:application/octet-stream");
  26. Header ("Content-type:application/download");
  27. Header ("Content-disposition:attachment;filename=". $m _stroutputexcelfilename);
  28. Header ("Content-transfer-encoding:binary");
  29. $objWriter->save ("Php://output");
  30. }

  31. If you need to export PDF format

  32. if ($m _exporttype== "pdf") {
  33. $objWriter = Phpexcel_iofactory::createwriter ($m _objphpexcel, ' PDF ');
  34. $objWriter->setsheetindex (0);

  35. Header ("Pragma:public");

  36. Header ("expires:0");
  37. Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
  38. Header ("Content-type:application/force-download");
  39. Header ("Content-type:application/pdf");
  40. Header ("Content-type:application/octet-stream");
  41. Header ("Content-type:application/download");
  42. Header ("Content-disposition:attachment;filename=". $m _stroutputpdffilename);
  43. Header ("Content-transfer-encoding:binary");
  44. $objWriter->save ("Php://output");
  45. }
  46. ?>

Copy Code

2. template file content (additional common operations)

  1. Global $m _objphpexcel; Defined by an external file

  2. Set basic properties

  3. $m _objphpexcel->getproperties ()->setcreator ("Sun Star Data Center")
  4. ->setlastmodifiedby ("Sun Star Data Center")
  5. ->settitle ("Microsoft Office Excel Document")
  6. ->setsubject ("Test Data report – from Sunstar data Center")
  7. ->setdescription ("LD Test Data Report, Generate by Sunstar data Center")
  8. ->setkeywords ("Sunstar LD report")
  9. ->setcategory ("Test result file");

  10. Create multiple workbooks

  11. $sheet 1 = $m _objphpexcel->createsheet ();
  12. $sheet 2 = $m _objphpexcel->createsheet ();

  13. Manipulate the corresponding workbook by manipulating the index

  14. Simply set the workbook index to be manipulated as the currently active workbook, as
  15. $m _objphpexcel->setactivesheetindex (0);

  16. Set the first workbook as the active workbook

  17. $m _objphpexcel->setactivesheetindex (0);

  18. Set the active workbook name

  19. If it is Chinese be sure to use the Iconv function to convert the encoding
  20. $m _objphpexcel->getactivesheet ()->settitle (Iconv (' GBK ', ' utf-8 ', ' Test Workbook ');

  21. Set default font and size

  22. $m _objphpexcel->getdefaultstyle ()->getfont ()->setname (Iconv (' GBK ', ' utf-8 ', ' Arial '));
  23. $m _objphpexcel->getdefaultstyle ()->getfont ()->setsize (10);

  24. Set the width of a column

  25. $m _objphpexcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (15);

  26. Set the height of a row

  27. $m _objphpexcel->getactivesheet ()->getrowdimension (' 6 ')->setrowheight (30);

  28. Merge cells

  29. $m _objphpexcel->getactivesheet ()->mergecells (' A1:p1 ');

  30. Define a style, bold, centered

  31. $styleArray 1 = Array (
  32. ' Font ' = = Array (
  33. ' Bold ' = True,
  34. ' Color ' =>array (
  35. ' Argb ' = ' 00000000 ',
  36. ),
  37. ),

  38. ' Alignment ' = Array (

  39. ' Horizontal ' = Phpexcel_style_alignment::horizontal_center,
  40. ),
  41. );

  42. Apply a style to a A1 cell

  43. $m _objphpexcel->getactivesheet ()->getstyle (' A1 ')->applyfromarray ($styleArray 1);

  44. Set cell style (black font)

  45. $m _objphpexcel->getactivesheet ()->getstyle (' H5 ')->getfont ()->getcolor ()->setargb (PHPExcel_Style _color::color_black); Black

  46. formatting cells (background)

  47. $m _objphpexcel->getactivesheet ()->getstyle (' H5 ')->getfill ()->getstartcolor ()->setargb (' 00ff99cc '); Set the background to light pink

  48. Format cells (number format)

  49. $m _objphpexcel->getactivesheet ()->getstyle (' F1 ')->getnumberformat ()->setformatcode (' 0.000 ');

  50. Write content to a specific cell

  51. $m _objphpexcel->getactivesheet ()->setcellvalue (' A1 ', ' Hello Baby ');

  52. Set cell style (centered)

  53. $m _objphpexcel->getactivesheet ()->getstyle (' H5 ')->getalignment ()->sethorizontal (PHPExcel_Style_ Alignment::horizontal_center);

  54. Put the picture in the cell and put the data picture in the J1 cell then cell

  55. $objDrawing = new phpexcel_worksheet_drawing ();
  56. $objDrawing->setname (' Logo ');
  57. $objDrawing->setdescription (' Logo ');
  58. $objDrawing->setpath (". /logo.jpg "); Picture path, only relative path
  59. $objDrawing->setwidth (400); Picture width
  60. $objDrawing->setheight (123); Picture height
  61. $objDrawing->setcoordinates (' J1 ');//Cell
  62. $objDrawing->setworksheet ($m _objphpexcel->getactivesheet ());

  63. Set A5 cell contents and add hyperlinks

  64. $m _objphpexcel->getactivesheet ()->setcellvalue (' A5 ', iconv (' GBK ', ' utf-8 ', ' Hyperlink jbxue.com '));
  65. $m _objphpexcel->getactivesheet ()->getcell (' A5 ')->gethyperlink ()->seturl (' http://bbs.it-home.org/') ;
  66. ?>

Copy Code

3, in the server-side generation of static files compared to direct generation, the main difference between the two methods is the resulting format of the different, template files are identical, the bottom is a change on the basis of the above example, pay attention to the difference between the above example.

  1. Basic header file containing class

  2. Include ("./class/class.php");

  3. Generate basic class definitions for Excel (note the case of file names)

  4. Include ("./class/phpexcel/phpexcel.php");
  5. Contains the file written in Excel5 format, if you need to generate excel2007 files, including the corresponding writer can
  6. Include ("./class/phpexcel/phpexcel/writer/excel5.php");
  7. Contains files written in PDF format
  8. Include ("./class/phpexcel/phpexcel/writer/pdf.php");

  9. Creates a Phpexcel object that contains the contents and format of the output

  10. $m _objphpexcel = new Phpexcel ();

  11. Template file, in order to achieve the separation of format and content, the specific content of the output file implemented in the template file

  12. Template file $m_objphpexcel object to be manipulated
  13. Include ("./include/excel.php");

  14. Type of output file, Excel or PDF

  15. $m _exporttype = "PDF";

  16. $m _stroutputexcelfilename = Date (' y-m-j_h_i_s '). ". XLS "; Output Excel file name

  17. $m _stroutputpdffilename = Date (' y-m-j_h_i_s '). ". PDF "; Output PDF file name

  18. Output file save path, this path must be writable

  19. $m _stroutputpath = "./output/";

  20. If you need to export Excel format

  21. if ($m _exporttype== "Excel") {
  22. $objWriter = new Phpexcel_writer_excel5 ($m _objphpexcel);
  23. $objWriter->save ($m _stroutputpath. $m _stroutputexcelfilename);
  24. }

  25. If you need to export PDF format

  26. if ($m _exporttype== "pdf") {
  27. $objWriter = new Phpexcel_writer_pdf ($m _objphpexcel);
  28. $objWriter->save ($m _stroutputpath. $m _stroutputpdffilename);
  29. }
  30. ?>

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.