alphy
alphy

Reputation: 991

PhpExcel embedding image in the wrong worksheet if not embedding at all

I am writing both data and image logo to excel file using php excel, currently it works, data is written in sheet 2 as specified but the logo is written in sheet 0, if i pass a value say 2 in the getActiveSheet(), for the image same as for the data, nothing happens and my excel file seems to disappear, what am i not getting right?

public function getDataToExcel() {        
        $labref = $this->uri->segment(3);        
        $SampleName = $this->input->post('SampleName');
        $LabRef = $this->input->post('LabRef');      

        $objReader = PHPExcel_IOFactory::createReader('Excel2007');
        $objPHPExcel = $objReader->load("workbooks/" . $labref . "/" . $labref . ".xlsx");

        $objPHPExcel->getActiveSheet(2);
        $objWorkSheet = $objPHPExcel->createSheet();     
        $objWorkSheet->setCellValue('B40', $SampleName)
                ->setCellValue('B41', $LabRef);                

        $objWorkSheet->setTitle("Sample Summary");

        $dir = "workbooks";        
        if (is_dir($dir)) {

        $objDrawing = new PHPExcel_Worksheet_Drawing();
        $objDrawing->setName('NQCL');
        $objDrawing->setDescription('The Image that I am inserting');
        $objDrawing->setPath('exclusive_image/nqcl.png');
        $objDrawing->setCoordinates('A1');
        $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
            $objWriter->save("workbooks/" . $labref . "/" . $labref . ".xlsx");


             echo 'Data exported';
        } else {
            echo 'An error occured';

        }
}

Upvotes: 0

Views: 1055

Answers (1)

Mark Baker
Mark Baker

Reputation: 212452

Your problem is understanding the getActiveSheet() method

$objPHPExcel->getActiveSheet(2); 

getActiveSheet() doesn't take any arguments, it simply returns the currently active worksheet; so the 2 is meaningless.

To set the current active sheet, you need to call

$objPHPExcel->setActiveSheet(2); 

Upvotes: 1

Related Questions