How does phpexcel Read excel files? Example of using phpexcel to read xls files

Source: Internet
Author: User
How does phpexcel Read excel files? Example of using phpexcel to read xls 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 an excel file
  11. $ Sheet = $ PHPExcel-> getSheet (0); // read the first worksheet
  12. $ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
  13. $ HighestColumm = $ sheet-> getHighestColumn (); // gets the total number of columns
  14. $ HighestColumm = PHPExcel_Cell: columnIndexFromString ($ colsNum); // Convert a letter column to a numeric column, for example, AA to 27
  15. /** Read the data of each cell cyclically */
  16. For ($ row = 1; $ row <= $ highestRow; $ row ++) {// The number of rows starts with 1st
  17. For ($ column = 0; $ column <$ highestColumm; $ column ++) {// The number of columns starts with 0th
  18. $ ColumnName = PHPExcel_Cell: stringFromColumnIndex ($ column );
  19. Echo $ columnName. $ row. ":". $ sheet-> getCellByColumnAndRow ($ column, $ row)-> getValue ()."
    ";
  20. }
  21. }
  22. ?>

Example 2,Simplified Method for 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 an excel file
  11. $ Sheet = $ PHPExcel-> getSheet (0); // read the first worksheet
  12. $ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
  13. $ HighestColumm = $ sheet-> getHighestColumn (); // gets the total number of columns
  14. /** Read the data of each cell cyclically */
  15. For ($ row = 1; $ row <= $ highestRow; $ row ++) {// The number of rows starts with 1st
  16. For ($ column = 'a'; $ column <= $ highestColumm; $ column ++) {// The number of columns starts with column.
  17. $ Dataset [] = $ sheet-> getCell ($ column. $ row)-> getValue ();
  18. Echo $ column. $ row. ":". $ sheet-> getCell ($ column. $ row)-> getValue ()."
    ";
  19. }
  20. }
  21. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.