PHPExcel - 如何插入行对象

时间:2017-03-13 13:02:02

标签: php phpexcel

我想找到A列为' 1'的行,然后将其复制到另一张表。现在我可以获得匹配行对象,但是如何使用对象插入到具有所有列样式和值的另一个工作表? (样式表示宽度,高度,背景颜色等)

这是我的代码:

foreach ($objPHPExcel->getActiveSheet()->getRowIterator() => $row) {
    foreach ($row->getCellIterator() as $column => $cell) {
        if ($column == 'A' && $cell->getValue() == '1') {
            // how to insert this row object to sheet index 1
        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这段代码:

foreach ($objPHPExcel->getActiveSheet()->getRowIterator() => $row) {
    foreach ($row->getCellIterator() as $column => $cell) {
        if ($column == 'A' && $cell->getValue() == '1') {
            // how to insert this row object to sheet index 1
            $activeSheet = $objPHPExcel->setActiveSheetIndex(1);
            $activeSheet->setCellValue("CELL_INDEX","VALUE");

        }
    }
}

答案 1 :(得分:0)

我认为以下代码适合您。我没有测试它,但我认为这种逻辑会起作用。试试看。

$row_index = 1;
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
foreach ($rowIterator as $row) {
    foreach ($row->getCellIterator() as $column => $cell) {
        if ($column == 'A' && $cell->getValue() == '1') {
            $objPHPExcel->setActiveSheetIndex(1); //Set index to sheet at position 1
            foreach ($row->getCellIterator() as $column => $cell) {
                $objPHPExcel->getActiveSheet()->setCellValue($column.$row_index, $cell->getValue()); //Write the data of the row to sheet 1
            }
            $row_index++;
            break; //Break and check the next row.
        }

    }
}