当前位置:Gxlcms > PHP教程 > php导入excelphp使用phpexcel导入excel文件

php导入excelphp使用phpexcel导入excel文件

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

  1. //载入PHPExcel类

  2. include(dirname(__FILE__).'/phpexcel/PHPExcel.php');

  3. $Obj = new PHPExcel_Reader_Excel5();

  4. $Obj->setReadDataOnly(true);

  5. //读取demo.xls文件

  6. $phpExcel = $Obj->load(dirname(__FILE__).'/output.xls');

  7. //获取当前活动sheet

  8. $objWorksheet = $phpExcel->getActiveSheet();

  9. //获取行数

  10. $highestRow = $objWorksheet->getHighestRow();

  11. //获取列数

  12. //by bbs.it-home.org
  13. $highestColumn = $objWorksheet->getHighestColumn();
  14. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

  15. //循环输出数据

  16. $data = array();
  17. for($row = 1; $row <= $highestRow; ++$row) {
  18. for($col = 0; $col < $highestColumnIndex; ++$col) {
  19. $val = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
  20. $data[$row][$col] = trim($val);
  21. }
  22. }

  23. echo '

    ';
  24. print_r($data);
  25. echo '
  26. ';

人气教程排行