php phpexcel 读,PHP phpexcel读取xls文件内容

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值