Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting cell styles in Excel from C# misbehaving

The following code snippet I have is a function that accepts a rectangular matrix of strings and a path to write an xls file. It puts the contents of the matrix into the cells of the Excel sheet, and applies some formatting based on the contents:

public static void WriteXL(string[,] matrix, string path)
{
    XL.Application app = new XL.Application();
    XL.Workbook wbk    = app.Workbooks.Add();
    XL.Worksheet wsht  = wbk.Worksheets.Add();

    for (int i = 0; i < matrix.GetLength(0); i++)
    {
        for (int j = 0; j < matrix.GetLength(1); j++)
        {
            int _i = i + 1;
            int _j = j + 1;

            if (matrix[i, 0] == "Day" || matrix[i, 0] == "Date")
            {
                wsht.Cells[_i, _j].Font.Bold            = true;
                wsht.Cells[_i, _j].Font.Italic          = false;
                wsht.Cells[_i, _j].Style.Font.Name      = "Arial";
                wsht.Cells[_i, _j].Style.Font.Size      = 12;
                wsht.Cells[_i, _j].Style.Interior.Color = NumberFromColor(Color.Yellow);
            }
            else if (j == 0)
            {
                wsht.Cells[_i, _j].Font.Bold            = false;
                wsht.Cells[_i, _j].Font.Italic          = true;
                wsht.Cells[_i, _j].Style.Font.Name      = "Arial";
                wsht.Cells[_i, _j].Style.Font.Size      = 12;
                wsht.Cells[_i, _j].Style.Interior.Color = NumberFromColor(Color.Beige);
            }
            else
            {
                wsht.Cells[_i, _j].Font.Bold            = false;
                wsht.Cells[_i, _j].Font.Italic          = false;
                wsht.Cells[_i, _j].Style.Font.Name      = "Arial";
                wsht.Cells[_i, _j].Style.Font.Size      = 10;
                wsht.Cells[_i, _j].Style.Interior.Color = NumberFromColor(Color.White);
            }

            wsht.Cells[_i, _j].Value = matrix[i, j];
        }
    }

    wbk.SaveAs(path);
    wbk.Close();
    app.Quit();
    app = null;

    GC.Collect();
    GC.WaitForFullGCComplete();
    GC.WaitForPendingFinalizers();
}

So if you can imagine, there are rows that begin with "Day" and "Date" which are delimiting rows, like headings. These rows have bold font and yellow background. With the exception of cells that fall on these delimiting rows, the left-most column has beige background and italic text. The remaining cells are normal text with white background.

When I open the produced xls file, this is not at all what I see. First of all, the entire worksheet is white. Secondly, "Day" (which comes before "Date") is bold but has the wrong size.

It looks like the most recent colour used is applied to the entire worksheet, and font size changes are only applied for the next cell, not the cell I'm currently in.

like image 281
Ozzah Avatar asked Sep 12 '25 10:09

Ozzah


1 Answers

Try dropping the .Style eg

wsht.Cells[_i, _j].Font.Name 

instead of

wsht.Cells[_i, _j].Style.Font.Name 

You want to operate on the cell itself, not on its Style, since any other cells on the sheet which share that Style will also be affected.

like image 165
Tim Williams Avatar answered Sep 15 '25 02:09

Tim Williams