您的位置 首页 php

「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):

错误提示列即改良列:

之前:

之后:

文章来源:智云一二三科技

文章标题:「PHPExcel」给模板文件写入数据

文章地址:https://www.zhihuclub.com/152183.shtml

关于作者: 智云科技

热门文章

网站地图