how to change font color of excel using Matlab (at specific sheet)
26 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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;
更多回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!