Anar Bayramov
Anar Bayramov

Reputation: 11584

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 
            $objPHPExcel->setActiveSheetIndex(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();
                  $objDrawing->setWorksheet($objWorkSheet);
                  $objDrawing->setName("name");
                  $objDrawing->setDescription("Description");
                  $objDrawing->setPath('temp/3.jpeg');
                  $objDrawing->setCoordinates('F9');
                  $objDrawing->setOffsetX(1);
                  $objDrawing->setOffsetY(5);
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
            $objWriter->save('some_excel_file.xlsx'); 

enter image description here

enter image description here

Upvotes: 5

Views: 28781

Answers (3)

senthilkumar
senthilkumar

Reputation: 49

write 2 sheet a single excel workbook using PHPEXCEL

for ($i=0; $i <2 ; $i++) { 
    $objPHPExcel->getActiveSheet()->setTitle('Worksheet'); //sheetname
    $newsheet = $objPHPExcel->createSheet(); //sheet create
}

Upvotes: 0

I don't know how rules phpexcel but in your context appears such you were creating two worksheets with this method cause you are calling two times...

$objWorkSheet = $objPHPExcel->createSheet();  

$objDrawing->setWorksheet($objWorkSheet);

Upvotes: 0

sybear
sybear

Reputation: 7784

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:

$objPHPExcel->setActiveSheetIndex(1); 

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:

Remove:

   $objWorkSheet = $objPHPExcel->createSheet();            
   $objPHPExcel->setActiveSheetIndex(0); 

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:

$objDrawing->setWorksheet($sheet);

Upvotes: 9

Related Questions