PHPExcel is a very convenient class for generating Excel files. The official download package contains a large number of examples of how to generate various style excel files, but there is no complete example for reading Excel files. xiaoqiang sorted out a simple example of reading Excel files based on the online materials. PHPExcel is a class that facilitates the generation of Excel files, the official download package contains a large number of examples of how to generate various style excel files, but there is no complete example of reading Excel files. xiaoqiang sorted out an example of simply reading an Excel file based on the information on the Internet.
Script ec (2); script
In the website management background, report generation and reading are often used. CSV and Excel are common report formats. CSV is relatively simple, if you have any questions, I will release some CSV instances one after another. Here we will mainly introduce how to use PHP to generate and read Excel files.
To execute the following functions, first introduce a Class Library: PHPExcel. PHPExcel is a powerful PHP class library used to read and write different file formats, such as Excel 2007, PDF, and HTML, this class library is based on Microsoft's OpenXML and PHP, and provides powerful support for Excel, such as setting the working thin, Font Style, image, and border, let's take a look at how it reads and writes Excel files:
First, if an Excel file is generated:
In the following code, the arrayToExcel function generates an excel file from a two-dimensional array and stores the data on the server.
| The Code is as follows: |
|
Require_once 'classes/PHPExcel/Reader/excel2007.php ';
Require_once 'classes/PHPExcel/Reader/excel5.php ';
Include 'classes/PHPExcel/IOFactory. php ';
Function arrayToExcel ($ data ){
$ ObjPHPExcel = new PHPExcel ();
$ ObjPHPExcel-> setActiveSheetIndex (0 );
$ ObjPHPExcel-> getActiveSheet ()-> setTitle ('firstsheet ');
$ ObjPHPExcel-> getDefaultStyle ()-> getFont ()-> setName ('arial ');
$ ObjPHPExcel-> getDefaultStyle ()-> getFont ()-> setSize (10 );
// Add data
$ I = 2;
Foreach ($ data as $ line ){
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('A'. $ I, $ line ['from']);
$ ObjPHPExcel-> getActiveSheet ()-> getCell ('A'. $ I)-> setDataType ('n ');
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('B'. $ I, $ line ['to']);
$ ObjPHPExcel-> getActiveSheet ()-> getCell ('B'. $ I)-> setDataType ('n ');
$ I ++;
}
$ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5 ');
$ File = 'excel.xls ';
$ ObjWriter-> save ($ file );
} |
If you do not want to save it on the server and want to download it directly to the client after generation, you can add the following code when outputting the file, instead of using $ objWriter-> save ($ file );
| The Code is as follows: |
|
|
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-execl ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");
Header ('content-Disposition: attachment; filename = "excel.xls "');
Header ("Content-Transfer-Encoding: binary ");
$ ObjWriter-> save ('php: // output '); |
Next, let's look at an example of reading Excel files:
In the following code, the excelToArray function is used to reorganize the content in an excel file and put it in an array.
| The Code is as follows: |
|
Require_once 'classes/PHPExcel. php ';
Require_once 'classes/PHPExcel/IOFactory. php ';
Function excelToArray ($ file ){
$ ObjReader = PHPExcel_IOFactory: createReader ('excel5 ');
$ ObjReader-> setReadDataOnly (true );
$ ObjPHPExcel = $ objReader-> load ($ file );
$ ObjWorksheet = $ objPHPExcel-> getActiveSheet ();
$ HighestRow = $ objWorksheet-> getHighestRow ();
$ HighestColumn = $ objWorksheet-> getHighestColumn ();
$ HighestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn );
$ ExcelData = array ();
For ($ row = 2; $ row <= $ highestRow; ++ $ row ){
For ($ col = 0; $ col <= $ highestColumnIndex; ++ $ col) {$ excelData [$ row] [] = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row)-> getValue ();
}
}
Return $ excelData;
} |
Simplified approach
| The Code is as follows: |
|
/**
*
* @ Copyright 2007-2012 Xiaoqiang.
* @ Author Xiaoqiang. Wu
* @ Version 1.01
*/
Error_reporting (E_ALL );
Date_default_timezone_set ('Asia/Shanghai ');
/** PHPExcel_IOFactory */
Require_once '../Classes/PHPExcel/IOFactory. php ';
// Check prerequisites
If (! File_exists ("31excel5.xls ")){
Exit ("not found 31excel5.xls. n ");
}
$ Reader = PHPExcel_IOFactory: createReader ('excel5'); // set to Excel5 format (Excel97-2003 workbook)
$ PHPExcel = $ reader-> load ("31excel5.xls"); // load an excel file
$ Sheet = $ PHPExcel-> getSheet (0); // read first ?? Worksheet
$ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
$ HighestColumm = $ sheet-> getHighestColumn (); // gets the total number of Columns
/** Read the data of each cell cyclically */
For ($ row = 1; $ row <= $ highestRow; $ row ++) {// The number of rows starts with 1st
For ($ column = 'a'; $ column <= $ highestColumm; $ column ++) {// The number of columns starts with column.
$ Dataset [] = $ sheet-> getCell ($ column. $ row)-> getValue ();
Echo $ column. $ row. ":". $ sheet-> getCell ($ column. $ row)-> getValue ()."
";
}
}
?> |