【问题标题】:How to set the style for an entire column in EPPlus?如何在 EPPlus 中设置整列的样式?
【发布时间】:2013-03-18 09:16:57
【问题描述】:

是否可以在 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);

在这两种情况下,我都是在添加一些标题行之后设置颜色,但在添加大部分行之前,我不会在其他任何地方设置颜色。在设置水平对齐方式时,我也得到了类似的意外结果。目前我不得不在单元格级别设置样式。

是我使用不正确还是这是一个错误?使用 EPPlus 3.1.2.0 和 Excel 2010 (14.0.6129.5000)。

【问题讨论】:

    标签: epplus


    【解决方案1】:
     int indexOfColumn = ...;
     worksheet.Column(indexOfColumn).Style.Font.Color.SetColor(Color.Red);
    

    【讨论】:

      【解决方案2】:

      尝试使用范围;我也有使用数字的问题。

      //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
      • EPPLUS 中可能存在此错误,因为如果使用数字(Excel 2016 和 EPPLUS 4.5),我仍然会在其他列中获得重复的样式,但使用范围可以解决此问题。这应该被标记为正确答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      相关资源
      最近更新 更多