DeleteEmptyExcelSheets
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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
% Version : 1.0
% Author : hnagel
% Date : 27/04/2007
% Tested : 02/05/2007 (DR)
%==========================================================================
% 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.
%
% Input:
%
% fileName: name of xls file
%
%---------------------------------------------------------------------
%
% Output:
%
% none
%
%---------------------------------------------------------------------
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
excelObj.Quit;
delete(excelObj);
return;
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;
%
% See also XLSWRITE
%---------------------------------------------------------------------
% Changes
%---------------------------------------------------------------------
sheetIdx = sheetIdx + 1;
end
sheetIdx2 = sheetIdx2 + 1; % prevent endless loop...
end
excelObj.EnableSound = true;
excelWorkbook.Save;
celWorkbook.Close(false);
%vcertain 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;
%
% Name :
% Date :
% Description:
% Indicated :
function DeleteEmptyExcelSheets(fileName)
% Check whether the file exists
if ~exist(fileName,'file')
% worksheet.
excelObj.EnableSound = false;
% Loop over all sheets
while sheetIdx2 <= numSheets
% Saves the current number of sheets in the workbook
temp = worksheets.count;
% have been saved in it.
%
% References: Torsten Jacobsen, "delete standard excel sheet"
%---------------------------------------------------------------------
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');
%
% 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
% DeleteEmptyExcelSheets.m
% DeleteEmptyExcelSheets: deletes all empty sheets in an xls-file
%
%==========================================================================
error([fileName ' does not exist !']);
else
% Check whether it is an Excel file
typ = xlsfinfo(fileName);
if ~strcmp(typ,'Microsoft Excel Spreadsheet')
worksheets = excelObj.sheets;
sheetIdx = 1;
sheetIdx2 = 1;
numSheets = worksheets.Count;
% Prevent beeps from sounding if we try to delete a non-empty
% Version : 1.0
% Author : hnagel
% Date : 27/04/2007
% Tested : 02/05/2007 (DR)
%==========================================================================
% 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.
%
% Input:
%
% fileName: name of xls file
%
%---------------------------------------------------------------------
%
% Output:
%
% none
%
%---------------------------------------------------------------------
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
excelObj.Quit;
delete(excelObj);
return;
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;
%
% See also XLSWRITE
%---------------------------------------------------------------------
% Changes
%---------------------------------------------------------------------
sheetIdx = sheetIdx + 1;
end
sheetIdx2 = sheetIdx2 + 1; % prevent endless loop...
end
excelObj.EnableSound = true;
excelWorkbook.Save;
celWorkbook.Close(false);
%vcertain 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;
%
% Name :
% Date :
% Description:
% Indicated :
function DeleteEmptyExcelSheets(fileName)
% Check whether the file exists
if ~exist(fileName,'file')
% worksheet.
excelObj.EnableSound = false;
% Loop over all sheets
while sheetIdx2 <= numSheets
% Saves the current number of sheets in the workbook
temp = worksheets.count;
% have been saved in it.
%
% References: Torsten Jacobsen, "delete standard excel sheet"
%---------------------------------------------------------------------
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');
%
% 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
% DeleteEmptyExcelSheets.m
% DeleteEmptyExcelSheets: deletes all empty sheets in an xls-file
%
%==========================================================================
error([fileName ' does not exist !']);
else
% Check whether it is an Excel file
typ = xlsfinfo(fileName);
if ~strcmp(typ,'Microsoft Excel Spreadsheet')