How to use xlswrite and Excel = actxserver​('Excel.Ap​plication'​) together

조회 수: 10 (최근 30일)
Suresh S
Suresh S 2018년 2월 26일
댓글: Suresh S 2018년 3월 2일
I want to write many values in the Excel sheet with two different color based on the condition. So I am using for loop. with in the for loop xlswrite used for write the value in xls and Excel = actxserver('Excel.Application') used for color the particular cell based on the condition. But I am getting the error that ".xls is not writable.It may be locked by another process." could you help me to resolve the issue. help me to use xlswrite and Excel = actxserver('Excel.Application') together (or) how to write a value in the particular cell by Excel = actxserver('Excel.Application')
The code is below
for intration1=1:row
for intration2=1:column
% logic for Calculate the values
% logic for Calculate the values
range_ip=strcat(sprintf('%c','D'+(intration2)),num2str(10+intration1));
if condition
Excel = actxserver('Excel.Application');
Excel.Workbooks.Open(strcat(pwd,'\',file));
Range= Excel.Range(range_ip);
Range.Interior.ColorIndex = 4;
else
Excel = actxserver('Excel.Application');
Excel.Workbooks.Open(strcat(pwd,'\',file));
Range= Excel.Range(range_ip);
Range.Interior.ColorIndex = 3;
end
end
end
Thanks in Advance,
Regards, Suresh.S

채택된 답변

Meade
Meade 2018년 2월 26일
편집: Stephen23 2018년 2월 28일
xlswrite automatically closes the ACTX session at the end of the call.
Try xlswrite1 from the FEX. This just leaves the connection open so that you can continue to invoke calls (as in your example).
You must close the connection when you are done (example code in the FEX submission). Best of luck.
  댓글 수: 3
Meade
Meade 2018년 2월 28일
How about this:
% Write headers
xlswrite1(FID,YourString,sheetName,'A1');
% Clean up appearance
ExcelSheets_curr = get(ExcelSheets, 'Item', sheetName);
ExcelSheets_curr.Activate;
ExcelSheets_curr.Range('A1').Font.Bold = 1; %Bold
ExcelSheets_curr.Range('A:B').HorizontalAlignment = 2; %Left justify
ExcelSheets_curr.Range('A:B').EntireColumn.AutoFit; %Autofit

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

추가 답변 (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