How to set excel cell color to red from Matlab?

조회 수: 103 (최근 30일)
Derek Jing
Derek Jing 2011년 3월 17일
댓글: Guillaume 2017년 1월 18일
According to excel color index, the color index for red is 3.
In VBA, I can easily set red color, but in matlab code, I tried different numbers for Interior.ColorIndex, never got red color.
Anyone can tell me why? Thanks a lot.
-Derek

채택된 답변

Jiro Doke
Jiro Doke 2011년 3월 17일
This works for me:
% Connect to Excel
Excel = actxserver('excel.application');
% Get Workbook object
WB = Excel.Workbooks.Open(fullfile(pwd, 'Book1.xlsx'),0,false);
% Set the color of cell "A1" of Sheet 1 to RED
WB.Worksheets.Item(1).Range('A1').Interior.ColorIndex = 3;
% Save Workbook
WB.Save();
% Close Workbook
WB.Close();
% Quit Excel
Excel.Quit();
  댓글 수: 3
JonSnow
JonSnow 2017년 1월 18일
Jiro, has any of this syntax changed over the different versions of Matlab (I am using R2015b)? I have the code:
filename = 'testFile.xlsx';
xlswrite(filename,5)
Excel = actxserver('Excel.application');
Workbooks = Excel.Workbooks;
Excel.visible = 1;
Workbooks.Worksheets.Item(1).Range('A1').Interior.ColorIndex = 3;
But that gives me an error on the last line " No appropriate method, property, or field 'Worksheets' for class 'Interface.000208DB_0000_0000_C000_000000000046'."
Also, what is Item(1)?
Guillaume
Guillaume 2017년 1월 18일
@JonSnow, please start your own question so that whoever answers your problem can get reputation points.
In particular, your code is not the same at all as the one Jiro wrote. The error is correct, Worksheets is not a member of the Workbooks collection (it's a member of Workbook, a completely different class)
Item(1) is a property of the Worksheets collection and returns the first Worksheet in that collection. In VBA you can directly access it as Worksheets(1) as it is the default property of the collection. Matlab does not understand default property, so you have to use its name to access it.
For a more detailed answer, as said, start your own question.

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

추가 답변 (3개)

Walter Roberson
Walter Roberson 2016년 10월 24일
편집: Walter Roberson 2016년 10월 24일
Following along with Jiro's solution:
The list of color index values and associated colors can be found at http://dmcritchie.mvps.org/excel/colors.htm . There are 56 predefined colors.
You can also set the RGB color more directly. If you have R, G, and B in the 0 to 255 range, then:
C = double(R) * 256^0 + double(G) * 256^1 + double(B) * 256^2;
WB.Worksheets.Item(1).Range('A1').Interior.Color = C;
This is backwards of the typical order, but I have confirmed it works.

KRUNAL
KRUNAL 2014년 7월 30일
Hi Jiro,tell me what if the range is unknown? or to put it in a better way, if one wants to color only those cells that have specific strings characters in them. I did try xlsfont, but it does not work if I want to do
xlsfont('file.xls','Sheet1','Find','something','colorindex',3);
In ActiveX server too it is asking for range. What do you suggest to do? Do you think it can be done in some another way?

Prabhakaran thirugnanam
Prabhakaran thirugnanam 2016년 10월 24일
What should be colorindex for other colors?

카테고리

Help CenterFile Exchange에서 MATLAB Functions in Microsoft Excel에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by