Class 使用 NPOI 的 CellStyle 静态值

Class with CellStyle static values for NPOI

[底部更新]

我想做的事情: 有一个 class 和 static readonlyCellStyles 值,所以我可以让我的代码构建 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 的 class,我计划在其中设置 public static readonly 变量,因此我可以调用我需要的那些,我知道如何用方法来实现,但是让它们直接成为 CellStyle 对象在理论上应该让以后的事情变得更容易。这段代码是我试图做的,但显然行不通,因为它不是正确的 syntax/way。

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() 可能会导致意外问题。有更简洁的方法吗?

最后这样做就是解决方案。

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

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

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]);
    }
}