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

百般思念 提交于 2019-12-19 14:38:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!