首页 > Web开发 > 详细

php读取excel文件数据

时间:2017-06-19 09:30:15      阅读:340      评论:0      收藏:0      [点我收藏+]
require_once $_SERVER[‘DOCUMENT_ROOT‘].‘/Classes/PHPExcel.php‘;
require_once $_SERVER[‘DOCUMENT_ROOT‘].‘/Classes/PHPExcel/IOFactory.php‘;

function getReadExcel($uploadfile,$time){
$extension = substr($uploadfile,strrpos($uploadfile,‘.‘)+1);
switch ($extension){
case ‘xlsx‘:{
$objReader = PHPExcel_IOFactory::createReader(‘Excel2007‘);/*excel2007 for 2007*/
}break;
case ‘xls‘:{
$objReader = PHPExcel_IOFactory::createReader(‘Excel5‘);/*Excel5 for 2003*/
}break;
case ‘csv‘:{
$objReader = PHPExcel_IOFactory::createReader(‘CSV‘);/*Csv for csv*/
}break;
}
$objPHPExcel = $objReader->load($uploadfile); //Excel 路径
$sheet = $objPHPExcel->getSheet(0);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数

for ($row = 1;$row <= $highestRow;$row++){
$strs=array();
//注意highestColumnIndex的列数索引从0开始
for ($col = 0;$col < $highestColumnIndex;$col++){
$strs[$col] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
if($extension == ‘csv‘){
$strs[$col] = iconv(‘gbk‘, ‘utf-8‘, $strs[$col]);
}
}
$data[] = $strs;
}
return $data;
}

php读取excel文件数据

原文:http://www.cnblogs.com/yuwenbo/p/7047182.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!