The following code is working only when I comment the Thrid sheet, I can see open and view the content which inserted, but when I include the Third sheet, it doesn't open the file but showing error,can’t be opened for some reason.
.
$this->spreadsheet = new Spreadsheet;
$this->spreadsheet->setActiveSheetIndex(0);
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello world');
$sheet->setTitle('First');
$this->spreadsheet->createSheet();
$this->spreadsheet->setActiveSheetIndex(1);
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello');
$sheet->setTitle('Second');
$this->spreadsheet->createSheet();
$this->spreadsheet->setActiveSheetIndex(2);
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->setTitle('Third');
$writer = new Xlsx($this->spreadsheet);
$writer->setOffice2003Compatibility(true);
$writer->save($fileName);
Please help me to solve this. https://github.com/PHPOffice/PhpSpreadsheet/ (develop) branch
Arcording to documentation you can try this:
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$spreadsheet = $reader->load('<path>'); //Path of reader sheet
// Create a new worksheet called "Name" in your case 3 Times
$myWorkSheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Name');
// Attach the "Name" worksheet as the first worksheet in the Spreadsheet object
$spreadsheet->addSheet($myWorkSheet, 0);
Then you can call a sheet by name
$spreadsheet->getSheetByName('Name');