【PHPExcel】给模板文件写入数据

代码如下:


在这里插入图片描述

//横向单元格标识 
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R');
$newName = time().rand(100,300).'.csv';
$source_file = FILENAME.'template/import/'.$file_name;
$destination = $error_path.$newName;
$startLine = 3; //从第三行开始写入数据
$objPHPExcel = \PHPExcel_IOFactory::load($source_file);
// print_r($objPHPExcel);die;
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$cols = count($testValue[0]);
$len = count($testValue);
// print_r($cols);die;
for ($i=0; $i < $len; $i++) { 
    for ($j = 0; $j < $cols; $j++) {
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$j] . ($i+$startLine), $testValue[$i][$j], \PHPExcel_Cell_DataType::TYPE_STRING);
    }
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter->save($destination);

改良版(2018/09/27):

在这里插入图片描述

错误提示列即改良列:
之前:
在这里插入图片描述
之后:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33055907/article/details/82868015