如何使用NPOI更改单元格颜色

24
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();
        }
1个回答

31

看看这个例子:

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命名空间中。我已经编辑了我的答案。 - Nino
仍然无法声明它。已将导入添加到问题中。 - meme
1
快速的谷歌搜索告诉我,你应该像这样声明它:ICellStyle boldStyle = workbook.CreateCellStyle();。我现在不在电脑旁边了,请自己找一下。做些小研究吧,伙计 :-) - Nino
有没有一种方法可以一次性为一系列单元格设置样式? 例如,在第一行中,我想为第1列到第5列设置样式。该行的其他单元格将保持不变。 - Bimzee
@Bimzee只需要像这样更改循环:for (int i = 0; i < 4; i++)。 这将仅更改索引为0到4(从第一到第五)的单元格,而不是行中的所有单元格。 - Nino
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接