How does phpexcel Read excel files? Example of using phpexcel to read xls files
- 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 the first worksheet
- $ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
- $ HighestColumm = $ sheet-> getHighestColumn (); // gets the total number of columns
- $ HighestColumm = PHPExcel_Cell: columnIndexFromString ($ colsNum); // Convert a letter column to a numeric column, for example, AA to 27
-
- /** Read the data of each cell cyclically */
- For ($ row = 1; $ row <= $ highestRow; $ row ++) {// The number of rows starts with 1st
- For ($ column = 0; $ column <$ highestColumm; $ column ++) {// The number of columns starts with 0th
- $ ColumnName = PHPExcel_Cell: stringFromColumnIndex ($ column );
- Echo $ columnName. $ row. ":". $ sheet-> getCellByColumnAndRow ($ column, $ row)-> getValue ()."
";
- }
- }
- ?>
Example 2,Simplified Method for reading excel files:
- 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 the 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 ()."
";
- }
- }
- ?>
|