error Call to a member function getActiveSheet() on a non-object

Topics: Developer Forum
Sep 25, 2012 at 7:25 AM
Edited Sep 25, 2012 at 7:25 AM

Hai... i have a problem when i want to insert image in phpexcel

require_once "../conf/Classes/PHPExcel/IOFactory.php";
require_once "../conf/Classes/PHPExcel.php";

// Create new picture object
  $objDrawing = new PHPExcel_Worksheet_Drawing();
  $objDrawing->setName('test_img');
  $objDrawing->setDescription('test_img');
  $objDrawing->setPath('k.jpg'); //image

// Insert picture
  $objDrawing->setCoordinates('P1');
  $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); ---> this code error

 

but,when i get to browser,any notice error

Call to a member function getActiveSheet() on a non-object

Please help me :D

Coordinator
Sep 25, 2012 at 7:43 AM

Where are you instantiating your PHPExcel object?

Sep 25, 2012 at 9:59 AM

$objTpl = PHPExcel_IOFactory::load("h.xlsx");
$objTpl->setActiveSheetIndex(0);

// Create new picture object
  $objDrawing = new PHPExcel_Worksheet_Drawing();
  $objDrawing->setName('test_img');
  $objDrawing->setDescription('test_img');
  $objDrawing->setPath('k.jpg'); //image

// Insert picture
  $objDrawing->setCoordinates('P1');
  $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());


$objTpl->getActiveSheet()->setCellValue('C6', stripslashes($data[0]['NO_REGISTRASI'])); //mengambil no. registrasi
$objTpl->getActiveSheet()->setCellValue('C8', stripslashes('1. '.$data[0]['NAMA_KARYAWAN'])); //mengambil nik karyawan pada index 0
$objTpl->getActiveSheet()->setCellValue('C9', stripslashes('2. '.$data[1]['NAMA_KARYAWAN']));//mengambil nik karyawan pada index 1
$objTpl->getActiveSheet()->setCellValue('C10', stripslashes('3. '.$data[2]['NAMA_KARYAWAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('C12', stripslashes($data[0]['TGL_USUL']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('A15', stripslashes($data[0]['LATAR_BELAKANG']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('A25', stripslashes($data[0]['PERBAIKAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H15', stripslashes($data[0]['MANFAAT']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H19', stripslashes($data[0]['PENYELESAIAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H24', stripslashes($data[0]['ALAT_BAHAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H28', stripslashes($data[0]['BIAYA']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H37', stripslashes($data[0]['NO_WR']));//mengambil nik karyawan pada index 2
//prepare download
$filename=mt_rand(1,100000).'.xlsx'; //just some random filename
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
 
$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
 
exit;

This is my code,

i dont understand where i am instantiating PHPExcel object..

because i new learning.. :D

Sep 25, 2012 at 10:27 AM

Dear Mr. MarkBaker..

i has put the instantiating PHPExcel object, and now nothing have a error Call to a member function getActiveSheet() on a non-object again,

But,when finished download to excel,picture dosen't show..

any wrong that code?

Please for help..

 

Thanks a lot

Sep 25, 2012 at 10:41 AM

instantiating means creating an instance of an object (have a look at OOP basics, Google will help you with that ;). In this case you want an instance of the PHPExcel class, and assign it to $objPHPExcel, since you are calling getActiveSheet() on it. However, since you are using $objTpl (and not $objPHPExcel), I'm guessing you just missed a replace after copy pasting the example (check the line with the "getActiveSheet()" call) ;)

Sep 25, 2012 at 10:53 AM

yes.. i miss that :D

but now,when i download file excel,the image doesnt show..

with same code in up..

can help?

Coordinator
Sep 25, 2012 at 12:32 PM

Is the image file in the current working directory of your script? Does your PHP script have permission to access the image? Try setting the file with the verify option:

$objDrawing->setPath('k.jpg', TRUE); //image


This has the added benefit of setting the height and width automatically

Sep 25, 2012 at 2:46 PM

 

I still can not display the picture in excel,
I want to put the images on the colom P1,
images from the same folder with the coding,and i has added the option seting

$objDrawing->setPath('k.jpg', TRUE); //image

Mr.Mark have a sample script to display the image?

Coordinator
Sep 25, 2012 at 3:32 PM

Look at some of the examples in the /Tests directory, like 05featuredemo.inc.php

Sep 25, 2012 at 3:47 PM

Ok sir, i will learn that code..

if i still do not understand, i will ask again ya sir.. :D

Sep 25, 2012 at 4:51 PM
Edited Sep 25, 2012 at 4:55 PM
//include PHPExcel library
require_once "../conf/Classes/PHPExcel.php";
require_once "../conf/Classes/PHPExcel/IOFactory.php";

$objTpl = PHPExcel_IOFactory::load("template.xlsx"); --> using template xlsx 
$objTpl->setActiveSheetIndex(0);

// Instantiate the module
$objTpl = new PHPExcel(); -->inilitation class 


// Add a drawing to the worksheet
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('PHPExcel logo');
$objDrawing->setDescription('PHPExcel logo');
$objDrawing->setPath('./images/phpexcel_logo.gif');
$objDrawing->setCoordinates('C33');
$objDrawing->setWorksheet($objTpl->getActiveSheet());

$id=$_GET['id_export'];
$sql=mysql_query("SELECT *,b.nik_karyawan as pengusul FROM (SELECT * FROM ira_data_improvement)a,(select * from ira_data_improvment_pengusul)b,(select * from ira_mst_karyawan)c WHERE a.no_registrasi=b.no_registrasi AND b.nik_karyawan=c.nik_karyawan AND id_improvement=".$id." ");
$data=array();
while($h=mysql_fetch_array($sql))
{
    $data[]=$h;
    
}
// fill the value from database
$objTpl->getActiveSheet()->setCellValue('C6', stripslashes($data[0]['NO_REGISTRASI'])); //mengambil no. registrasi
$objTpl->getActiveSheet()->setCellValue('C8', stripslashes('1. '.$data[0]['NAMA_KARYAWAN'])); //mengambil nik karyawan pada index 0
$objTpl->getActiveSheet()->setCellValue('C9', stripslashes('2. '.$data[1]['NAMA_KARYAWAN']));//mengambil nik karyawan pada index 1
$objTpl->getActiveSheet()->setCellValue('C10', stripslashes('3. '.$data[2]['NAMA_KARYAWAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('C12', stripslashes($data[0]['TGL_USUL']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('A15', stripslashes($data[0]['LATAR_BELAKANG']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('A25', stripslashes($data[0]['PERBAIKAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H15', stripslashes($data[0]['MANFAAT']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H19', stripslashes($data[0]['PENYELESAIAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H24', stripslashes($data[0]['ALAT_BAHAN']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H28', stripslashes($data[0]['BIAYA']));//mengambil nik karyawan pada index 2
$objTpl->getActiveSheet()->setCellValue('H37', stripslashes($data[0]['NO_WR']));//mengambil nik karyawan pada index 2
//prepare download
$filename=mt_rand(1,100000).'.xlsx'; //just some random filename
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
 
$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
 
exit; //done.. exiting

But,when finished download template excel,formating in template become bad,not same with early.
if i remove the class drawing,template good, and same from early..

please your help and explanation
:D

-Thank You-