10
votes

Ajouter une nouvelle ligne pour exceller le fichier à l'aide de la bibliothèque PHPEXCEL

Je suis nouveau à PHP également à phpexcel code>. Je veux juste enregistrer publier des données sur une feuille Excel existante à chaque fois à une nouvelle ligne.

Comme je l'ai cherché sur Stackoverflow.com J'ai la référence de la bibliothèque phpexcel code>. P>

J'écris le code suivant de prendre des échantillons. P>

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

?>


5 commentaires

À moins que vous ne souhaitiez insérer une nouvelle ligne avant une ligne existante, vous n'avez pas besoin de vous inquiéter. Set CellValue () ajoutera des données sur la cellule que vous spécifiez: Si une valeur de cellule existe déjà, elle sera écrasée; Si cela n'existe pas, il sera créé. Si vous devez connaître le numéro de la dernière ligne, utilisez la méthode GetHighestrow () de la feuille de calcul, ou GetHighestDaTarow ()


Je veux juste ajouter une nouvelle ligne à chaque fois que les données sont post et mon fichier Excel est déjà placé sur le serveur. signifie que je ne veux pas avoir perdu de vieilles données. Nouvelles données Nouvelle ligne.


Dans ce cas: chargez le fichier, appelez gethighestrow () pour obtenir la dernière ligne utilisée, ajoutez-en une à cette valeur, puis écrivez les nouvelles données sur ce nouveau numéro de ligne.


@MarkBaker Votre suggestion m'a aidé .Merci


Dupliqué possible de Ajout d'une nouvelle ligne avec phpexcel?


3 Réponses :


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

0 commentaires

3
votes

J'ai eu le même problème, et j'ai trouvé une solution de fratharray méthode sur la feuille et le troisième paramètre, startcell

regarder sur php-doc: < / p> xxx

donc, juste: xxx


0 commentaires

1
votes
 <?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 commentaires

Ajoutez une description à votre réponse pour une meilleure compréhension.