-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_excel_sheets.m
More file actions
66 lines (52 loc) · 1.56 KB
/
delete_excel_sheets.m
File metadata and controls
66 lines (52 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function delete_excel_sheets(varargin)
% Code does as it says
% Modified from Technical Solution ID 1-21EPB4 at
% http://www.mathworks.com/support/solutions/en/data/1-21EPB4/index.html?solution=1-21EPB4
params.filename='';
params.delete_sheets={'Sheet1','Sheet2','Sheet3'};
% Update
params=parse_pv_pairs(params,varargin);
% Try to correct filename for relative path
temp_string = params.filename;
if (temp_string(2)~=':')
% It's not likely to be a full path
params.filename = fullfile(cd,params.filename);
end
% Check file exists
check_f=fopen(params.filename,'r');
if (check_f<0)
disp(sprintf('%s could not be opened by delete_Excel_sheets()'));
return
end
fclose(check_f);
% Open Excel as a COM Automation server
Excel = actxserver('Excel.Application');
% Make it invisible
set(Excel,'Visible',0);
% Turn off alerts
set(Excel,'DisplayAlerts',0);
% Get a handle to Excel's Workbooks
Workbooks=Excel.Workbooks;
% Open an Excel Workbook and activate it
Workbook=Workbooks.Open(params.filename);
% Get the sheets
Sheets=Excel.ActiveWorkBook.Sheets;
index_adjust=0;
[temp,sheet_names]=xlsfinfo(params.filename);
% Cycle through the sheets
for i=1:length(params.delete_sheets)
matches=(strcmp(sheet_names,params.delete_sheets{i}));
if (any(matches))
current_sheet=get(Sheets,'Item',(i-index_adjust));
invoke(current_sheet,'Delete');
index_adjust=index_adjust+1;
end
end
% Save the workbook
Workbook.Save;
% Close the Workbook
Workbooks.Close;
% Quite Excel
invoke(Excel,'Quit');
% Delete the handle to the ActiveX object
delete(Excel);