Published in WordPress
avatar
4 minutes read

PHPExcel modify existing .xlsx file with multiple worksheet

require_once '../Classes/PHPExcel/IOFactory.php';


/** PHPExcel */require_once '../Classes/PHPExcel.php';

$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('dentkey.xlsx');
$excel2->setLoadAllSheets();
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('C6', '4')           

->setCellValue('C7', '5')         

  ->setCellValue('C8', '6')       

    ->setCellValue('C9', '7');
$excel2->setActiveSheetIndex(1);
$excel2->getActiveSheet()->setCellValue('A7', '4')

->setCellValue('C7', '5');


$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('dentkey1.xlsx');


  Here, I can load the .xlsx file and modify dentkey.xlsx successfully. However, after generating the new file (dentkey1.xlsx), the data from all the worksheets in dentkey.xlsx is not loading, and I am also unable to set values for the second worksheet.  

you can fine

$excel2->setActiveSheetIndex(1);
$excel2->getActiveSheet()->setCellValue('A7', '4')

->setCellValue('C7', '5');
  • php
  • wordpress
  • phpexcel

please help me...

I can load .xlsx files and edit dentkey.xlsx without any issues. However, when I create a new .xlsx file (dentkey1.xlsx), all the data from the worksheets in dentkey.xlsx fails to load. Additionally, I'm unable to set the value of 5 in column 'C7' of the generated dentkey.xlsx file.

Could you please assist me with this?

Thank you in advance!

  • php
  • wordpress
  • phpexcel

Solutions

  In this case, I may load (.xlsx) files and edit (dentkey.xlsx). However, all of the worksheet data that was in (dentkey.xlsx) is not loading after creating a new.xlsx file (dentkey1.xlsx), and I am also unable to set values for the second worksheet. You can be okay.  

<?php
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
require_once '../Classes/PHPExcel/IOFactory.php';
require_once '../Classes/PHPExcel.php';

$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('nTest.xlsx'); // Empty Sheet
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('C6', '4')
    ->setCellValue('C7', '5')
    ->setCellValue('C8', '6')       
    ->setCellValue('C9', '7');

$excel2->setActiveSheetIndex(1);
$excel2->getActiveSheet()->setCellValue('A7', '4')
    ->setCellValue('C7', '5');
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('Nimit New.xlsx');
?>

Remove this line from your code: $excel2->setLoadAllSheets();.

Hope this will help.

Comments