Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 am able to load (.xlsx) file and i also able to modify (dentkey.xlsx). But After generating new .xlsx file (dentkey1.xlsx)..all worksheets data which was in (dentkey.xlsx) is not loading and also i cannot set value for 2nd worksheet.

you can fine

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

is not setting value 5 to 'C7' column of generated (dentkey.xlsx) file

please help me...

thanks a lot in advance

like image 352
Denish Avatar asked Sep 29 '11 08:09

Denish


People also ask

How do you create and edit Excel spreadsheets in php?

You may use the following code: header('Content-Type: application/vnd. ms-excel'); header('Content-Disposition: attachment;filename="file. xlsx"'); header('Cache-Control: max-age=0'); $writer->save('php://output');

How to add new sheet in PhpSpreadsheet?

Adding a new Worksheet You can add a new worksheet to the workbook using the createSheet() method of the Spreadsheet object.

How can I export data from multiple Excel sheets in php?

This is index. php file, in this file first we have make database connection, and fetch data from customer table and display on web page. After this we have make form with input select option for select how many records you want to export in single Excel file and below you can find submit button.

How to write in Excel file using php?

We can set the value to the cell by using the setCellValueByColumnAndRow(a,b,c) function. Write the created spreadsheet using the save() function. We can set the cell value using setCellValue() function() by passing cell index name like A1, A2, D3 etc. and value.


1 Answers

This code seems working, Tested in local Environment WampServer Version 2.1

Download latest version of PHPExcel from here PHPExcel 1.7.6

Working Code.

<?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.

like image 140
Nimit Dudani Avatar answered Sep 22 '22 13:09

Nimit Dudani