How can I get data from an open Excel file without knowing the file name?

6 ビュー (過去 30 日間)
Charlotte McHugh
Charlotte McHugh 2017 年 7 月 20 日
コメント済み: Massimo Salese 2022 年 6 月 7 日
I have an unsaved .xlsx file open from another script, and I'm trying to write a Matlab program to extract the data from the file. There is no filename or path, however I am able to access the Active Workbook and corresponding Sheet by using ActiveX. It possible for me to pull the file data from the Workbook or Sheet object?
Here is how I get the workbook and sheet:
ex = actxGetRunningServer('Excel.Application');
book = ex.ActiveWorkbook;
sheet = book.Worksheets.Item(1);

回答 (1 件)

Ankitha Kollegal Arjun
Ankitha Kollegal Arjun 2017 年 7 月 24 日
You can add two additional lines to your code as follows:
ex = actxGetRunningServer('Excel.Application');
book = ex.ActiveWorkbook;
sheet = book.Worksheets.Item(1);
% Below command will read cells A1-B4
cells = sheet.get('Range','A1','B4'); %Specify the range of cells you want to read from the Excel File
data = cells.Value; %'data' will be a cell array containing the excel cell values
  3 件のコメント
Ankitha Kollegal Arjun
Ankitha Kollegal Arjun 2017 年 7 月 25 日
To save the workbook, you can add the following line to the code:
SaveAs(book,'myfile.xls')
Massimo Salese
Massimo Salese 2022 年 6 月 7 日
And what if i use the dinamic range notation?
cells = sheet.get('Range','A1#','B4#')

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by