PHPExcel导入多sheet表数据超过Z列(大于26列)如何查询

发布时间:2020-08-20 13:25:53编辑:丝画阁阅读(916)

将列的数字序号转成字母使用,代码如下:

$i=0;

PHPExcel_Cell::stringFromColumnIndex($i);

打印出来。结果是A

将列的字母转成数字序号使用,代码如下:

PHPExcel_Cell::columnIndexFromString('AA');

这个是27.

用这种方法。能解决。这里需要include_once('PHPExcel.class.php');这是当然的。读取和写入excl的方法。请自行上网搜索。有这个类


/**
* 安装excel处理类 composer require phpoffice/phpexcel
* 导入excel获取excel的数据
* @return  hink
esponseJson
* @throws PHPExcel_Exception
* @throws PHPExcel_Reader_Exception
* Created on 2018/12/27 0:05
* Created by Dh
*/
public function importExcel()
{
return $this->apiSuc(ActionExcel::importExcelGetArray(request()->file()));
}
/**
* 逻辑处理
* 读取excel文件内容输出数组
* @param $data
* @return array
* @throws PHPExcel_Exception
* @throws PHPExcel_Reader_Exception
* Created on 2018/12/27 0:00
* Created by Dh
*/
public static function importExcelGetArray($data)
{
$obj = $data['file'];
//上传文件
$info = $obj->move(ROOT_PATH . 'public' . DS . 'uploads'. DS . 'file');
$filePath = $info->getPathname();
//文件名自动判断文件类型
$fileType = PHPExcel_IOFactory::identify($filePath);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($filePath);
$sheetIndex = $objPHPExcel->getSheetCount();
$resultArray = [];
for ($i = 0;$i < $sheetIndex;$i++) {
$sheetObj = $objPHPExcel->getSheet($i);
//最大行
$highestRow = $sheetObj->getHighestRow();
//最大列
$highestColumn = $sheetObj->getHighestColumn();
for ($row = 1; $row <= $highestRow; $row++){
/ /读取每一行的代码到一个数组中
$rowData = $sheetObj->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
$resultArray['Sheet_' . $i][$row] = $rowData[0];
}
}
return $resultArray;

}


重新整理一下:

if($suffix=="xlsx"){
$reader = \PHPExcel_IOFactory::createReader('Excel2007');
}elseif($suffix=="xls"){
$reader = \PHPExcel_IOFactory::createReader('Excel5');
}else{
ajax_json('请上传EXCEL文件!',2);
}

$objContent = $reader->load($files['tmp_name']);
//$sheetContent = $objContent->getSheet(0)->toArray();

//查询有效站点数量
$sheetnum=$objContent->getSheetCount();
$returnarr = array();

for($i=0;$i<$sheetnum;$i++){

$currentSheet=$objContent->getSheet($i);
//获取总列数
$allColumn=$currentSheet->getHighestColumn();
//获取总行数
$allRow=$currentSheet->getHighestRow();

//容错 sheet获取总列数出错.
if($allColumn !='AD'){
$allColumn='AD';
}

$columnum = \PHPExcel_Cell::columnIndexFromString($allColumn);


//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for($currentRow=2;$currentRow<=$allRow;$currentRow++){
$row = array();
//从哪列开始,A表示第一列
for($currentColumn=0;$currentColumn<$columnum;$currentColumn++){

$currentColumna = \PHPExcel_Cell::stringFromColumnIndex($currentColumn);

//数据坐标
$address=$currentColumna.$currentRow;
//读取到的数据,保存到数组$arr中
$cell =$currentSheet->getCell($address)->getValue();
//$cell = $data[$currentRow][$currentColumn];
if($cell instanceof \PHPExcel_RichText){
$cell  = $cell->__toString();
}

if(!empty($cell)){
//对物殊字符过滤
$row[$currentColumna] = $cell;
}else{
$row[$currentColumna] = $cell;
}

}
$returnarr[$i."_".$currentRow] = $row;
}
}

关键字