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

조회 수: 26 (최근 30일)
Yu Li
Yu Li 2024년 2월 27일
댓글: Ravi Raja 2024년 2월 29일
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
Ravi Raja
Ravi Raja 2024년 2월 29일
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;
Ravi Raja
Ravi Raja 2024년 2월 29일
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.

카테고리

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