添加一行到多行PHPExcel [关闭]

I have a script that inputs form data on a row in an excel file. Each time the form is sent the new data is appended to the same excel file.

The problem is that the new data is appended by two rows. So a blank row is inserted between each row.

Here is the code. $data contains the $_POST data.

function generateExcel($data) {
    $filename = dirname(__FILE__).'/myexcelfile.xlsx';

    // this is shortened
    $columns = array(
        'fieldname1' => 'Column 1', 
        'fieldname2' => 'Column 2', 
    );

    // check if file exist
    if(file_exists($filename)) {

        // load existing excel file
        $objPHPExcel = PHPExcel_IOFactory::load($filename);

    } else {

        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();
        $objWorksheet = $objPHPExcel->getActiveSheet();

        $objWorksheet->insertNewRowBefore(1, 1);

        // Set column names
        $columnIndex = 0;
        foreach($columns as $columnName) {
            $objWorksheet->setCellValueByColumnAndRow($columnIndex, 1, $columnName);
            $columnIndex++;
        }
    }

    // get sheet and highest row
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $numberOfRows = $objPHPExcel->getActiveSheet()->getHighestRow();

    // Insert a new row after highest row
    $objWorksheet->insertNewRowBefore($numberOfRows + 1, 1);

    // Get the highest row again (should be one more since last time)
    // $numberOfRows2 = $objPHPExcel->getActiveSheet()->getHighestRow();

    // Insert data for each column
    $columnIndex = 0;
    foreach($columns as $fieldName => $columnName) {
        $fieldValue = $data[$fieldName];

        $objWorksheet->setCellValueByColumnAndRow($columnIndex, $numberOfRows + 1, $fieldValue);

        $columnIndex++;
    }

    // Save Excel 2007 file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save($filename);
}

This is what I used: https://stackoverflow.com/a/12417477

http://i.imgur.com/eYIIktZ.png

I just guess that, can we try this

$objWorksheet->setCellValueByColumnAndRow($columnIndex,"",$columnName);

instead of

$objWorksheet->setCellValueByColumnAndRow($columnIndex, 1, $columnName);