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

phpexcel类库读取excel文件的例子

程序员文章站 2022-04-13 16:13:22
...
  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 //判断每一行的b列是否为有效的序号,如果为空或者小于之前的序号则结束
  15. $xh = (int)$currentsheet->getcell('b'.$currentrow)->getvalue();
  16. if(empty($xh))break;
  17. $tmptype = (string)$currentsheet->getcell('c'.$currentrow)->getvalue(); //赛事类型
  18. if(!empty($tmptype))$pretype = $tmptype;
  19. $output[$xh]['type'] = $pretype;
  20. $output[$xh]['master'] = $currentsheet->getcell('f'.$currentrow)->getvalue(); //主队
  21. $output[$xh]['guest'] = $currentsheet->getcell('h'.$currentrow)->getvalue(); //客队
  22. }
  23. //从当前行开始往下循环,取出第一个不为空的行
  24. for( ; ; $currentrow++){
  25. $xh = (int)$currentsheet->getcell('b'.$currentrow)->getvalue();
  26. if(!empty($xh))break;
  27. }
  28. for( ; $currentrow $xh = (int)$currentsheet->getcell('b'.$currentrow)->getvalue();
  29. if(empty($xh))break;
  30. $output[$xh]['rq'] = $currentsheet->getcell('i'.$currentrow)->getvalue();
  31. }
  32. header("content-type:text/html; charset=utf-8");
  33. echo '期号:' . $qh . "\n\n";
  34. if(!empty($output)){
  35. printf("%-5s\t%-15s\t%-40s\t%-40s\t%-5s\n", '序号', '赛事类型', '主队', '客队', '让球值');
  36. foreach($output as $key => $row){
  37. $format = "%-5d\t%-15s\t%-40s\t%-40s\t%-5s\n";
  38. printf($format, $key, $row['type'], $row['master'], $row['guest'], $row['rq']);
  39. }
  40. }
  41. ?>
复制代码