10

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.

5
  • 1
    Unless you want to insert a new row before an existing row, you don't need to worry. set cellValue() will add data at the cell you specify: if a cell value already exists, then it will be overwritten; if it doesn't exist, then it will be created. If you need to know the number of the last row, then use the worksheet's getHighestRow() method, or getHighestDataRow() method
    – Mark Baker
    May 13, 2013 at 6:31
  • I just want to append new row everytime when data is post and my excel fiel is already placed on server. means i don't want to lost old data. new data new row. May 13, 2013 at 6:32
  • 2
    In that case: load the file, call getHighestRow() to get the last row used, add one to that value, then write the new data to that new row number
    – Mark Baker
    May 13, 2013 at 6:33
  • @MarkBaker your suggestion helped me .Thanks Sep 20, 2013 at 7:03
  • Possible duplicate of Adding a new row with PHPExcel?
    – Simon East
    Jun 21, 2016 at 3:20

3 Answers 3

28
<?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');
?>
3

I had the same problem, and i found solution from fromArray method on sheet and the third parameter, startCell

have a look on PHP-Doc :

Worksheet::fromArray
    Fill worksheet from values in array 
Parameters:
    array $source
        Source array, default null
    mixed $nullValue
        Value in source array that stands for blank cell, default null
    string $startCell
        Insert array starting from this cell address as the top left coordinate, default 'A1'
    boolean $strictNullComparison
        Apply strict comparison when testing for null, default false

so, just :

<?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;
$rowData = array( 
    array( $_POST['name'], $_POST['email'], $_POST['phone'], 
    $_POST['city'], $_POST['kid1'], $_POST['kid2'] ) 
); //fromArray allow you multi-row append
$objPHPExcel->getActiveSheet()->fromArray($rowData, null, 'A'.$row);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('myfile.xlsx');
?>
1
 <?php 
  require_once 'PHPExcel.php';
  require_once 'PHPExcel/IOFactory.php';
  //Update the multiple sheets in PHP excel
 $report_file = 'Report_' . date('Y-m-d') . '.xlsx';
 $report_file_exists  = 0;
 //If the file doesnot exist , create new otherwise append the data at last
 if (!file_exists($report_file)) {
      $objPHPExcel = new PHPExcel();
 } else {
      $report_file_exists = 1;
      $objPHPExcel = PHPExcel_IOFactory::load($report_file);
 }

 $columns = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

 //Sheet details
 $sheet_details = array(
                        //1st sheet details
                        0 => array('sheet_title' => 'Products', 
                                   'sheet_heading' => array('Article_Number','Name'),
                                   'sheet_data' => array('1234','Pen')
                                   ),
                        //2nd Sheet Details
                        1 => array('sheet_title' => 'Categories',
                                   'sheet_heading' => array('Category Id','Name'),
                                   'sheet_data' => array(123,'Accessories')
                                  )
                   );

 $sheet_count = 0;
 $row = 1;
 $column = 0;
 while ($sheet_count <= count($sheet_details)) {
      $objWorkSheet = '';
      if ($report_file_exists == 0) {
           if ($sheet_count > 0) {
                $objWorkSheet = $objPHPExcel->createSheet($sheet_count);
           } else {
                $objWorkSheet = $objPHPExcel->getActiveSheet();
           }
           $row = 1;
           $column = 0;
           foreach ($sheet_details[$sheet_count]['sheet_heading'] as $head) {
                $objWorkSheet->setCellValue($columns[$column] . $row, $head);
                $column++;
           }
      } else {
           $objPHPExcel->setActiveSheetIndex($sheet_count);
           $objWorkSheet = $objPHPExcel->getActiveSheet($sheet_count);
      }

      $row = $objWorkSheet->getHighestRow() + 1; //row count
      foreach ($sheet_details[$sheet_count]['sheet_data'] as $report_details) {
           $column = 0;
           foreach ($report_details as $data) {
                $objWorkSheet->setCellValue($columns[$column] . $row, $data);
                $column++;
           }
           $row++;
      }

      $objWorkSheet->setTitle($sheet_details[$sheet_count]['sheet_title']);
      $sheet_count++;
 }

 $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

 $objWriter->save($report_file);
 ?>
1
  • Add some description to your answer for better understanding. Jan 13, 2016 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.