From: Andy on
"Amit S. Deshpande" <adeshpan(a)mathworks.com> wrote in message <i4480c$s0t$3(a)fred.mathworks.com>...
> Hi,
>
> I know that there is a way to create an xls files having worksheet
> specified by the user and not having those default sheet1, sheet2 sheet3
> worksheet in the file.
> I know you can add sheets by passing the sheet name as an argument to
> the command. But, it only appends the sheet to existing default ones.
> I would appreciate your help.
>
> Regards,
>
> Amit
>
> --
>
> Amit S.Deshpande
> MathWorks
> 39555 Orchard Hill Place, Suite 280
> Novi MI 48375
> Phone: 248 675 3309
> Fax: 248 596 7959
> email: Amit.Deshpande(a)mathworks.com

Read the documentation for actxserver, which will let you fully control Excel from MATLAB.

P.S. It's maybe not the best idea to post all of your contact information to the internet.
From: Amit S. Deshpande on
thanks.

On 8/13/2010 4:05 PM, Andy wrote:
> "Amit S. Deshpande" <adeshpan(a)mathworks.com> wrote in message
> <i4480c$s0t$3(a)fred.mathworks.com>...
>> Hi,
>>
>> I know that there is a way to create an xls files having worksheet
>> specified by the user and not having those default sheet1, sheet2
>> sheet3 worksheet in the file.
>> I know you can add sheets by passing the sheet name as an argument to
>> the command. But, it only appends the sheet to existing default ones.
>> I would appreciate your help.
>>
>> Regards,
>>
>> Amit
>>
>> --
>>
> Read the documentation for actxserver, which will let you fully control
> Excel from MATLAB.
>
> P.S. It's maybe not the best idea to post all of your contact
> information to the internet.

From: ImageAnalyst on
I don't know. I do know that you can set the number of blank sheets
at startup down to 1 though, via the Excel options/preferences.

What I do is to just delete those unused empty default sheets using
the code below.

% DeleteEmptyExcelSheets: deletes all empty sheets in an xls-file
%
%==========================================================================
% Version : 1.0
% Author : hnagel
% Date : 27/04/2007
% Tested : 02/05/2007 (DR)
%==========================================================================
%
% This function looped through all sheets and deletes those sheets
that are
% empty. Can be used to clean a newly created xls-file after all
results
% have been saved in it.
%
% References: Torsten Jacobsen, "delete standard excel sheet"
%---------------------------------------------------------------------
%
% Input:
%
% fileName: name of xls file
%
%---------------------------------------------------------------------
%
% Output:
%
% none
%
%---------------------------------------------------------------------
%
% See also XLSWRITE
%---------------------------------------------------------------------
% Changes
%---------------------------------------------------------------------
%
% Name :
% Date :
% Description:
% Indicated :
function DeleteEmptyExcelSheets(fileName)
% Check whether the file exists
if ~exist(fileName,'file')
error([fileName ' does not exist !']);
else
% Check whether it is an Excel file
typ = xlsfinfo(fileName);
if ~strcmp(typ,'Microsoft Excel Spreadsheet')
error([fileName ' not an Excel sheet !']);
end
end

% If fileName does not contain a "\" the name of the current path is
added
% to fileName. The reason for this is that the full path is required
for
% the command "excelObj.workbooks.Open(fileName)" to work properly.
if isempty(strfind(fileName,'\'))
fileName = [cd '\' fileName];
end

excelObj = actxserver('Excel.Application');
excelWorkbook = excelObj.workbooks.Open(fileName);
worksheets = excelObj.sheets;
sheetIdx = 1;
sheetIdx2 = 1;
numSheets = worksheets.Count;
% Prevent beeps from sounding if we try to delete a non-empty
worksheet.
excelObj.EnableSound = false;

% Loop over all sheets
while sheetIdx2 <= numSheets
% Saves the current number of sheets in the workbook
temp = worksheets.count;
% Check whether the current worksheet is the last one. As there
always
% need to be at least one worksheet in an xls-file the last sheet
must
% not be deleted.
if or(sheetIdx>1,numSheets-sheetIdx2>0)
% worksheets.Item(sheetIdx).UsedRange.Count is the number of used
cells.
% This will be 1 for an empty sheet. It may also be one for
certain other
% cases but in those cases, it will beep and not actually delete
the sheet.
if worksheets.Item(sheetIdx).UsedRange.Count == 1
worksheets.Item(sheetIdx).Delete;
end
end
% Check whether the number of sheets has changed. If this is not
the
% case the counter "sheetIdx" is increased by one.
if temp == worksheets.count;
sheetIdx = sheetIdx + 1;
end
sheetIdx2 = sheetIdx2 + 1; % prevent endless loop...
end
excelObj.EnableSound = true;
excelWorkbook.Save;
excelWorkbook.Close(false);
excelObj.Quit;
delete(excelObj);
return;