배은정
배은정

Reputation: 11

C# Excel Change Char Color

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;
                    }
                }

            }


        }
    }
}

Upvotes: 0

Views: 61

Answers (1)

Peter Kolos
Peter Kolos

Reputation: 46

The main problem of your code is color values conversion.

I hope the code below will be helpful for you

        Excel.Application xlApp = new Excel.Application();
        xlApp.Visible = false;
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@SelectedFilePath);
        Excel._Worksheet xlWorksheet = xlWorkbook.ActiveSheet;
        Excel.Range xlRange = xlWorksheet.UsedRange;

        for (int i = 1; i <= xlRange.Rows.Count; i++)
        {
            for (int j = 1; j <= xlRange.Columns.Count; j++)
            {
                if ((xlRange.Cells[i, j] != null) && (xlRange.Cells[i, j].Value2 != null))
                {
                    if (xlRange.Cells[i, j].Characters != null)
                    {
                        for (int k = 1; k <= xlRange.Cells[i, j].Characters.Count; k++)
                        {
                            int color = Convert.ToInt32(xlRange.Cells[i, j].Characters(k, 1).Font.Color);

                            if (ColorTranslator.FromOle(color) == Color.Red)
                                xlRange.Cells[i, j].Characters(k, 1).Font.Color = ColorTranslator.ToOle(Color.Blue);
                        }
                    }
                }
            }
        }

        xlWorkbook.Save();
        xlApp.Visible = true;

Upvotes: 0

Related Questions