Phpexcel quick Development Guide (good)

Source: Internet
Author: User
Phpexcel quick Development Guide (good)

  1. Include ("./class. php"); // contains the basic class header file

  2. Include ("./class/phpexcel/PHPExcel. php"); // generate basic class definitions for excel (note the case sensitivity of file names)

  3. // If an excel file is output directly, the file must be included.

  4. Include ("./class/phpexcel/PHPExcel/IOFactory. php ");

  5. // Create a phpexcel object that contains the output content and format

  6. $ M_objPHPExcel = new PHPExcel ();

  7. // Template file. to separate the format and content, the specific content of the output file is implemented in the template file.

  8. // Operate the object $ m_objPHPExcel in the template file
  9. Include ("./include/excel. php ");

  10. // Type of the 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 output the 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. // Output 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. ?>

2. template file content (additional common operations)

  1. Global $ m_objPHPExcel; // defined by an external file

  2. // Set basic attributes

  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. $ Sheet1 = $ m_objPHPExcel-> createSheet ();
  12. $ Sheet2 = $ m_objPHPExcel-> createSheet ();

  13. // You can use the index to operate the corresponding Workbook.

  14. // You only need to set the index of the workbook to be operated as the current active Workbook, as shown in
  15. // $ M_objPHPExcel-> setActiveSheetIndex (0 );

  16. // Set the first workbook as an active Workbook

  17. $ M_objPHPExcel-> setActiveSheetIndex (0 );

  18. // Set the name of the activity Workbook

  19. // Use the iconv function for conversion encoding if it is a Chinese character
  20. $ M_objPHPExcel-> getActiveSheet ()-> setTitle (iconv ('gbk', 'utf-8', 'Test workbooks '));

  21. // Set the default font and size

  22. $ M_objPHPExcel-> getDefaultStyle ()-> getFont ()-> setName (iconv ('gbk', 'utf-8', ' '));
  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, and centered

  31. $ StyleArray1 = array (
  32. 'Font' => array (
  33. 'Bold '=> true,
  34. 'Color' => array (
  35. 'Arg' => '123 ',
  36. ),
  37. ),

  38. 'Alignment '=> array (

  39. 'Horizontal '=> PHPExcel_Style_Alignment: HORIZONTAL_CENTER,
  40. ),
  41. );

  42. // Apply the style to cell A1

  43. $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('A1')-> applyFromArray ($ styleArray1 );

  44. // Set the cell style (black font)

  45. $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('h5')-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color: COLOR_BLACK); // Black

  46. // Set the cell format (background)

  47. $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('h5')-> getFill ()-> getStartColor ()-> setARGB ('00ff99cc '); // set the background to pale pink

  48. // Set the cell format (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 Baba ');

  52. // Set the cell style (center)

  53. $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('h5')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER );

  54. // Put an image in the cell and the data image in the J1 cell

  55. $ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
  56. $ ObjDrawing-> setName ('logo ');
  57. $ ObjDrawing-> setDescription ('logo ');
  58. $ ObjDrawing-> setPath ("../logo.jpg"); // The Image path, which can only be a relative path
  59. $ ObjDrawing-> setWidth (400); // the image width.
  60. $ ObjDrawing-> setHeight (123); // The Image height.
  61. $ ObjDrawing-> setCoordinates ('j1'); // cell
  62. $ ObjDrawing-> setWorksheet ($ m_objPHPExcel-> getActiveSheet ());

  63. // Set the content of cell A5 and add a hyperlink

  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. ?>

3. generate static files on the server side. The main difference between the two methods is that the formats are different and the template files are identical, the following figure shows the modified image based on the previous example. Note the difference with the previous example.

  1. // Contains the basic class header file

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

  3. // Generate basic class definitions for excel (note the case sensitivity of file names)

  4. Include ("./class/phpexcel/PHPExcel. php ");
  5. // Contains files in Excel5 format. if you need to generate an excel2007 file, you can include the corresponding Writer.
  6. Include ("./class/phpexcel/PHPExcel/Writer/Excel5.php ");
  7. // Contains PDF files.
  8. Include ("./class/phpexcel/PHPExcel/Writer/PDF. php ");

  9. // Create a phpexcel object that contains the output content and format

  10. $ M_objPHPExcel = new PHPExcel ();

  11. // Template file. to separate the format and content, the specific content of the output file is implemented in the template file.

  12. // Operate the object $ m_objPHPExcel in the template file
  13. Include ("./include/excel. php ");

  14. // Type of the 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 storage path, which must be writable

  19. $ M_strOutputPath = "./output /";

  20. // If you need to output the EXCEL format

  21. If ($ m_exportType = "excel "){
  22. $ ObjWriter = new PHPExcel_Writer_Excel5 ($ m_objPHPExcel );
  23. $ ObjWriter-> save ($ m_strOutputPath. $ m_strOutputExcelFileName );
  24. }

  25. // Output PDF format

  26. If ($ m_exportType = "pdf "){
  27. $ ObjWriter = new PHPExcel_Writer_PDF ($ m_objPHPExcel );
  28. $ ObjWriter-> save ($ m_strOutputPath. $ m_strOutputPdfFileName );
  29. }
  30. ?>

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.