I want to change all red characters to blue characters in the Excel file and then save it.
I kept trying, but the method seemed to be wrong and didn't work..
Please advise.
color = xlRange.Cells[i, j].Characters[k,1].Font.Color;
if(color == Color.Red)
{
xlRange.Cells[i, j].Characters[k, 1].Font.Color = Color.Blue;
}
I just want to get the color of each character, change it, and save it, but no matter what I do, it doesn't work.
Thank you.
I've attached the more code below.
After opening excel I tried to read the colors.
However, it doesn't behave as desired.
Please advise..
//Create COM Objects. Create a COM object for everything that is referenced
Excel.Application xlApp = new Excel.Application();
int nSheetNo = 3;
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@SelectedFilePath);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[nSheetNo];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = 10;// xlRange.Rows.Count;
int colCount = 10;// xlRange.Columns.Count;
double color;
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
{
strSentence = xlRange.Cells[i, j].Characters;
if (strSentence is not null)
{
LenSentence = strSentence.Count;
for (int k = 0; k < strSentence.Count; k++)
{
color = xlRange.Cells[i, j].Characters[k,1].Font.Color;
if(color == Color.Red)
{
xlRange.Cells[i, j].Characters[k, 1].Font.Color = Color.Blue;
}
}
}
}
}
}
The main problem of your code is color values conversion.
I hope the code below will be helpful for you