Home >Backend Development >PHP Tutorial >Two methods for reading excel files in phpexcel

Two methods for reading excel files in phpexcel

WBOY
WBOYOriginal
2016-07-25 08:53:051048browse
  1. /**
  2. *
  3. * @copyright 2007-2012 xiaoqiang.
  4. * @author xiaoqiang.wu
  5. * @version 1.01
  6. */
  7. error_reporting(e_all);
  8. date_default_timezone_set('asia/shanghai');
  9. /**phpexcel_iofactory*/
  10. require_once '. ./classes/phpexcel/iofactory.php';
  11. // check prerequisites
  12. if (!file_exists("31excel5.xls")) {
  13. exit("not found 31excel5.xls.n");
  14. }
  15. $reader = phpexcel_iofactory::createreader('excel5'); //Set to excel5 format (excel97-2003 workbook)
  16. $phpexcel = $reader->load("31excel5.xls"); //Load excel file
  17. $sheet = $phpexcel->getsheet(0); // Read the first worksheet
  18. $highestrow = $sheet->gethighestrow(); // Get the total number of rows
  19. $highestcolumm = $sheet-> gethighestcolumn(); // Get the total number of columns
  20. $highestcolumm= phpexcel_cell::columnindexfromstring($colsnum); //Convert the letter column to a numeric column such as: aa becomes 27
  21. /**Loop to read the data of each cell*/
  22. for ( $row = 1; $row <= $highestrow; $row++){//The number of rows starts from row 1
  23. for ($column = 0; $column < $highestcolumm; $column++) {//The number of columns It starts with column 0
  24. $columnname = phpexcel_cell::stringfromcolumnindex($column);
  25. echo $columnname.$row.":".$sheet->getcellbycolumnandrow($column, $row)->getvalue() ."
    ";
  26. }
  27. }
  28. ?>
Copy code

Example 2, a streamlined method for phpexcel to read excel files.

  1. /**
  2. *
  3. * @copyright 2007-2012 xiaoqiang.
  4. * @author xiaoqiang.wu
  5. * @version 1.01
  6. */
  7. error_reporting(e_all);
  8. date_default_timezone_set('asia/shanghai');
  9. /**phpexcel_iofactory*/
  10. require_once ' ../classes/phpexcel/iofactory.php';
  11. // check prerequisites
  12. if (!file_exists("31excel5.xls")) {
  13. exit("not found 31excel5.xls.n");
  14. }
  15. $reader = phpexcel_iofactory::createreader('excel5'); //Set to excel5 format (excel97-2003 workbook)
  16. $phpexcel = $reader->load("31excel5.xls"); //Load excel File
  17. $sheet = $phpexcel->getsheet(0); // Read the first worksheet
  18. $highestrow = $sheet->gethighestrow(); // Get the total number of rows
  19. $highestcolumm = $sheet-> ;gethighestcolumn(); // Get the total number of columns
  20. /**Loop to read the data of each cell*/
  21. for ($row = 1; $row <= $highestrow; $row++){//The number of rows is based on row 1 Start
  22. for ($column = 'a'; $column <= $highestcolumm; $column++) {//The number of columns starts with column a
  23. $dataset[] = $sheet->getcell($column.$row )->getvalue();
  24. echo $column.$row.":".$sheet->getcell($column.$row)->getvalue()."
    ";
  25. }
  26. }
  27. ?>
Copy code

The above are the methods and examples of phpexcel reading files. To download the latest version of phpexcel, please visit the phpexcel official website address: http://phpexcel.codeplex.com/



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