Include ("./class. php"); // contains the basic class header file
- Include ("./class/phpexcel/PHPExcel. php"); // generate basic class definitions for excel (note the case sensitivity of file names)
// If an excel file is output directly, the file must be included.
- Include ("./class/phpexcel/PHPExcel/IOFactory. php ");
// Create a phpexcel object that contains the output content and format
- $ M_objPHPExcel = new PHPExcel ();
// Template file. to separate the format and content, the specific content of the output file is implemented in the template file.
- // Operate the object $ m_objPHPExcel in the template file
- Include ("./include/excel. php ");
// Type of the output file, excel or pdf
- $ M_exportType = "excel ";
$ M_strOutputExcelFileName = date ('Y-m-j_H_ I _s '). ". xls"; // output EXCEL file name
- $ M_strOutputPdfFileName = date ('Y-m-j_H_ I _s '). ". pdf"; // output PDF file name
// PHPExcel_IOFactory, output excel
- // Require_once dirname (_ FILE _). '/Classes/PHPExcel/IOFactory. php ';
// If you need to output the EXCEL format
- If ($ m_exportType = "excel "){
- $ ObjWriter = PHPExcel_IOFactory: createWriter ($ m_objPHPExcel, 'excel5 ');
// Output $ m_strOutputExcelFileName directly from the browser
- Header ("Pragma: public ");
- Header ("Expires: 0 ");
- Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
- Header ("Content-Type: application/force-download ");
- Header ("Content-Type: application/vnd. ms-excel ;");
- Header ("Content-Type: application/octet-stream ");
- Header ("Content-Type: application/download ");
- Header ("Content-Disposition: attachment; filename =". $ m_strOutputExcelFileName );
- Header ("Content-Transfer-Encoding: binary ");
- $ ObjWriter-> save ("php: // output ");
- }
// Output PDF format
- If ($ m_exportType = "pdf "){
- $ ObjWriter = PHPExcel_IOFactory: createWriter ($ m_objPHPExcel, 'PDF ');
- $ ObjWriter-> setSheetIndex (0 );
Header ("Pragma: public ");
- Header ("Expires: 0 ");
- Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
- Header ("Content-Type: application/force-download ");
- Header ("Content-Type: application/pdf ");
- Header ("Content-Type: application/octet-stream ");
- Header ("Content-Type: application/download ");
- Header ("Content-Disposition: attachment; filename =". $ m_strOutputPdfFileName );
- Header ("Content-Transfer-Encoding: binary ");
- $ ObjWriter-> save ("php: // output ");
- }
- ?>
2. template file content (additional common operations)
Global $ m_objPHPExcel; // defined by an external file
// Set basic attributes
- $ M_objPHPExcel-> getProperties ()-> setCreator ("Sun Star Data Center ")
- -> SetLastModifiedBy ("Sun Star Data Center ")
- -> SetTitle ("Microsoft Office Excel Document ")
- -> SetSubject ("Test Data Report -- From Sunstar Data Center ")
- -> SetDescription ("LD Test Data Report, Generate by Sunstar Data Center ")
- -> SetKeywords ("sunstar ld report ")
- -> SetCategory ("Test result file ");
// Create multiple Workbooks
- $ Sheet1 = $ m_objPHPExcel-> createSheet ();
- $ Sheet2 = $ m_objPHPExcel-> createSheet ();
// You can use the index to operate the corresponding Workbook.
- // You only need to set the index of the workbook to be operated as the current active Workbook, as shown in
- // $ M_objPHPExcel-> setActiveSheetIndex (0 );
// Set the first workbook as an active Workbook
- $ M_objPHPExcel-> setActiveSheetIndex (0 );
// Set the name of the activity Workbook
- // Use the iconv function for conversion encoding if it is a Chinese character
- $ M_objPHPExcel-> getActiveSheet ()-> setTitle (iconv ('gbk', 'utf-8', 'Test workbooks '));
// Set the default font and size
- $ M_objPHPExcel-> getDefaultStyle ()-> getFont ()-> setName (iconv ('gbk', 'utf-8', ' '));
- $ M_objPHPExcel-> getDefaultStyle ()-> getFont ()-> setSize (10 );
// Set the width of a column
- $ M_objPHPExcel-> getActiveSheet ()-> getColumnDimension ('A')-> setWidth (15 );
// Set the height of a row
- $ M_objPHPExcel-> getActiveSheet ()-> getRowDimension ('6')-> setRowHeight (30 );
// Merge cells
- $ M_objPHPExcel-> getActiveSheet ()-> mergeCells ('A1: P1 ');
// Define a style, bold, and centered
- $ StyleArray1 = array (
- 'Font' => array (
- 'Bold '=> true,
- 'Color' => array (
- 'Arg' => '123 ',
- ),
- ),
'Alignment '=> array (
- 'Horizontal '=> PHPExcel_Style_Alignment: HORIZONTAL_CENTER,
- ),
- );
// Apply the style to cell A1
- $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('A1')-> applyFromArray ($ styleArray1 );
// Set the cell style (black font)
- $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('h5')-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color: COLOR_BLACK); // Black
// Set the cell format (background)
- $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('h5')-> getFill ()-> getStartColor ()-> setARGB ('00ff99cc '); // set the background to pale pink
// Set the cell format (number format)
- $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('F1')-> getNumberFormat ()-> setFormatCode ('0. 000 ');
// Write content to a specific cell
- $ M_objPHPExcel-> getActiveSheet ()-> setCellValue ('A1', 'Hello Baba ');
// Set the cell style (center)
- $ M_objPHPExcel-> getActiveSheet ()-> getStyle ('h5')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER );
// Put an image in the cell and the data image in the J1 cell
- $ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
- $ ObjDrawing-> setName ('logo ');
- $ ObjDrawing-> setDescription ('logo ');
- $ ObjDrawing-> setPath ("../logo.jpg"); // The Image path, which can only be a relative path
- $ ObjDrawing-> setWidth (400); // the image width.
- $ ObjDrawing-> setHeight (123); // The Image height.
- $ ObjDrawing-> setCoordinates ('j1'); // cell
- $ ObjDrawing-> setWorksheet ($ m_objPHPExcel-> getActiveSheet ());
// Set the content of cell A5 and add a hyperlink
- $ M_objPHPExcel-> getActiveSheet ()-> setCellValue ('a5 ', iconv ('gbk', 'utf-8', 'hyperlink jbxue.com '));
- $ M_objPHPExcel-> getActiveSheet ()-> getCell ('a5 ')-> getHyperlink ()-> setUrl ('http: // bbs.it-home.org /');
- ?>
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.
// Contains the basic class header file
- Include ("./class. php ");
// Generate basic class definitions for excel (note the case sensitivity of file names)
- Include ("./class/phpexcel/PHPExcel. php ");
- // Contains files in Excel5 format. if you need to generate an excel2007 file, you can include the corresponding Writer.
- Include ("./class/phpexcel/PHPExcel/Writer/Excel5.php ");
- // Contains PDF files.
- Include ("./class/phpexcel/PHPExcel/Writer/PDF. php ");
// Create a phpexcel object that contains the output content and format
- $ M_objPHPExcel = new PHPExcel ();
// Template file. to separate the format and content, the specific content of the output file is implemented in the template file.
- // Operate the object $ m_objPHPExcel in the template file
- Include ("./include/excel. php ");
// Type of the output file, excel or pdf
- $ M_exportType = "pdf ";
$ M_strOutputExcelFileName = date ('Y-m-j_H_ I _s '). ". xls"; // output EXCEL file name
- $ M_strOutputPdfFileName = date ('Y-m-j_H_ I _s '). ". pdf"; // output PDF file name
// Output file storage path, which must be writable
- $ M_strOutputPath = "./output /";
// If you need to output the EXCEL format
- If ($ m_exportType = "excel "){
- $ ObjWriter = new PHPExcel_Writer_Excel5 ($ m_objPHPExcel );
- $ ObjWriter-> save ($ m_strOutputPath. $ m_strOutputExcelFileName );
- }
// Output PDF format
- If ($ m_exportType = "pdf "){
- $ ObjWriter = new PHPExcel_Writer_PDF ($ m_objPHPExcel );
- $ ObjWriter-> save ($ m_strOutputPath. $ m_strOutputPdfFileName );
- }
- ?>
|