Adding a new row with PHPExcel?

前端 未结 2 1678
执念已碎
执念已碎 2020-12-18 21:41

How can I add a new row to an existing .xls file using PHPExcel?

Do I have to calculate the number of rows that already exist?

If so, how can I do that for a

2条回答
  •  既然无缘
    2020-12-18 21:45

    The example above only adds a blank row. The example below adds data coming from a form.

    getActiveSheet();
    
            //add the new row
            $num_rows = $objPHPExcel->getActiveSheet()->getHighestRow();
            $objWorksheet->insertNewRowBefore($num_rows + 1, 1);
            $name = isset($_POST['name']) ? $_POST['name'] : '';
            if($submit){
        //SAVING THE NEW ROW - on the last position in the table
            $objWorksheet->setCellValueByColumnAndRow(0,$num_rows+1,$name);
            }
    
            //display the table
            echo ''."\n";
            echo ''."\n";
            echo ''."\n";
            foreach ($objWorksheet->getRowIterator() as $row) {
            echo ''."\n";
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(false);
            foreach ($cellIterator as $cell) {
            echo ''."\n";
            }
            echo ''."\n";
            }
            echo ''."\n";
            echo '
    Company Name
    '.$cell->getValue().'
    '."\n"; ?>

提交回复
热议问题