如何在EPPlus中设置整个列的样式?

12

在EPPlus中是否可以为整个列设置样式?我期望我可以使用Column方法,但是当我这样做时,我得到了奇怪的结果:

//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);
在这两种情况下,我都是在添加一些标题行之后但在添加大部分行之前设置颜色,而不会在其他任何地方设置颜色。我还发现设置水平对齐方式时也出现了类似的意外结果。目前,我只能在单元格级别上设置样式。
我是在错误地使用它吗?还是这是一个 bug?使用 EPPlus 3.1.2.0 和 Excel 2010 (14.0.6129.5000)。
2个回答

13
 int indexOfColumn = ...;
 worksheet.Column(indexOfColumn).Style.Font.Color.SetColor(Color.Red);

8

尝试使用范围;我也曾遇到使用数字时遇到问题。

//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);

希望这个可以用,但我没有试过。


谢谢!这对我很有帮助!如果您想跳过转换,也可以使用 worksheet.Dimension.End.Address - confusedandamused
1
如果使用数字(Excel 2016和EPPLUS 4.5),则在其他列中仍会出现重复样式,因此EPPLUS可能存在此错误,但使用范围可以解决此问题。这应该被标记为正确答案。 - KMC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接