필터 지우기
필터 지우기

ActiveX Excel Awkward Behavior

조회 수: 2 (최근 30일)
Oren Lee
Oren Lee 2021년 1월 28일
답변: Oren Lee 2021년 2월 1일
Hi,
I am working on something for which I need to read many sheets from a single excel file. The file is on the large side, using the standard readcell(...) was too slow opening an instance of excel for each sheet. I instead ventured into using ActiveX instead, something I am only slightly familiar with.
Based on several posts I found online, I put together the following bit of code:
hExcel = actxserver('Excel.Application');
hExcel.visible = 1;
hExcel.DisplayAlerts = 0;
Wkbk = hExcel.Workbooks.Open(filePath);
for iter = 1:length(loops)
% Looping over Wkbk sheets
data = Wkbk.Worksheets.Item(sheetTitles(iter)).UsedRange.Value;
end
Wkbk.Close;
hExcel.Quit;
hExcel.delete;
The above runs properly, however there are some very strange caveats I I dont understand.
  1. The above code ONLY works if visibility is set to 1. I would prefer this run in the background without the user seeing it.
  2. Even with DisplayAlerts set to 0, I cannot close the opened Excel application without manually pressing the "Save" or "Do not Save" button on the window that appears.
Thank you guys for the help! The answers on here are always very insightful!

채택된 답변

Oren Lee
Oren Lee 2021년 2월 1일
For those who may stumble across this in the future, I have a solution to both issues. Ultimately I belive both issues are due to internal ActiveX/Excerl settings, nothing to do with MATLAB.
First, to have the workbook not prompt the user for Save/Do not Save, the following line can be used to "trick" the workbook into thinking it was saved. I have it a line before closing the application.
Wkbk.Saved = 1;
Second, and less elegant, but achieves the same result of the user not having the Excel window bombard their screen, I use the following just before opening the workbook:
hExcel.WindowState = 'xlMinimized';

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by