how to delete the first sheet of an excel through script

35 ビュー (過去 30 日間)
wenchao zhang
wenchao zhang 2024 年 11 月 13 日 7:15
コメント済み: wenchao zhang 2024 年 11 月 13 日 7:48
hello, when i write some contents to excel file and add new sheet with specific name, i find sheet1 is still exist, how to delete it through script, thanks.

採用された回答

Pavl M.
Pavl M. 2024 年 11 月 13 日 7:38
編集済み: Pavl M. 2024 年 11 月 13 日 7:39
Good question.
There are numerous options to do that complete.
Which Excel driver are there available to utilize for greater value?
Which OS are you on?
filepath = ... your file path;
sheetordernum = 1;
1.
newExcel = actxserver('excel.application');
excelWB = newExcel.Workbooks.Open(filepath,0,false);
newExcel.Visible = true;
newExcel.DisplayAlerts = false;
newExcel.Sheets.Item(sheetordernum).Delete;
excelWB.Save();
excelWB.Close();
newExcel.Quit();
clear excelWB
clear newExcel
2.
Excel = actxserver('Excel.Application');
Workbook = Excel.Workbooks.Open(strcat(pwd,'\MAT.xls'));
% Make the application invisible
set(Excel, 'Visible', 0);
% Make excel not display alerts
set(Excel,'DisplayAlerts',0);
% Get a handle to Excel's Workbooks
Workbooks = Excel.Workbooks;
Excel.ActiveWorkBook.Sheets.Item(sheetordernum).Delete;
Excel.Save()
Excel.Close()
3.
NET.addAssembly('microsoft.office.interop.excel');
app = Microsoft.Office.Interop.Excel.ApplicationClass;
app.Visible = true;
app.DisplayAlerts = false;
app.ActiveWorkBook.Sheets.Item(sheetodernum).Delete;
app.Save()
app.Close()
clear app
Hope this finds you well and solves the problem.
  1 件のコメント
wenchao zhang
wenchao zhang 2024 年 11 月 13 日 7:48
thank you,seems little complex

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by