From: George on
Hi,

I have one question about erasing the content in a excel sheet. We know that we can use xlswrite to write data into excel, I am just wondering is there any function that can erase the content in a existing excel sheet? Thanks very much.
From: us on
"George " <guanjihou(a)gmail.com> wrote in message <hvgbku$3fm$1(a)fred.mathworks.com>...
> Hi,
>
> I have one question about erasing the content in a excel sheet. We know that we can use xlswrite to write data into excel, I am just wondering is there any function that can erase the content in a existing excel sheet? Thanks very much.

well... you could fill the sheet with empty CELLs...

us
From: George on
"us " <us(a)neurol.unizh.ch> wrote in message <hvgc7n$aok$1(a)fred.mathworks.com>...
> "George " <guanjihou(a)gmail.com> wrote in message <hvgbku$3fm$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I have one question about erasing the content in a excel sheet. We know that we can use xlswrite to write data into excel, I am just wondering is there any function that can erase the content in a existing excel sheet? Thanks very much.
>
> well... you could fill the sheet with empty CELLs...
>
> us

For sure I can do this, but I am wondering if there is any better idea. We gotta create a empty cells that is a waste of memory. Also the size of the cell is undecided.
From: us on
"George " <guanjihou(a)gmail.com> wrote in message <hvh1hj$gt2$1(a)fred.mathworks.com>...
> "us " <us(a)neurol.unizh.ch> wrote in message <hvgc7n$aok$1(a)fred.mathworks.com>...
> > "George " <guanjihou(a)gmail.com> wrote in message <hvgbku$3fm$1(a)fred.mathworks.com>...
> > > Hi,
> > >
> > > I have one question about erasing the content in a excel sheet. We know that we can use xlswrite to write data into excel, I am just wondering is there any function that can erase the content in a existing excel sheet? Thanks very much.
> >
> > well... you could fill the sheet with empty CELLs...
> >
> > us
>
> For sure I can do this, but I am wondering if there is any better idea. We gotta create a empty cells that is a waste of memory. Also the size of the cell is undecided.

well... in this case: think about using the ACTIVEX/COM interface...

help com;
help actxserver;

us
From: ImageAnalyst on
Geroge, try this:
Sample call:
fullFileName = 'C:\Program Files\MATLAB\work\Book1.xls';
EraseExcelSheets(fullFileName);

%------------------------------------------------------------------------
% Erase all data in all sheets in the workbook.
function EraseExcelSheets(fileName)
% By ImageAnalyst
% 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;
numSheets = worksheets.Count;
% Prevent beeps from sounding if we try something not allowed.
excelObj.EnableSound = false;

% Loop over all sheets
for s = 1 : numSheets
% worksheets.Item(sheetIndex).UsedRange is the range of used
cells.
worksheets.Item(s).UsedRange.Delete;
end
excelObj.EnableSound = true;
excelWorkbook.Save;
excelWorkbook.Close(false);
excelObj.Quit;
delete(excelObj);
return;