当前位置:Gxlcms > PHP教程 > phpexcel类库读取excel文件的例子

phpexcel类库读取excel文件的例子

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

  1. require_once('include/common.inc.php');
  2. require_once(rootpath . 'include/phpexcel/phpexcel/iofactory.php');
  3. $filepath = './file/xls/110713.xls';
  4. $filetype = phpexcel_iofactory::identify($filepath); //文件名自动判断文件类型
  5. $objreader = phpexcel_iofactory::createreader($filetype);
  6. $objphpexcel = $objreader->load($filepath);
  7. $currentsheet = $objphpexcel->getsheet(0); //第一个工作簿
  8. $allrow = $currentsheet->gethighestrow(); //行数
  9. $output = array();
  10. $pretype = '';
  11. #// bbs.it-home.org
  12. $qh = $currentsheet->getcell('a4')->getvalue();
  13. //按照文件格式从第7行开始循环读取数据
  14. for($currentrow = 7;$currentrow<=$allrow;$currentrow++){
  15. //判断每一行的b列是否为有效的序号,如果为空或者小于之前的序号则结束
  16. $xh = (int)$currentsheet->getcell('b'.$currentrow)->getvalue();
  17. if(empty($xh))break;
  18. $tmptype = (string)$currentsheet->getcell('c'.$currentrow)->getvalue(); //赛事类型
  19. if(!empty($tmptype))$pretype = $tmptype;
  20. $output[$xh]['type'] = $pretype;
  21. $output[$xh]['master'] = $currentsheet->getcell('f'.$currentrow)->getvalue(); //主队
  22. $output[$xh]['guest'] = $currentsheet->getcell('h'.$currentrow)->getvalue(); //客队
  23. }
  24. //从当前行开始往下循环,取出第一个不为空的行
  25. for( ; ; $currentrow++){
  26. $xh = (int)$currentsheet->getcell('b'.$currentrow)->getvalue();
  27. if(!empty($xh))break;
  28. }
  29. for( ; $currentrow <= $allrow; $currentrow++){
  30. $xh = (int)$currentsheet->getcell('b'.$currentrow)->getvalue();
  31. if(empty($xh))break;
  32. $output[$xh]['rq'] = $currentsheet->getcell('i'.$currentrow)->getvalue();
  33. }
  34. header("content-type:text/html; charset=utf-8");
  35. echo '期号:' . $qh . "\n\n";
  36. if(!empty($output)){
  37. printf("%-5s\t%-15s\t%-40s\t%-40s\t%-5s\n", '序号', '赛事类型', '主队', '客队', '让球值');
  38. foreach($output as $key => $row){
  39. $format = "%-5d\t%-15s\t%-40s\t%-40s\t%-5s\n";
  40. printf($format, $key, $row['type'], $row['master'], $row['guest'], $row['rq']);
  41. }
  42. }
  43. ?>

人气教程排行