1.下载phpexcel 放到可引用的地方。/**
* 解析 xlsx
* @ function fdmodule_parseXls
* @ param $file xls 文件
* @ return array
*/
function fdmodule_parseXls($file,$sheet=0,$maxC=false) {
set_time_limit(0);
ini_set('memory_limit', '512M');
require_once dirname(__DIR__) . '/PHPExcel/IOFactory.php';
if(!file_exists($file)) { //文件不存在
echo 'no file';exit;
}
$PHPExcel = PHPExcel_IOFactory::load($file); //获取objexcel有多种方式,也可尝试其他形式
$currentSheet = $PHPExcel->getSheet($sheet); //获取第一个sheet 工作簿
$allColumn = !empty($maxC)? $maxC : $currentSheet->getHighestColumn(); //列数
$allRow = $currentSheet->getHighestRow(); //行数
$data = array();
for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
if ($currentRow == 1) { //第一行为表头
$allColumn = getOrd($allColumn);
for($currentColumn='A'; getOrd($currentColumn) <= $allColumn ; $currentColumn++){
$address = $currentColumn.$currentRow;
$keys[$currentColumn] = trim($currentSheet->getCell($address)->getValue());
}
$basekey = array_flip($keys); //like: array('书名'=>'A','作者'=>'B'...)
}
else { //第二行起为数据
$tmpdata = null;
foreach($basekey as $k => $v) {
$address = $v.$currentRow;
$tmpdata[$k] = trim($currentSheet->getCell($address)->getValue());
}
$data[] = $tmpdata; //压每行数据到data里面
//file_put_contents("/tmp/fd.log",var_export($tmpdata,true),FILE_APPEND);
}
}
$PHPExcel = $currentSheet = null;
return $data;
}
技术ASCII
/*** 返回计算过后的 ascii
*/
function getOrd($str) {
if(strlen($str) == 2) {
$ord = ord($str[0]).ord($str[1]);
} else {
$ord = ord($str[0]);
}
return $ord;
}