【问题标题】:Class with CellStyle static values for NPOINPOI 的具有 CellStyle 静态值的类
【发布时间】:2021-05-05 12:02:12
【问题描述】:

[底部更新]

我想做的事: 为CellStyles 设置一个具有static readonly 值的类,这样我可以让构建excel 文件的代码如下所示:

ICellStyle headerStyle1 = workbook.CreateCellStyle();
headerStyle1 = ExcelStyles.header1;

而不是这样:

headerStyle1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium;
headerStyle1.BorderTop = NPOI.SS.UserModel.BorderStyle.Medium;
headerStyle1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Medium;
headerStyle1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;

这将使以后更容易阅读、理解和维护。

到目前为止我所做的: 我创建了一个名为 ExcelStyles.cs 的类,我计划在其中使用 public static readonly 变量,以便我可以调用我需要的变量,我知道如何用方法来做,但是让它们直接成为CellStyle 对象在理论上应该会让以后的事情变得更容易。这段代码是我试图做的,但显然不起作用,因为它不是正确的语法/方法。

class ExcelStyles
{
   public static readonly ICellStyle header1 = 
   { 
      header1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium;
      header1.BorderTop = NPOI.SS.UserModel.BorderStyle.Medium;
   }
}

问题:我不知道如何正确执行此操作,并且我已经将头撞到墙上一段时间试图弄清楚我应该如何搜索这个但没有太大成功,不知道能不能用NPOI

我在用什么:Visual Studio 2019、Windows Forms、C#、.NET Framework 4.7.2、NPOI 2.5.3,它是一个桌面应用程序

更新:再摆弄一下后,我得到了这段代码:

class ExcelStyles
{
    public static readonly ICellStyle header1 = ((XSSFWorkbook)new XSSFWorkbook()).CreateCellStyle();
    static ExcelStyles()
    {
        header1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium;
        header1.BorderTop = NPOI.SS.UserModel.BorderStyle.Medium;
        header1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Medium;
        header1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
        header1.FillPattern = FillPattern.SolidForeground;
        header1.VerticalAlignment = VerticalAlignment.Center;
    }
}

我这样称呼它:

ICellStyle headerStyle1 = workbook.CreateCellStyle();
headerStyle1.CloneStyleFrom(ExcelStyles.header1);

所以问题发生了变化,到目前为止,测试工作与预期一样,但我担心((XSSFWorkbook)new XSSFWorkbook()).CreateCellStyle() 可能会导致意外问题。有没有更清洁的方法来做到这一点?

【问题讨论】:

    标签: c# static readonly npoi


    【解决方案1】:

    最终这样做是解决方案。

    public static readonly ICellStyle header1 = new XSSFWorkbook().CreateCellStyle();
    
    static ExcelStyles()
    {
       header1.FillPattern = FillPattern.SolidForeground;
       header1.FillForegroundColor = color;
       header1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium;
       header1.BorderTop = NPOI.SS.UserModel.BorderStyle.Medium;
       header1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Medium;
       header1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
       header1.VerticalAlignment = VerticalAlignment.Center;
       header1.Alignment = HorizontalAlignment.Left;
    }
    

    然后这个来应用它

    ICellStyle headerStyle1 = workbook.CreateCellStyle();
    headerStyle1.CloneStyleFrom(ExcelStyles.header1);```
    

    【讨论】:

      【解决方案2】:

      另一种选择是使用枚举和字典。

      public enum TextType
      {
          Header1,
          ...
      }
      
      
      public class ExcelStyles
      {
          public static Dictionary<TextType, CellStyle> CreateDefaultStyels(HSSFWorkbook hssfworkbook)
          {
              {
                      CellStyle style = hssfworkbook.CreateCellStyle();
                      style.SetFont(getFont(12, 3000, false, false));
                      style.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;
                      style.FillPattern = FillPatternType.SOLID_FOREGROUND;
                      style.VerticalAlignment = VerticalAlignment.CENTER;
      
                      styles.Add(TextType.Header1, style);
              }
          }
      }
      

      你可以这样使用它:

      public MyExcelReport
      {
          private Dictionary<TextType, CellStyle> Styles { get; set; }
      
          MyExcelReport()
          {
              Styles = ExcelStyles.CreateDefaultStyels(Hssfworkbook);
          }
          
          public void Set()
          {
              AddCell("Simple text", 0, NextRow, Styles[TextType.Header1]);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-19
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        相关资源
        最近更新 更多