Page 1 of 1

why i can't insert image into Excel

Posted: Fri Jan 11, 2008 5:21 am
by warran
i create a class file,and use com application to control Excel

class.Excel.php

Code: Select all

<?php

/**
*   class   Excel
*
*   ??com??Excel
*
*   @author:warran
*   @version:1.0
*/
class   Excel     {

var   $xlsPath;
var   $_app;
var   $_book;
var   $_sheet;
var   $_activeWorkSheet=1;
function   Excel($xlsPath)   {
$this-> _app=new   COM( "excel.application ");
$this-> xlsPath=$xlsPath;
if(file_exists($xlsPath)){
$this-> _open();
}else{
$this-> _addWorkBook();
}
$this-> _book=$this-> _app-> Workbooks(1);
$this-> _app-> WorkSheets[1]-> Activate();
$this-> _sheet=$this-> _book-> Worksheets(1);
}
function   _open(){
$this-> _app-> Workbooks-> Open($this-> xlsPath);
}
function   _addWorkBook(){
$this-> _app-> Workbooks-> add();
}
function   setActiveWorkBook($k){
$this-> _book=$this-> _app-> Workbooks($k);
$this-> _sheet=$this-> _book-> Worksheets($this-> _activeWorkSheet);
}
function   setActiveWorkSheet($k){
$this-> _activeWorkSheet=$k;
$this-> _app-> WorkSheets[$k]-> Activate();
}
function   setValue($cell,$row,$value){
$this-> _sheet-> Cells[$cell][$row]   -> Value   =   $value;
}
function   setValueByRange($range,$value){
foreach($this-> _sheet-> Range($range)   as   $cell){
$cell-> Value=$value;
}
}
function   save(){
$this-> _book-> Save();
}
function   quit(){
$this-> _app-> Quit();
}
function   addSheet($name){
$this-> _app-> WorkSheets-> add();
$this-> _app-> WorkSheets[1]-> Name=$name;
}
function   addImg($imgPath){
//$this-> _sheet-> Cells[$cell][$row]-> Select();
//$this-> _sheet-> Cells[$cell][$row]-> Picture-> LoadPicture($imgPath);
//$this-> _sheet-> Shapes-> AddPicture($imgPath,True,True,60,20,400,300);
$this-> _sheet-> Pictures-> Insert( "D:/web/www/rencai/html/1.jpg ");
}

}

?>
test.php

Code: Select all

<?php
require( "class.Excel.php ");
$excel=new   Excel( "c:/test.xls ");
$excel-> setValueByRange( "a1:f5 ", "aaa ");
//$excel-> addSheet( "aaa11 ");
$excel-> addImg(10,10, "D:/1.jpg ");
$excel-> save();
$excel-> quit();
?>

Re: why i can't insert image into Excel

Posted: Sat Jan 12, 2008 11:37 pm
by John Cartwright
Take a look here,

Code: Select all

function   addImg($imgPath){
   //$this-> _sheet-> Cells[$cell][$row]-> Select();
   //$this-> _sheet-> Cells[$cell][$row]-> Picture-> LoadPicture($imgPath);
   //$this-> _sheet-> Shapes-> AddPicture($imgPath,True,True,60,20,400,300);
   $this-> _sheet-> Pictures-> Insert( "D:/web/www/rencai/html/1.jpg ");
} 
your method insert() probably should be accepting $imgPath as it's argument instead of the hardcoded image.