问题
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) .
回答1:
int indexOfColumn = ...;
worksheet.Column(indexOfColumn).Style.Font.Color.SetColor(Color.Red);
回答2:
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.
来源:https://stackoverflow.com/questions/15673296/how-to-set-the-style-for-an-entire-column-in-epplus