Rodrane Rodrane - 2 months ago 11x
PHP Question

PhpExcel creates multiple worksheets

hello I am trying to create an excel template by using phpexcel

For some reason image creates new worksheet instead of using current one.
so when I open excel file I've created there are worksheet and worksheet1 instead of single one.

objPHPExcel = new PHPExcel();
$objWorkSheet = $objPHPExcel->createSheet();

// Set the active Excel worksheet to sheet 0

//Taslak Verileri
$objPHPExcel->getActiveSheet()->SetCellValue('D'.'1', 'Firm');
$objPHPExcel->getActiveSheet()->SetCellValue('J'.'1', 'SFUFORMU - FR.PS.21');
$objPHPExcel->getActiveSheet()->SetCellValue('J'.'3', 'NO:');
$objPHPExcel->getActiveSheet()->SetCellValue('D'.'2', 'Name Surname Signature');
$objPHPExcel->getActiveSheet()->SetCellValue('A'.'4', 'Date');
$objPHPExcel->getActiveSheet()->SetCellValue('A'.'5', 'Stock No:');
$objPHPExcel->getActiveSheet()->SetCellValue('C'.'5', 'Image');
$objPHPExcel->getActiveSheet()->SetCellValue('E'.'5', 'Image');
$objPHPExcel->getActiveSheet()->SetCellValue('G'.'5', 'Resim');
$objPHPExcel->getActiveSheet()->SetCellValue('I'.'5', 'Image');
$objPHPExcel->getActiveSheet()->SetCellValue('K'.'5', 'Quantity');
$objPHPExcel->getActiveSheet()->SetCellValue('M'.'5', 'Price');

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

enter image description here

enter image description here


You basically create a PHPExcel object which has already en empty sheet with index 0.

Then you create a new sheet with index 1.

Then you write all your stuff to sheet with index 0 and add the picture on second sheet (newly created).

This should solve your problem:


Note, that you still create a new sheet, even the first one already exists. If you want to use already existing worksheet, just do the following:


   $objWorkSheet = $objPHPExcel->createSheet();            

And do all the stuff with already existing sheet.

$sheet = $objPHPExcel->getSheet(0);
$sheet->setCellValue('D'.'1', 'Firm')//Etc all the stuff.

Give the drawing the same sheet: