Code covered by the BSD License  

Highlights from
pic2xls

pic2xls

by

 

01 Dec 2005 (Updated )

This function inserts a picture into an Excel file

pic2xls(pic,file,sheet,param)
function pic2xls(pic,file,sheet,param)

%PIC2XLS Inserts a picture into an Excel file
%
% pic2xls(pic,file,sheet,param)
%
% pic2xls  : Inserts a picture into an Excel file
%
%       pic:            image file name (.gif,.jpg,.bmp,..etc)
%       file:           Excel file name (new or existing)
%       sheet:          sheet name.
%       param:          consists of a row of 4 numbers:
%                       [left top width height]
%                       left:   image location from left side (pixels)
%                       top:    image location from top side (pixels)
%                       width:  image width (pixels)
%                       height: image width (pixels)
% 
% Example:
%   pic = 'tree.jpg';
%   file = 'file.xls';
%   pic2xls(pic,file,'Sheet1',[0 0 200 200]);

%   Copyright 2005 Fahad Al Mahmood
%   Version: 1.0    $Date: 1-Dec-2005

[fpath,fname,fext] = fileparts(file);
if isempty(fpath)
    out_path = pwd;
elseif fpath(1)=='.'
    out_path = [pwd filesep fpath];
else
    out_path = fpath;
end

[ifpath,ifname,ifext] = fileparts(pic);
if isempty(ifpath)
    iout_path = pwd;
elseif fpath(1)=='.'
    iout_path = [pwd filesep ifpath];
else
    iout_path = ifpath;
end

Excel = actxserver('Excel.Application');

if ~exist(file,'file')
        % The following case if file specified and does not exist (Creating New Workbook)
        Workbook = invoke(Excel.Workbooks,'Add');
        new=1;
        set(Excel, 'Visible', 1);
    else
        % The following case if file specified and does exist (Opening Workbook)
        Workbook = invoke(Excel.Workbooks, 'open', [out_path filesep fname fext]);
        new=0;
        set(Excel, 'Visible', 0);
end
    
% Activating Sheet
Sheets = Excel.Worksheets;
sheet = get(Sheets, 'Item', sheet);
invoke(sheet, 'Activate');

% Adding Picture

% Function AddPicture(Filename As String, LinkToFile As MsoTriState,
% SaveWithDocument As MsoTriState, Left As Single, Top As Single, Width As Single, Height As Single) As Shape

ExAct = Excel.Activesheet;
invoke(ExAct.Shapes,'AddPicture',[iout_path filesep ifname ifext],0,1,param(1),param(2),param(3),param(4));

if ~new
    invoke(Workbook, 'Save');
    invoke(Excel, 'Quit');
end
        
delete(Excel);

Contact us