Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the style for an entire column in EPPlus?

Tags:

epplus

Is it possible to set the style for an entire column in EPPlus? I would expect that I could just use the Column method, but when I do I get strange results:

//Sets all cells in all columns to Red
worksheet.Column(1).Style.Font.Color.SetColor(Color.Red);

//Sets some cells in column B to red.
worksheet.Column(2).Style.Font.Color.SetColor(Color.Red);

In both cases I am setting the colour after adding some header rows but before adding the bulk of the rows am an not setting the colour anywhere else. I also get similar unexpected results setting the horizontal alignment. At the moment I am resorting to having to set the style at the cell level.

Am I using it incorrectly or is this a bug? Using EPPlus 3.1.2.0 and Excel 2010 (14.0.6129.5000) .

like image 901
row1 Avatar asked Mar 28 '13 02:03

row1


2 Answers

 int indexOfColumn = ...;
 worksheet.Column(indexOfColumn).Style.Font.Color.SetColor(Color.Red);
like image 125
Jiūtas Brólickaitis Avatar answered Sep 24 '22 00:09

Jiūtas Brólickaitis


Try using ranges; I was having a problem with using numbers as well.

//Get the final row for the column in the worksheet
int finalrows = worksheet.dimension.End.Row;

//Convert into a string for the range.
string ColumnString = "A1:A" + finalrows.ToString();

//Convert the range to the color Red
worksheet.Cells[ColumnString].Style.Font.Color.SetColor(Color.Red);

Hopefully this works,but I didn't try it out.

like image 40
mattcschmidt Avatar answered Sep 22 '22 00:09

mattcschmidt