PHP export EXCEL quick Development Guide-PHPEXCEL usage details. PHP export EXCEL quick development guide phpexcel has a proprietary development documentation. for detailed operations, see its development documentation. this document only optimizes and integrates its use to facilitate the use of new projects.
PHP quick development guide for exporting EXCEL
Phpexcel has proprietary development documentation. for detailed operations, see its development documentation. this document only optimizes and integrates its usage to facilitate rapid development in new projects.
Phpexcel can also generate files in two ways: direct output and static file generation.
Direct output:
The main file is (the same directory file in the class directory ):
The code is as follows:
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 ");
}
?>
Template file content (additional common operations)
The code is as follows:
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 keiyi.com '));
$ M_objPHPExcel-> getActiveSheet ()-> getCell ('a5 ')-> getHyperlink ()-> setUrl ('http: // www.keiyi.com /');
?>
Generate static files on the server
Compared with direct generation, the main difference between the two methods is that the generation format is different, and the template file is exactly the same. The following figure shows the changed format based on the previous example. Note the difference with the previous example.
The code is as follows:
// 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 );
}
?>
Phpexcel has proprietary development documentation. for detailed operations, see its development documentation. this document only optimizes and integrates its use to facilitate the integration of new projects...