My problem is that I try to delete multiple rows in a loop. I want to delete only '开发者_如何学Gogood' rows and leave bad ones with marks. It looks like this:
$this->writeCellErrorData($objPHPExcel);//write coments a to cell and set bg to red
//get unique rows array
foreach($this->celConstraintViolation as $columnNum=>$row){
foreach($row as $rowNum=>$data){
if(!in_array($rowNum, $this->errorRowsArr)){
$this->errorRowsArr[]=$rowNum;
}
}
}
sort($this->errorRowsArr);
$this->errorRowsArr= array_reverse($this->errorRowsArr);
for($i=$this->celArrRowNum; $i>1; $i--){ //reverse loop
if(!in_array($i, $this->errorRowsArr)){
$objPHPExcel->getActiveSheet()->removeRow($i,1); //remove rows
}
}
after that I save the file. While the bg color is where it should be, it seems that comments are where they were before the removal. Is there a way to remove rows so that comments move where they should?
To add comments I use this line:$objPHPExcel->getActiveSheet()->
getComment(array_search($columnNum, $this->header).$rowNum)->getText()->
createTextRun($this->cellErrorMessages[$data[0]]);
Try this code, Its worked me:
$filename = 'path/example.xls';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$row_id = 1; // deleted row id
$number_rows = 2; // number of rows count
if ($objWorksheet != NULL) {
if ($objWorksheet->removeRow($row_id, $number_rows)) {
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($filename;
}
}
精彩评论