欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

phpexcel读取excel文件的二种方法

程序员文章站 2022-05-13 16:18:12
...
  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 for ($column = 0; $column $columnname = phpexcel_cell::stringfromcolumnindex($column);
  23. echo $columnname.$row.":".$sheet->getcellbycolumnandrow($column, $row)->getvalue()."
    ";
  24. }
  25. }
  26. ?>
复制代码

例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 for ($column = 'a'; $column $dataset[] = $sheet->getcell($column.$row)->getvalue();
  22. echo $column.$row.":".$sheet->getcell($column.$row)->getvalue()."
    ";
  23. }
  24. }
  25. ?>
复制代码

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