phpexcel中转换数组

143 阅读1分钟

1,在导入一个复杂的文件的时候,需要先转换成数组 ,然后遍历插入更新结果

$objPHPExcel = $objReader->load($file_path); //获取excel文件

$currentSheet = $objPHPExcel->getSheet(0);
//获取总列数
$allColumn = $currentSheet->getHighestColumn();
//获取总行数
$allRow = $currentSheet->getHighestRow();
/*if($allRow > 22){
    $allRow = 22;
}*/
//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
    //从哪列开始,A表示第一列
    for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
        //数据坐标
        $address = $currentColumn . $currentRow;
        //读取到的数据,保存到数组$data中
        $cell = $currentSheet->getCell($address)->getValue();
        if ($cell instanceof PHPExcel_RichText) {
            $cell = $cell->__toString();
        }
        $data[$currentRow - 1][$currentColumn] = $cell;
          //print_r($cell);
    }
}

var_dump($data);

image.png

2,这里二维数组遍历 取到流水号