分享一个phpexcel类库读取excel文件的例子,学习下phpexcel的用法。
转自: http://www.jbxue.com/article/phpexcel_Xc4Hp2u7Hv1.html
首先,下载phpexcel类库: http://phpexcel.codeplex.com/ 。
然后,在代码中要包含PHPExcel类库文件,不确定文件类型时,可以使用PHPExcel_IOFactory::identify方法返回文件的类型,传递给该函数一个文件名即可。
转自: http://www.jbxue.com/article/phpexcel_Xc4Hp2u7Hv1.html
首先,下载phpexcel类库: http://phpexcel.codeplex.com/ 。
然后,在代码中要包含PHPExcel类库文件,不确定文件类型时,可以使用PHPExcel_IOFactory::identify方法返回文件的类型,传递给该函数一个文件名即可。
然后,根据返回的文件类型创建该类型的读取对象,进行文件的load。
最后,进行excel数据的读取即可。
有关phpexcel的更详细用法,请参考文档:
phpexcel快速开发指南
phpExcel中文帮助手册
实例代码:
复制代码代码示例:
<?php
/**
* PHPExcel读取excel文件
* site: www.jbxue.com
*
*/
require_once('include/common.inc.php');
require_once(ROOTPATH . 'include/phpExcel/PHPExcel/IOFactory.php');
$filePath = './file/xls/110713.xls';
$fileType = PHPExcel_IOFactory::identify($filePath); //文件名自动判断文件类型
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($filePath);
$currentSheet = $objPHPExcel->getSheet(0); //第一个工作簿
$allRow = $currentSheet->getHighestRow(); //行数
$output = array();
$preType = '';
$qh = $currentSheet->getCell('A4')->getValue();
//按照文件格式从第7行开始循环读取数据
for($currentRow = 7;$currentRow<=$allRow;$currentRow++){
//判断每一行的B列是否为有效的序号,如果为空或者小于之前的序号则结束
$xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
if(empty($xh))break;
$tmpType = (string)$currentSheet->getCell('C'.$currentRow)->getValue(); //赛事类型
if(!empty($tmpType))$preType = $tmpType;
$output[$xh]['type'] = $preType;
$output[$xh]['master'] = $currentSheet->getCell('F'.$currentRow)->getValue(); //主队
$output[$xh]['guest'] = $currentSheet->getCell('H'.$currentRow)->getValue(); //客队
}
//从当前行开始往下循环,取出第一个不为空的行
for( ; ; $currentRow++){
$xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
if(!empty($xh))break;
}
for( ; $currentRow <= $allRow; $currentRow++){
$xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
if(empty($xh))break;
$output[$xh]['rq'] = $currentSheet->getCell('I'.$currentRow)->getValue();
}
header("content-type:text/html; charset=utf-8");
echo '期号:' . $qh . "\n\n";
if(!empty($output)){
printf("%-5s\t%-15s\t%-40s\t%-40s\t%-5s\n", '序号', '赛事类型', '主队', '客队', '让球值');
foreach($output as $key => $row){
$format = "%-5d\t%-15s\t%-40s\t%-40s\t%-5s\n";
printf($format, $key, $row['type'], $row['master'], $row['guest'], $row['rq']);
}
}
?>
/**
* PHPExcel读取excel文件
* site: www.jbxue.com
*
*/
require_once('include/common.inc.php');
require_once(ROOTPATH . 'include/phpExcel/PHPExcel/IOFactory.php');
$filePath = './file/xls/110713.xls';
$fileType = PHPExcel_IOFactory::identify($filePath); //文件名自动判断文件类型
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($filePath);
$currentSheet = $objPHPExcel->getSheet(0); //第一个工作簿
$allRow = $currentSheet->getHighestRow(); //行数
$output = array();
$preType = '';
$qh = $currentSheet->getCell('A4')->getValue();
//按照文件格式从第7行开始循环读取数据
for($currentRow = 7;$currentRow<=$allRow;$currentRow++){
//判断每一行的B列是否为有效的序号,如果为空或者小于之前的序号则结束
$xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
if(empty($xh))break;
$tmpType = (string)$currentSheet->getCell('C'.$currentRow)->getValue(); //赛事类型
if(!empty($tmpType))$preType = $tmpType;
$output[$xh]['type'] = $preType;
$output[$xh]['master'] = $currentSheet->getCell('F'.$currentRow)->getValue(); //主队
$output[$xh]['guest'] = $currentSheet->getCell('H'.$currentRow)->getValue(); //客队
}
//从当前行开始往下循环,取出第一个不为空的行
for( ; ; $currentRow++){
$xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
if(!empty($xh))break;
}
for( ; $currentRow <= $allRow; $currentRow++){
$xh = (int)$currentSheet->getCell('B'.$currentRow)->getValue();
if(empty($xh))break;
$output[$xh]['rq'] = $currentSheet->getCell('I'.$currentRow)->getValue();
}
header("content-type:text/html; charset=utf-8");
echo '期号:' . $qh . "\n\n";
if(!empty($output)){
printf("%-5s\t%-15s\t%-40s\t%-40s\t%-5s\n", '序号', '赛事类型', '主队', '客队', '让球值');
foreach($output as $key => $row){
$format = "%-5d\t%-15s\t%-40s\t%-40s\t%-5s\n";
printf($format, $key, $row['type'], $row['master'], $row['guest'], $row['rq']);
}
}
?>
您可能感兴趣的文章:
PHPExcel常用方法举例
PHP导出EXCEL的简单范例 使用phpexcel类库导出excel
phpExcel类的使用方法分享
phpexcel导出excel的经典实例
phpexcel类库实例 支持(excel2003 excel2007)
phpexcel导出数据的实例代码
phpexcel导入excel到数据库的代码
使用PHPExcel判别和格式化Excel中的日期格式的例子
phpexcel导出excel的颜色与网页中颜色不一致的解决方法
CI中使用PHPExcel导出数据到Excel