0

I have en excel 2007 document with 10 sheets but when I try to edit it with PHPExcel all the contents of those sheets (except the first one and the input made with PHPExcel) are erased.

Here is my code:

require 'PHPExcel.php';
require 'PHPExcel/IOFactory.php';
require 'PHPExcel/Writer/Excel2007.php';

$fileType = 'Excel2007';
$fileName = 'test.xlsx';

$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);

$activeSheet = $objPHPExcel->setActiveSheetIndexbyName('ID');
$activeSheet->setCellValue('A2', 'string test');

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($fileName);
pnuts
  • 58,317
  • 11
  • 87
  • 139
user2741700
  • 881
  • 3
  • 11
  • 21

1 Answers1

0

By looking at this post try rearranging your code like this:

require 'PHPExcel.php';
require 'PHPExcel/IOFactory.php';
require 'PHPExcel/Writer/Excel2007.php';

$fileType = 'Excel2007';
$fileName = 'test.xlsx';

$objPHPExcel = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objPHPExcel->load($fileName);
$objPHPExcel->setActiveSheetIndexbyName('ID');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 'string test');

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($fileName);
Community
  • 1
  • 1
Automate This
  • 30,726
  • 11
  • 60
  • 82