【问题标题】:NPOI setting background color isn't workingNPOI 设置背景颜色不起作用
【发布时间】:2021-01-21 02:51:45
【问题描述】:

我需要设置和删除行的背景颜色。不知何故,当我将样式设置为行时,它不起作用 - 该行仍然没有背景颜色。我尝试设置单个单元格的 CellStyle 属性并使用 HSSFStyle。结果是一样的——没有背景颜色。代码如下:

        XSSFCellStyle style = (XSSFCellStyle)_myBook.CreateCellStyle();
        // Define cell style according to input color parameter
        XSSFColor colorToFill;
        switch (color)
        {
            default:
                colorToFill = new XSSFColor(Color.Gray);
                break;
        }
        style.SetFillBackgroundColor(colorToFill);
        for (int i = From; i <= To; i++)
        {
            var row = GetCreateRow(i);
            row.RowStyle = style;
        }

附:文档以 XSSF 格式打开

【问题讨论】:

    标签: c# .net npoi


    【解决方案1】:

    啊,我们每个人都有部分答案! (转换为 XSSFCellStyle 是我所缺少的,因为 ICellStyle 没有 SetFill 方法,所以我无法获得自定义颜色,只有一个索引颜色...)

    你需要做两件事:

    1. 调用 SetFillForegroundColor,而不是 SetFillBackgroundColor。
    2. 设置 style.FillPattern = FillPattern.SolidForeground;

    显然,Excel中的填充是双色的,所以当你想改变填充时,你必须设置一个图案,因为默认是一个没有填充的单元格,这意味着没有FillPattern。 SolidForeground 是基本的填充图案,这意味着您必须设置填充的前景色,而不是填充的背景色。有点反直觉,但“填充”指的是“单元格的背景颜色”,所以你可以把它想象成“SetBackgroundForegroundColor”/“SetBackgroundBackgroundColor”

    【讨论】:

    • 似乎你注定要成为......对于那个问题:D
    • 到目前为止,这是我看到的关于为什么设置背景颜色使用名为“SetFillForegroundColor”的方法的最佳解释。终于有道理了!
    • 简单而有效,但单独找到这样的东西将花费几个小时和/或头发。谢谢!
    • 尝试这个我遇到了另一个问题:如果你想将此样式应用于一行,那么每个单元格都会被着色,除了带有文本的单元格。
    【解决方案2】:
    Dim conn As New SqlConnection("xxxxxxx")
    conn.Open()
    
    Dim a As SqlDataAdapter = New SqlDataAdapter(XXXXXX, conn)
    Dim dt As DataTable = New DataTable()
    a.Fill(dt)
    conn.Close()
    conn.Dispose()
    
    Using fs = New FileStream("C:\123.xlsx", FileMode.Append, FileAccess.Write)
    
                Dim workbook As IWorkbook = New XSSFWorkbook()
                Dim excelSheet As ISheet = workbook.CreateSheet("Sheet1")
                Dim columns As List(Of String) = New List(Of String)()
    
                Dim styleOrange As ICellStyle = workbook.CreateCellStyle    
                Dim row As IRow = excelSheet.CreateRow(0)    
                Dim columnIndex As Integer = 0
    

    块引用 对于列标题

                For Each column As Data.DataColumn In dt.Columns
                    columns.Add(column.ColumnName)
                    row.CreateCell(columnIndex).SetCellValue(column.ColumnName)
                    columnIndex += 1
                Next
    
                
    

    Blockquote 'for Record and rows to fill excel

                Dim rowIndex As Integer = 1    
                For Each dsrow In dt.Rows
    
                    row = excelSheet.CreateRow(rowIndex)    
                    Dim cellIndex As Integer = 0 
    
                    For Each col In columns    
                        Dim cell = row.CreateCell(cellIndex)
                        cell.SetCellValue(dsrow(col).ToString())    
                   
                        styleOrange.FillPattern = FillPattern.SolidForeground
                        styleOrange.FillForegroundColor = IndexedColors.Red.Index
    
                        cell.CellStyle = styleOrange
                        cellIndex += 1
    
                    Next
    
                    rowIndex += 1
                Next              
    
                workbook.Write(fs)
            End Using
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 2017-12-20
      • 2019-07-07
      • 2010-11-30
      • 2017-04-23
      • 2013-06-01
      • 2014-07-20
      • 2022-01-05
      相关资源
      最近更新 更多