1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 使用phpexcel在excel文件中插入新的数据

使用phpexcel在excel文件中插入新的数据

时间:2020-03-16 02:18:57

相关推荐

使用phpexcel在excel文件中插入新的数据

使用phpexcel在excel文件中插入新的数据

摘要:在开发中,我们经常需要读写excel表格。今天开发了一下读excel表格,然后使用 insertNewRowBefore 方法插入新行,生成新的表格。

代码如下,亲测好用:

/*** 在已有excel表中插入数据例子*/public function readyExcel($filename){vendor("PHPExcel.PHPExcel");$inputFileName = $filename;//excel文件路径date_default_timezone_set('PRC');// 读取excel文件try {$inputFileType = \PHPExcel_IOFactory::identify($inputFileName);$objReader = \PHPExcel_IOFactory::createReader($inputFileType);$objPHPExcel = $objReader->load($inputFileName);} catch(\Exception $e) {die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());}$array = [['A','B','C','D','E','F'],['A1','B1','C1','D1','E1','F1'],['A2','B2','C2','D2','E2','F2']];$baseRow=17;//指定插入到第17行后foreach($array as $index=>$dataRow){$row= $baseRow +$index; //$row是循环操作行的行号$objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1); //在操作行的号前加一空行,这空行的行号就变成了当前的行号//对应的列都附上数据和编号$objPHPExcel->getActiveSheet()->setCellValue( 'A'.$row,$dataRow[0]);$objPHPExcel->getActiveSheet()->setCellValue( 'B'.$row,$dataRow[1]);$objPHPExcel->getActiveSheet()->setCellValue( 'C'.$row,$dataRow[2]);$objPHPExcel->getActiveSheet()->setCellValue( 'D'.$row,$dataRow[3]);$objPHPExcel->getActiveSheet()->setCellValue( 'E'.$row, $dataRow[4]);$objPHPExcel->getActiveSheet()->setCellValue( 'F'.$row, $dataRow[5]);}ob_end_clean();//清除缓存区,解决乱码问题header('Content-Type: application/vnd.ms-excel');header('Content-Disposition: attachment;filename="Bill.xls"');header('Cache-Control: max-age=0');$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');$objWriter->save('php://output');exit;}

​分享不易,点赞关注给作者一点点鼓励🤓🤓,你的点赞让更多人获得帮助。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。