【问题标题】:How to change cell color with NPOI如何使用 NPOI 更改单元格颜色
【发布时间】:2016-05-05 22:35:11
【问题描述】:
using NPOI.XSSF.UserModel;
using NPOI.XSSF.Model;

using NPOI.HSSF.UserModel;
using NPOI.HSSF.Model;

using NPOI.SS.UserModel;
using NPOI.SS.Util;


(...)

XSSFWorkbook hssfwb;

using (FileStream file = new FileStream(@"D:\VB\XLSX teste com NPOI\XLSX 1\Book1.xlsx", 
     FileMode.Open, FileAccess.Read))
{
    hssfwb = new XSSFWorkbook(file);
    file.Close();
}

ISheet sheet = hssfwb.GetSheetAt(0);
IRow row = sheet.GetRow(0);

ICell cell = row.CreateCell(5);
cell.SetCellValue("test");
cell.CellStyle.FillBackgroundColor = IndexedColors.BrightGreen.Index;
cell.CellStyle.FillPattern = FillPattern.SolidForeground;

using (FileStream file = new FileStream(@"D:\VB\XLSX teste com NPOI\XLSX 1\Book1ee22.xlsx", 
     FileMode.Create, FileAccess.Write))
{
    hssfwb.Write(file);
    file.Close();
}

NPOI 版本:2.1.3.1 我有这段代码,它正在改变孔板的颜色,而不仅仅是单元格...改变单元格填充颜色的正确方法是什么?


这是工作代码,基于下面标记为正确的答案:

XSSFWorkbook hssfwb;
        using (FileStream file = new FileStream(@"D:\Copy D\Tech\VB\XLSX teste com NPOI\XLSX 1\Book1.xlsx", FileMode.Open, FileAccess.Read))
        {
            hssfwb = new XSSFWorkbook(file);
            file.Close();
        }

        ISheet sheet = hssfwb.GetSheetAt(0);
        IRow row = sheet.GetRow(0);


        ICellStyle testeStyle = hssfwb.CreateCellStyle();
        testeStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium;
        testeStyle.FillForegroundColor = IndexedColors.BrightGreen.Index;
        testeStyle.FillPattern = FillPattern.SolidForeground;


        ICell cell = row.CreateCell(5);
        cell.SetCellValue("testeeerere");
        cell.CellStyle = testeStyle;


        using (FileStream file = new FileStream(@"D:\Copy D\Tech\VB\XLSX teste com NPOI\XLSX 1\Book1ee22.xlsx", FileMode.Create, FileAccess.Write))
        {
            hssfwb.Write(file);
            file.Close();
        }

【问题讨论】:

    标签: c# npoi


    【解决方案1】:

    看看这个例子:

    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using NPOI.SS.Util;
    

    (...)

    Row row = sheet.CreateRow(0);
    
    //styling
    Font boldFont = workbook.CreateFont();
    boldFont.Boldweight = (short)FontBoldWeight.BOLD;
    ICellStyle boldStyle = workbook.CreateCellStyle();
    boldStyle.SetFont(boldFont);
    
    boldStyle.BorderBottom = CellBorderType.MEDIUM;
    boldStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index;
    boldStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
    
    
    for (int i = 0; i < columns.Length; i++)
    {
        Cell cell = row.CreateCell(i);
        cell.SetCellValue(columns[i]);
        cell.CellStyle = boldStyle;
    }
    

    在这里,您可以看到如何为一行中的每个单元格应用粗体、背景颜色和边框。在此示例中,columns 是表示列数据的字符串数组;改用你的价值观。

    【讨论】:

    • CellStyle 位于 NPOI.SS.UserModel 命名空间中。我已经编辑了我的答案。
    • 还是不能声明。向问题添加了导入
    • 我的示例使用 NPOI 1.2.3.0。在这个版本中,您应该在 NPOI.SS.UserModel 中找到它,正如您在 APi overview 中看到的那样。你的版本是什么?
    • 呃,我在 2.1.3.1
    • 快速谷歌搜索告诉我你应该像ICellStyle boldStyle = workbook.CreateCellStyle();一样声明它。我不在我的电脑上,尝试自己找到它。做一些小研究,伙计:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2011-06-23
    • 2012-07-16
    • 2021-11-04
    • 2011-08-13
    相关资源
    最近更新 更多