Phpexcel How to read Excel files? Phpexcel example of reading an XLS file

Source: Internet
Author: User
  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 in EXCEL5 format (excel97-2003 workbook)
  10. $PHPExcel = $reader->load ("31excel5.xls"); Loading Excel files
  11. $sheet = $PHPExcel->getsheet (0); Read the first worksheet
  12. $highestRow = $sheet->gethighestrow (); Total number of rows obtained
  13. $highestColumm = $sheet->gethighestcolumn (); Total number of columns obtained
  14. $highestColumm = phpexcel_cell::columnindexfromstring ($colsNum); The letter column is converted to a number column such as: AA to 27
  15. /** cycle through the data of each cell */
  16. for ($row = 1; $row <= $highestRow; $row + +) {//lines start at line 1th
  17. for ($column = 0; $column < $highestColumm; $column + +) {//Number of columns starts with column NO. 0
  18. $columnName = Phpexcel_cell::stringfromcolumnindex ($column);
  19. Echo $columnName. $row. ":". $sheet->getcellbycolumnandrow ($column, $row)->getvalue (). "
    ";
  20. }
  21. }
  22. ?>
Copy Code

Example 2, a simplified way to read an Excel file :

  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 in EXCEL5 format (excel97-2003 workbook)
  10. $PHPExcel = $reader->load ("31excel5.xls"); Loading Excel files
  11. $sheet = $PHPExcel->getsheet (0); Read the first worksheet
  12. $highestRow = $sheet->gethighestrow (); Total number of rows obtained
  13. $highestColumm = $sheet->gethighestcolumn (); Total number of columns obtained
  14. /** cycle through the data of each cell */
  15. for ($row = 1; $row <= $highestRow; $row + +) {//lines start at line 1th
  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
  • 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.