追加新行使用phpExcel库excel文件(Append new row to excel fil

2019-09-02 18:52发布

我新的PHP以及对phpExcel 。 我只是想后的数据,每次保存到一个现有的Excel工作表到一个新行。

正如我搜索了Stackoverflow.com我得到图书馆的参考phpExcel

我采取一些样品写下下面的代码。

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

?>

但问题我面对的是我没有想法如何追加新行到Excel工作表,以便保存到新行每次发布的数据。

我知道我的这个代码仅保存文件到磁盘每次用单行,但我需要追加新行至最后的Excel工作表。

Answer 1:

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


Answer 2:

我有同样的问题,我发现从溶液fromArray上薄片法和第三参数, startCell

对PHP督一看:

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

所以就 :

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


Answer 3:

 <?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);
 ?>


文章来源: Append new row to excel file using phpExcel library
标签: php phpexcel