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
  • 56,678
  • 9
  • 81
  • 133
user2741700
  • 841
  • 3
  • 10
  • 20

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
  • 29,798
  • 11
  • 58
  • 80