Home  >  Article  >  Backend Development  >  How to read excel files in phpexcel? Example of phpexcel reading xls file

How to read excel files in phpexcel? Example of phpexcel reading xls file

WBOY
WBOYOriginal
2016-07-25 08:51:371061browse
  1. error_reporting(E_ALL);
  2. date_default_timezone_set('Asia/ShangHai');
  3. /**PHPExcel_IOFactory*/
  4. require_once '../Classes/PHPExcel/IOFactory.php' ;
  5. // Check prerequisites
  6. if (!file_exists("31excel5.xls")) {
  7. exit("not found 31excel5.xls.n");
  8. }
  9. $reader = PHPExcel_IOFactory::createReader('Excel5' ); //Set to Excel5 format (Excel97-2003 workbook)
  10. $PHPExcel = $reader->load("31excel5.xls"); //Load excel file
  11. $sheet = $PHPExcel->getSheet( 0); // Read the first worksheet
  12. $highestRow = $sheet->getHighestRow(); // Get the total number of rows
  13. $highestColumm = $sheet->getHighestColumn(); // Get the total number of columns
  14. $highestColumm= PHPExcel_Cell::columnIndexFromString($colsNum); //Convert letter column to numeric column such as: AA becomes 27
  15. /**Loop to read the data of each cell*/
  16. for ($row = 1; $row <= $ highestRow; $row++){//The number of rows starts with row 1
  17. for ($column = 0; $column < $highestColumm; $column++) {//The number of columns starts with column 0
  18. $columnName = PHPExcel_Cell ::stringFromColumnIndex($column);
  19. echo $columnName.$row.":".$sheet->getCellByColumnAndRow($column, $row)->getValue()."
    ";
  20. }
  21. }
  22. ?>
Copy code

Example 2, Simplified method of reading excel files:

  1. error_reporting(E_ALL);
  2. date_default_timezone_set('Asia/ShangHai');
  3. /**PHPExcel_IOFactory*/
  4. require_once '../Classes/PHPExcel/IOFactory.php ';
  5. // Check prerequisites
  6. if (!file_exists("31excel5.xls")) {
  7. exit("not found 31excel5.xls.n");
  8. }
  9. $reader = PHPExcel_IOFactory::createReader('Excel5 '); //Set to Excel5 format (Excel97-2003 workbook)
  10. $PHPExcel = $reader->load("31excel5.xls"); //Load excel file
  11. $sheet = $PHPExcel->getSheet (0); // Read the first worksheet
  12. $highestRow = $sheet->getHighestRow(); // Get the total number of rows
  13. $highestColumm = $sheet->getHighestColumn(); // Get the total number of columns
  14. /**Loop to read the data of each cell*/
  15. for ($row = 1; $row <= $highestRow; $row++){//The number of rows starts with row 1
  16. for ($column = 'A'; $column <= $highestColumm; $column++) {//The number of columns starts with column A
  17. $dataset[] = $sheet->getCell($column.$row)->getValue();
  18. echo $ column.$row.":".$sheet->getCell($column.$row)->getValue()."
    ";
  19. }
  20. }
  21. ?>
Copy Code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn