10

我也是 php 新手phpExcel。我只想每次将帖子数据保存到现有的 Excel 工作表到新行。

当我在Stackoverflow.com上搜索时,我得到了 library 的参考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 表。

4

3 回答 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');
?>
于 2013-05-13T18:18:48.683 回答
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');
?>
于 2015-12-08T10:47:16.247 回答
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);
 ?>
于 2016-01-13T06:04:32.720 回答