phpexcel按顺序删除多行,并对单元格进行注释

时间:2011-08-30 06:58:02

标签: php phpexcel

我的问题是我尝试在循环中删除多行。我想只删除'好'行并留下带有标记的坏行。 它看起来像这样:

$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
            }
        }

之后我保存文件。虽然bg颜色应该是它应该的位置,但似乎注释是它们在删除之前的位置。有没有办法删除行,以便注释移动到应该的位置? 要添加评论,请使用以下行:

$objPHPExcel->getActiveSheet()->
getComment(array_search($columnNum, $this->header).$rowNum)->getText()->
createTextRun($this->cellErrorMessages[$data[0]]);

1 个答案:

答案 0 :(得分:0)

试试这段代码吧,我的工作是:

$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;
            }
        }