xlsread and password protected Excel files

조회 수: 10 (최근 30일)
Les
Les 2014년 9월 11일
댓글: Ehtisham 2021년 6월 28일
There is nothing in the xlsread help file about passwords & the xlsprotect fuction (file exchange) does not work (all it does is hang & you have to shift to the Excel window to enter your password).
Does anybody know how to do this properly from a Matlab file?
  댓글 수: 1
Ehtisham
Ehtisham 2021년 6월 28일
Go and download this code. It will protect the excel file with password. The files won't remain editable unless you unprotect them by providing the password.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2014년 9월 11일
The short answer is that xlsread is not written to handle password protected file. According to Microsoft documentation, when matlab opens a protected workbook Excel should prompt for a password.
You then have two options:
1. Read the excel file yourself, using COM, the save way matlab does. It's actually not that complicated if you're familiar with VBA or COM or .Net. You'd start by instantiating excel, then open the file, the access whatever range you need in whatever workbook:
excel = actxserver('excel.application');
workbook = excel.Workbooks.Open(filename, [], true, [], password);
...
2. Copy xlsread.m, xlsReadCOM.m and the private folder associated with it to a folder and add that folder to matlab path so that it shadows xlsread.
Then, edit your xlsread.m and change the function definition to
function [numericData, textData, rawData, customOutput] = xlsread(file, sheet, range, mode, customFun, password)
After the if nargin >= 5 ... end block add
if nargin >= 6
%do argument validation if you want
else
password = [];
end
Edit the call to xlsReadCOM to:
[numericData, textData, rawData, customOutput] = xlsreadCOM(file, sheet, range, Excel, customFun, password);
Edit xlsReadCOM.m in the private folder you've created, so that the call to OpenExcelWorkbook reads:
[~, workbookHandle] = openExcelWorkbook(Excel, file, readOnly, password);
And finally edit OpenExcelWorkbook.m (again in the private folder) so that the Open call reads:
Excel.workbooks.Open(filename, 0, readOnly, [], password);
You should then be able to read a password protected excel file with:
xlsread(filename, sheet, range, [], [], password)
Note that it will give a you warning about the fifth argument of xlsread, you can safely ignore it.
Also note, that it's untested.
  댓글 수: 1
chengtao liu
chengtao liu 2018년 6월 8일
the second option was tested and it worked. Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Joseph Cheng
Joseph Cheng 2014년 9월 11일
you would have to use activeX. Since getting data from excel using activeX is documented fairly well i'll leave here how to open a file with the password.
[fname pname]=uigetfile('protected.xlsx');
password = 'password';
xlsAPP = actxserver('excel.application');
xlsWB = xlsAPP.Workbooks;
xlsFile = xlsWB.Open([pname fname],[],false,[],password)
xlsFile.Visible = 1;
where i hard coded the password with password and my test xls file was protected.xlsx.
  댓글 수: 1
Joseph Cheng
Joseph Cheng 2014년 9월 11일
there is a function on the file exchange called xlsprotect which looks to be able to unprotect and protect the file again but that maybe problematic in a security standpoint (the xls files were protected for some reason) and if program crash or hangup the reprotect wouldn't occur.

댓글을 달려면 로그인하십시오.


Les
Les 2014년 9월 11일
Then you can use xls read?

Les
Les 2014년 9월 11일
Nope, it still just hangs at the Excel interface

카테고리

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