PHPExcel 对xls表格的文件读取

//先去http://phpexcel.codeplex.com/releases/view/119187 下载文件包最后解压Classes到当前目录
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ALL);
date_default_timezone_set('Asia/ShangHai');
require_once './Classes/PHPExcel/IOFactory.php';
// Check prerequisites
if (!file_exists("ssc_user.xls")) {
exit("not found ssc_user.xls.\n");
}
$patch="ssc_user.xls";
$reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
$PHPExcel = $reader->load($patch); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
/** 循环读取每个单元格的数据 */
$arr=array();
for($row = 1; $row <= $highestRow; $row++)
{
$number='B'.$row;//列数用户名
$name=$sheet->getCell($number)->getValue();
$number='A'.$row;//列数用户名id
$uid=$sheet->getCell($number)->getValue();
$arr[$name]=$uid;
}

 //如果在读取文件中有遇到时间可用以下函数解决

//时间转换
function excelTime($date, $time = false) {
if(function_exists('GregorianToJD'))
{
if (is_numeric( $date ))
{
$jd = GregorianToJD( 1, 1, 1970 );
$gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
$date = explode( '/', $gregorian );
$date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT ). ($time ? " 00:00:00" : '');return $date_str;
}

}else{
$date=$date>25568?$date+1:25569;
/*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
$ofs=(70 * 365 + 17+2) * 86400;$date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
}
return $date;
}

posted @ 2016-07-11 00:36  二年后的今天  阅读(226)  评论(0编辑  收藏  举报