I have already code for exporting excel file from php, and it works properly. But i h

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bernie123
    New Member
    • Jan 2014
    • 1

    I have already code for exporting excel file from php, and it works properly. But i h

    I have already a code for export php file to excel. My problem is I want to insert the image file into excel using php code. When I try this code below on this..

    Code:
    <?php
    	
    	//*** Get Document Path ***//
    	$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
    
    	$xlEdgeLeft = 7;
    	$xlEdgeTop = 8;
    	$xlEdgeBottom = 9;
    	$xlEdgeRight = 10;
    
    	//*** Excel Document Root ***//
    	$strFileName = "MyXls/Book1.xls";
    
    	//*** Connect to Excel.Application ***//
    	$xlApp = new COM("Excel.Application");
    	$xlBook = $xlApp->Workbooks->Add();
    	$xlSheet1 = $xlBook->Worksheets(1);
    	$xlApp->Application->Visible = False;	
    
    
    	//*** Sheet 1 ***//
    	$xlSheet1->Name = "My Sheet1";
    
    	//*** Write text to Row 1 Column 1 ***//		
    	$xlApp->ActiveSheet->Cells(1,1)->Value = "ShotDev.Com";
    	
    	//*** Insert Picture (1) ***//
    	$xlApp->Range("B3")->Select();
    	$xlApp->ActiveSheet->Pictures->Insert($strPath."/logo.gif")->Select();
    
    
    	//*** Insert Picture (2) ***//
    	$xlApp->Range("I3")->Select();
    	$Pic = $xlApp->ActiveSheet->Pictures->Insert($strPath."/logo.gif");
    	$Pic->Width = 300;
    	$Pic->Height = 150;
    	
    	@unlink($strFileName); //*** Delete old files ***//	
    
    	$xlBook->SaveAs($strPath."/".$strFileName); //*** Save to Path ***//
    
    	//*** Close & Quit ***//
    	$xlApp->Application->Quit();
    	$xlApp = null;
    	$xlBook = null;
    	$xlSheet1 = null;
    ?>
    The output will look like this

    "Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Office Excel<br/><b>Description :</b> Unable to get the SaveAs property of the Workbook class' in C:\xampp\htdocs \sample\php_ins ert_picture\php _insert_picture .php:45 Stack trace: #0 C:\xampp\htdocs \sample\php_ins ert_picture\php _insert_picture .php(45): variant->SaveAs('C:\xam pp\htdocs...') #1 {main} thrown in C:\xampp\htdocs \sample\php_ins ert_picture\php _insert_picture .php on line 45"

    Can anyone help me to debug this code. Thanks a Lot..
    Attached Files
    Last edited by Rabbit; Jan 27 '14, 04:24 AM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
Working...