简体   繁体   中英

Trying to color a whole column of cells in an Excel spreadsheet using NPOI

Below is a C# program I'm using to color cells in an Excel file using NPOI:

string pathSource = @"C:\Users\mvmurthy\Downloads\VOExportTemplate (2).xlsx";
HSSFWorkbook templateWorkbook;
HSSFSheet sheet;
HSSFRow dataRow;

using (var fs = new FileStream(pathSource, FileMode.Open, FileAccess.ReadWrite))
{
    templateWorkbook = new HSSFWorkbook(fs, true);
    sheet = (HSSFSheet)templateWorkbook.GetSheet("ImportTemplate");
    int num = sheet.PhysicalNumberOfRows;
    for (int i=1; i<num; i++)
    {
            dataRow = (HSSFRow)sheet.GetRow(i);                   
            HSSFCellStyle hStyle = (HSSFCellStyle)templateWorkbook.CreateCellStyle();
            hStyle = (HSSFCellStyle)templateWorkbook.CreateCellStyle();
            hStyle.FillForegroundColor = IndexedColors.Red.Index;
            hStyle.FillPattern = FillPattern.SolidForeground;
            dataRow.Cells[9].CellStyle = hStyle;                   
    }
}

using (var fs = new FileStream(pathSource, FileMode.Open, FileAccess.ReadWrite))
{
    templateWorkbook.Write(fs);
}

When I run the above code I am getting the following output, but I want to color only column K:

在此处输入图片说明

What am I doing wrong?

The problem is this line:

dataRow.Cells[9].CellStyle = hStyle;

Cells[n] ignores empty cells, so this will get the 10th (0-based) non-empty cell. In your screenshot, the third and fourth rows have a value in the Description column, so the 10th non-empty cell is in column J, whereas for the rest of the rows, it is in column K.

If you want to get cells by a specific column, you should use GetCell(n) instead. Note, however, that if there is no cell in that column, you will get a null back by default. If needed, you can pass a second parameter to this method to specify that it should create a new cell in that situation instead of returning null.

Try changing the problematic line to this:

dataRow.GetCell(10, MissingCellPolicy.CREATE_NULL_AS_BLANK).CellStyle = hStyle;

As an aside, I notice you are creating a brand new style object for each cell, even though the styling properties are all the same for each. Generally you should not do this, as Excel can only handle a limited number of styles (up to 64,000 according to the Excel Specifications and Limits documentation). Instead, you should only create new style objects where the actual styling properties will differ, then share them amongst all cells that you want to have the same styling.

In other words, move the creation of your hStyle object outside the loop like this:

HSSFCellStyle hStyle = (HSSFCellStyle)templateWorkbook.CreateCellStyle();
hStyle.FillForegroundColor = IndexedColors.Red.Index;
hStyle.FillPattern = FillPattern.SolidForeground;

for (int i = 1; i < num; i++)
{
    dataRow = (HSSFRow)sheet.GetRow(i);
    dataRow.GetCell(10, MissingCellPolicy.CREATE_NULL_AS_BLANK).CellStyle = hStyle;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM