フィルターのクリア

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

39 ビュー (過去 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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by