JeanKininho
JeanKininho

Reputation: 89

Append new row to Excel file with PHPExcel

I have this code

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);
    $LastRow = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
    $row = $LastRow + 1;

    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row, $_POST['name']);
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$row, $_POST['email'] );
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$row, $_POST['tel']);
    $objPHPExcel->getActiveSheet()->SetCellValue('D'.$row, $_POST['adresss']);

    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

    $objWriter->save('files\Clientes.xlsx');  

The problem in here is the always create a new .xlsx file or erase the others rows that are in Excel File, i need Add a new row... Any ideas ?

Upvotes: 1

Views: 3038

Answers (2)

asdawrqtgf
asdawrqtgf

Reputation: 640

$objPHPExcel = $objReader->load("FILE.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();

Just open the file

Upvotes: 2

SnareChops
SnareChops

Reputation: 13347

In your current code you are creating a new PHPExcel() object each time. You need to load the existing, then insert a new row. See here for how: Adding a new row with PHPExcel?

Upvotes: 1

Related Questions