how to change font color of excel using Matlab (at specific sheet)

Hi:
from the answer in link below, I learned the way to change cell color of Excel sheet using Matlab, however it only tells how to change the cell color, at the 1st default sheet.
now I want change the font color, at specific sheet of a EXCEL file, is it possible to do this in Matlab?
Thanks!
Yu

 채택된 답변

Ravi Raja
Ravi Raja 2024년 2월 27일
% Connect to Excel
excel = actxserver('Excel.Application');
excel.Visible = true; % Set to true if you want Excel to be visible
% Open the Excel file
workbook = excel.Workbooks.Open('C:\Users\raviraja\OneDrive\Documents\Book1.xlsx');
% Get the specific sheet
sheet = workbook.Sheets.Item('Sheet1'); % Replace 'SheetName' with the name of your sheet
% Define the cell range
cellRange = 'E1:E13'; % Change to the cell or range of cells you want to modify
% Get the Range object
range = sheet.Range(cellRange);
% Change font color to RGB value
red = 255; % Change these values according to the RGB color you desire
green = 0;
blue = 0;
rgbColor = red + (green * 256) + (blue * 256^2); % Convert RGB to Excel color value
range.Font.Color = rgbColor;
% Save the changes
workbook.Save;
% Close Excel
excel.Quit;
excel.delete;
In place of "'C:\Users\raviraja\OneDrive\Documents\Book1.xlsx'"
use your file

댓글 수: 5

Hi:
thank you, your suggestion works. I have one further question, in your answer, you define the cell as
" E1:E13", if I want to specify E1, E4, E6, may I know how to do it in the command?
Thanks!
Yu
Try the following
cellRanges = {'E1', 'E4', 'E6'}; % Specify the cells you want to modify
it's not working, below is the test code:
excel = actxserver('Excel.Application');
% excel.Visible = true; % Set to true if you want Excel to be visible
% Open the Excel file
workbook = excel.Workbooks.Open('testFile.xlsx');
% Get the specific sheet
sheet = workbook.Sheets.Item('testSheet'); % Replace 'SheetName' with the name of your sheet
% Define the cell range
cellRange={'A3','A4'};
% Get the Range object
range = sheet.Range(cellRange);
error message occured in the last row:
Error using Interface.000208D8_0000_0000_C000_000000000046/Range
Error: Object returned error code: 0x800A03EC
attached please find the test file.
could you please provide some more suggestions?
Bests,
Yu
addpath 'D:\'
excel = actxserver('Excel.Application');
% excel.Visible = true; % Set to true if you want Excel to be visible
% Open the Excel file
workbook = excel.Workbooks.Open('D:\Book1.xlsx');
% Get the specific sheet
sheet = workbook.Sheets.Item('Sheet1'); % Replace 'SheetName' with the name of your sheet
% Define the cell locations
cellLocations = {'A1', 'A7', 'B1'};
% Change font color to RGB value for each cell
red = 255; % Change these values according to the RGB color you desire
green = 0;
blue = 0;
rgbColor = red + (green * 256) + (blue * 256^2); % Convert RGB to Excel color value
for i = 1:length(cellLocations)
% Get the Range object for the current cell
range = sheet.Range(cellLocations{i});
% Set font color
range.Font.Color = rgbColor;
end
% Save the changes
workbook.Save;
% Close Excel
excel.Quit;
excel.delete;
make sure your excel file is not a read only file.

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

추가 답변 (1개)

Shreeya
Shreeya 2024년 2월 27일
편집: Shreeya 2024년 2월 27일
From the question, I understand that you want to change the color of a cell in a specific sheet of the excel file. In the below code referenced from the linked answer,
WB.Worksheets.Item(1).Range('A1').Interior.ColorIndex = 3;
Item(1) refers to the first sheet in the excel file. You can change this number to index into the sheet you want to see the changes in.

카테고리

질문:

2024년 2월 27일

댓글:

2024년 2월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by