I am new to php as well to phpExcel
. I just want to save post data to a existing excel sheet every time to a new row.
As i searched on Stackoverflow.com i got the reference of library phpExcel
.
I write down the following code from taking some samples.
<?php
/** Include PHPExcel */
require_once 'Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$row, $_POST['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$row, $_POST['email']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$row, $_POST['phone']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$row, $_POST['city']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$row, $_POST['kid1']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$row, $_POST['kid2']);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('myfile.xlsx');
?>
but the problem i am facing that i don't have idea how to append new row to excel sheet so every time posted data saved to new row.
I know my this code is only saving file to disk everytime with a single row, but i need to append new row to last excel sheet.
$num_rows = $objPHPExcel->getActiveSheet()->getHighestRow(); Following this, you can look into inserting a row by using the following statement: $objWorksheet->insertNewRowBefore($num_rows + 1, 1); This adds 1 new row before $num_rows .
PHP provides a library to deal with Excel files. It is called PHP Excel library. It enables you to read and write spreadsheets in various formats including csv, xls, ods, and xlsx. You will need to ensure that you have PHP's upgraded version not older than PHP 5.2 .
<?php
/** Include PHPExcel */
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load("myfile.xlsx");
$objPHPExcel->setActiveSheetIndex(0);
$row = $objPHPExcel->getActiveSheet()->getHighestRow()+1;
//echo $row;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$row, $_POST['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$row, $_POST['email']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$row, $_POST['phone']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$row, $_POST['city']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$row, $_POST['kid1']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$row, $_POST['kid2']);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('myfile.xlsx');
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With