PHPExcel读取数据之数据排列
程序员文章站
2022-03-09 23:13:45
...
//核对返回数据
public function actionChecking()
{
if(Yii::$app->request->isPost){
$post = Yii::$app->request->post();
if (isset($_FILES['batchFile']) && $_FILES['batchFile']['error'] == 0) {
spl_autoload_unregister(array('YiiBase', 'autoload')); //注销YII的自动加载,采用手动导入,PHPexcel有自己的加载功能
$files = $_FILES['batchFile']['tmp_name'];
$objPHPExcel = new \PHPExcel();
$fileType = \PHPExcel_IOFactory::identify($files); //文件名自动判断文件类型
$excelReader = \PHPExcel_IOFactory::createReader($fileType);
$phpexcel = $excelReader->load($files)->getActiveSheet();//载入文件并获取活动表
$total_line = $phpexcel->getHighestRow();//总行数
$total_column = $phpexcel->getHighestColumn();//总列数
$_arr = ['市场管理','线索录入','销售部门','技术方案','飞行部','项目实施','硬件研发','软件研发'];
$hardRate = 0;//硬件占比
$softRate = 0;//软件占比
//三维数组取出数值
if($total_line > 1) {
$dataArr = array();
$rowNum = 0;
$oneKey = '';
for ($row = 6; $row <= $total_line; $row++) {
$rowNum++;
$data = array();
//取每一列的值
for ($column = 'A'; $column <= $total_column; $column++) {
$val = trim($phpexcel->getCell($column . $row)->getValue());
if (in_array($val, $_arr)) {
$val == '硬件研发' ? $hardRate = trim($phpexcel->getCell('F' . $row)->getValue()) : '';
$val == '软件研发' ? $softRate = trim($phpexcel->getCell('F' . $row)->getValue()) : '';
$oneKey = $val;
break;
} else {
if(!empty($val))
$data[$val] = trim($phpexcel->getCell(++$column . $row)->getValue());
}
}
if(!empty($data))
$dataArr[$oneKey][] = $data;
}
}
//软硬件综合占比 == 100%(1)
$totalRate = $hardRate + $softRate;
if(intval($totalRate) == 1){
return ['code'=>-1,'status'=>'fail','message' => '软硬件研发占比不等于100%'];
}
}
}
}
数据排列方式: