【问题标题】:C# Flexcel. Change the colour of background cell. ExcellC# 弹性体。更改背景单元格的颜色。 Excel
【发布时间】:2012-06-10 13:52:30
【问题描述】:

我正在使用 Flexcel 库并想更改表格(Excel 文档)中单元格的颜色。

我该怎么做?我找不到必要的 API。我可以使用 Flexcell 吗?

【问题讨论】:

    标签: c# cell background-color


    【解决方案1】:

    我在尝试设置单元格的背景颜色时遇到了同样的问题。我从未设法直接设置它,但我确实设法设置了单元格的 FillPattern,这实际上设置了背景颜色。

    这里我有一个私有方法,可以设置我计划使用的 4 种不同的背景颜色。请注意,当您添加样式时,您会将它们直接添加到 Excel 文件中。这就是为什么我将 excelFile 传递给此方法,然后将 excelFile 取回。您必须先将样式添加到 excelFile 中,然后才能使用样式。

            private XlsFile SetUpBackgroundColours(XlsFile excelFile)
        {
            var patternStyle = TFlxPatternStyle.Solid;
    
            TFlxFormat format = excelFile.GetDefaultFormat;
            format.FillPattern = new TFlxFillPattern { Pattern = patternStyle, FgColor = Color.Yellow }; //1
            format.VAlignment = TVFlxAlignment.top;
            format.HAlignment = THFlxAlignment.left;
            excelFile.AddFormat(format);
    
            format.FillPattern = new TFlxFillPattern { Pattern = patternStyle, FgColor = Color.LightBlue }; //2
            format.VAlignment = TVFlxAlignment.top;
            format.HAlignment = THFlxAlignment.left;
            excelFile.AddFormat(format);
    
            format.FillPattern = new TFlxFillPattern { Pattern = patternStyle, FgColor = Color.LightGray }; //3
            format.VAlignment = TVFlxAlignment.top;
            format.HAlignment = THFlxAlignment.left;
            excelFile.AddFormat(format);
    
            format.FillPattern = new TFlxFillPattern { Pattern = patternStyle, FgColor = Color.White }; //4
            format.VAlignment = TVFlxAlignment.top;
            format.HAlignment = THFlxAlignment.left;
            excelFile.AddFormat(format);
    
            return excelFile;
        }
    

    这将设置 4 种新样式,然后我可以使用它们添加顺序的索引来引用它们。Flexcel 的索引以 1 开头。

    因此,当您使用 SetCellValue 方法在单元格中添加值时,您可以提供已添加样式的索引以更改背景颜色。以下显示了如何执行此操作的示例:

    excelFile.SetCellValue(rowNumber, columnNumber, "Some text for the cell", 1)
    

    您看到 1 是该方法的最后一个值,这个 1 指的是我使用上面的 SetUpBackgroundColours() 方法添加到 Excel 文件中的第一个样式。

    如果我希望我的单元格具有浅灰色,我会使用 3 而不是 1,如下所示:

    excelFile.SetCellValue(rowNumber, columnNumber, "Some text for the cell", 3)
    

    显示对 SetUpBackgroundColours 的调用的一些附加代码:

    var excelFile = new XlsFile(true);
    excelFile.NewFile(someList.Count() + 1, TExcelFileFormat.v2007);
    var sheetIndex = 0;
    excelFile = SetUpBackgroundColours(excelFile);
    
    excelFile.SetCellValue(1, 1, "Some text for the cell A1", 1) //cell colour will be yellow
    excelFile.SetCellValue(2, 2, "Some text for the cell B1", 2) //cell colour will be lightblue
    

    【讨论】:

    • 这个答案很好,对我来说关键问题是要设置“背景”颜色,你实际上需要一个 TFlxFormat 对象,其 FillPattern.Pattern 是 TFlxPatternStyle.Solid 然后有点令人困惑你需要将 FgColor 属性设置为您想要的背景颜色。开发者在此解释:bgColor vs fgColor
    【解决方案2】:

    我没有使用过那个 API,但是如果你找不到方法,这就是你可以用 C# 做到的方法:

        //http://www.mvps.org/dmcritchie/excel/colors.htm
        private void setCellColor(Microsoft.Office.Interop.Excel.Range cell, int index)
        {
            cell.Interior.ColorIndex = index;
        }
    

    【讨论】:

      【解决方案3】:

      您可以使用 TFlxFormat 类来应用颜色。使用这个类你可以应用任何你想要的格式。

      【讨论】:

        猜你喜欢
        • 2017-09-17
        • 1970-01-01
        • 1970-01-01
        • 2011-09-24
        • 2013-04-12
        • 2021-10-31
        • 2017-03-11
        • 2015-10-21
        • 2016-05-27
        相关资源
        最近更新 更多