matlab批量删除excel中指定sheet
- 输入excel文件路径
- 扫描当前路径下.xlsx文件(子文件可自行修改)
- 删除各excel文件中的默认sheet,e.g.Sheet1
源代码
亲测 源码如下
.
// An highlighted block
function defaultsheetDel()
xlsxfile = input('please input the path:','s');
AllDDName = dir(xlsxfile);
for m = 1:length(AllDDName)
if strcmp(AllDDName(m).name,'.') || strcmp(AllDDName(m).name,'..')
continue;
end
if ~isdir(AllDDName(m).name)
filetype = split(AllDDName(m).name,".");
if strcmp(filetype(2),'xlsx')
mdlpath = AllDDName(m).folder;
ModelPath = [mdlpath,'\',AllDDName(m).name];
sheetName = 'Sheet'; % EN: Sheet, DE: Tabelle, etc. (Lang. dependent)
% Open Excel file.
objExcel = actxserver('Excel.Application');
objExcel.Workbooks.Open(ModelPath); % Full path is necessary!
% Delete sheets.
try
% Throws an error if the sheets do not exist.
objExcel.ActiveWorkbook.Worksheets.Item([sheetName '1']).Delete;
objExcel.ActiveWorkbook.Worksheets.Item([sheetName '2']).Delete;
objExcel.ActiveWorkbook.Worksheets.Item([sheetName '3']).Delete;
catch
% Do nothing.
end
% Save, close and clean up.
objExcel.ActiveWorkbook.Save;
objExcel.ActiveWorkbook.Close;
objExcel.Quit;
objExcel.delete;
end
end
end
end