getsheet(0); // 读取第一個工作表$highestrow = $sheet->gethighestrow(); //">
当前位置:Gxlcms > PHP教程 > phpexcel读取excel文件的二种方法

phpexcel读取excel文件的二种方法

时间:2021-07-01 10:21:17 帮助过:13人阅读

  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'); //设置以excel5格式(excel97-2003工作簿)
  16. $phpexcel = $reader->load("31excel5.xls"); // 载入excel文件
  17. $sheet = $phpexcel->getsheet(0); // 读取第一個工作表
  18. $highestrow = $sheet->gethighestrow(); // 取得总行数
  19. $highestcolumm = $sheet->gethighestcolumn(); // 取得总列数
  20. $highestcolumm= phpexcel_cell::columnindexfromstring($colsnum); //字母列转换为数字列 如:aa变为27
  21. /** 循环读取每个单元格的数据 */
  22. for ($row = 1; $row <= $highestrow; $row++){//行数是以第1行开始
  23. for ($column = 0; $column < $highestcolumm; $column++) {//列数是以第0列开始
  24. $columnname = phpexcel_cell::stringfromcolumnindex($column);
  25. echo $columnname.$row.":".$sheet->getcellbycolumnandrow($column, $row)->getvalue()."
    ";
  26. }
  27. }
  28. ?>

例2,phpexcel读取excel文件的精简方法。

  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'); //设置以excel5格式(excel97-2003工作簿)
  16. $phpexcel = $reader->load("31excel5.xls"); // 载入excel文件
  17. $sheet = $phpexcel->getsheet(0); // 读取第一個工作表
  18. $highestrow = $sheet->gethighestrow(); // 取得总行数
  19. $highestcolumm = $sheet->gethighestcolumn(); // 取得总列数
  20. /** 循环读取每个单元格的数据 */
  21. for ($row = 1; $row <= $highestrow; $row++){//行数是以第1行开始
  22. for ($column = 'a'; $column <= $highestcolumm; $column++) {//列数是以a列开始
  23. $dataset[] = $sheet->getcell($column.$row)->getvalue();
  24. echo $column.$row.":".$sheet->getcell($column.$row)->getvalue()."
    ";
  25. }
  26. }
  27. ?>

以上就是phpexcel读取文件的方法与实例,phpexcel最新版下载,请访问phpexcel官网地址:http://phpexcel.codeplex.com/

人气教程排行