使用phpExcel库将新行附加到excel文件

时间:2013-05-13 03:29:03

标签: php phpexcel

我是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表的最后一行添加新行。

3 个答案:

答案 0 :(得分:24)

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

答案 1 :(得分:3)

我遇到了同样的问题,我在工作表上找到了fromArray方法的解决方案,第三个参数startCell

查看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

所以,只是:

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

答案 2 :(得分: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);
 ?>