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');
When you first instantiate the $objPHPExcel, it already has a single sheet (sheet 0); you're then adding a new sheet (which will become sheet 1), but setting active sheet to sheet $i (when $i is 0)... so you're renaming and populating the original worksheet created when you instantiated $objPHPExcel rather than the one ...
Adding a new Worksheet You can add a new worksheet to the workbook using the createSheet() method of the Spreadsheet object.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With