Fryndorfer
Fryndorfer

Reputation: 91

C# Excel worksheet doesn't format cells

I want to export a table on a website (Silverlight) to an excel worksheet. The export itself works without any problems, but I can't set the format for the cells. This is my code:

if (row.ElementAt(i - 1).Value == null) 
{
    excelWorksheet.Cells[rowCount, colIndex].Value = "";
}
else 
{
    excelWorksheet.Cells[rowCount, colIndex].Value = row.ElementAt(i - 1).Value.ToString();

    switch (row.ElementAt(i - 1).Key)
    {
         case "AE":
             //excelWorksheet.Cells[rowCount, colIndex].Style.Numberformat.Format = "#,###,###.00 €";
             excelWorksheet.SelectedRange[rowCount, colIndex].Style.Numberformat.Format = "#,###,###.00 €";
             break;
         default:
             break;
    }
}

row.ElementAt(i - 1).Key is holding the name of the column, which I need for the switch-case, because I just want to format specific cells.

Both these lines aren't working.

excelWorksheet.Cells[rowCount, colIndex].Style.Numberformat.Format = "#,###,###.00 €"; excelWorksheet.SelectedRange[rowCount, colIndex].Style.Numberformat.Format = "#,###,###.00 €";

I hope you can help me!

Upvotes: 1

Views: 1079

Answers (1)

Fryndorfer
Fryndorfer

Reputation: 91

I solved it now!

It didn't work because you need to set the cell's value AFTER you set the format.

Upvotes: 1

Related Questions