【问题标题】:C# changing backColor of a cell using the NPOI libraryC# 使用 NPOI 库更改单元格的背景颜色
【发布时间】:2018-07-24 21:52:22
【问题描述】:

您好,我正在尝试使用 c# 库 NPOI 编写一个 .xls 文件,我已经能够创建该文件,但是当我尝试更改工作表中某些单元格的背景颜色时,我不知道为什么表格的所有单元格都变色了,这让我发疯了,请你帮帮我。

这是我正在使用的代码:

// Creation of XLS

// I create a new excel work
HSSFWorkbook workbook = new HSSFWorkbook();

int rowNumber = 0;
//I add to the excel work a sheet of work
ISheet sheet = workbook.CreateSheet("Sheet 1");

// I create the Header of the sheet
var headerRow = sheet.CreateRow(0);

headerRow.CreateCell(0).SetCellValue("Famiglia");
headerRow.CreateCell(1).SetCellValue("Quantità Tagliata Fogli");
headerRow.CreateCell(2).SetCellValue("Lotto Medio di Produzione Fogli");
headerRow.CreateCell(3).SetCellValue("Quantità Scarto Medio(pezzi)");
headerRow.CreateCell(4).SetCellValue("Valore Scarti Euro");
headerRow.CreateCell(5).SetCellValue("% Scarto");
headerRow.CreateCell(6).SetCellValue(" Lead Time Medio Produttivo");

for (int c = 0; c < headerRow.Cells.Count; c++)
{                               
    headerRow.Cells[0].CellStyle.FillForegroundColor= IndexedColors.LightBlue.Index;
    headerRow.Cells[0].CellStyle.FillPattern = FillPattern.SolidForeground;
}

// Now what I have to do is to write the data in to the cells creating so a new record
rowNumber++;
IRow row = sheet.CreateRow(rowNumber);                    

row.CreateCell(0).SetCellValue(f.family);
row.CreateCell(1).SetCellValue(f.QuantitàTagliataFogli);
row.CreateCell(2).SetCellValue(f.LottoMedioProduzioneFogli);
row.CreateCell(3).SetCellValue(f.QuantitàScartoMedioInPezzi);
row.CreateCell(4).SetCellValue(f.ValoreScartoInEuro);
row.CreateCell(5).SetCellValue(f.ScartoMedio);
row.CreateCell(6).SetCellValue(f.LeadTimeMedioProduttivo);

// Now I have to try to write the file XLS

MemoryStream output = new MemoryStream();
workbook.Write(output);

SaveFileDialog SaveFileDialog = new SaveFileDialog();
SaveFileDialog.Title = "Save As...";
SaveFileDialog.Filter = "xls File (*.xls)|*.xls";
SaveFileDialog.InitialDirectory = @"C:\";

if (SaveFileDialog.ShowDialog() == DialogResult.OK)
{
    FileStream fs = new FileStream(SaveFileDialog.FileName, FileMode.Create);
    // Create the writer for data.
    workbook.Write(fs);
    fs.Close();
}

我本来希望只有第一行的单元格带有 backColor 浅蓝色,而不是我得到了具有该颜色的工作表的所有单元格。

为什么?!?

【问题讨论】:

    标签: c# excel npoi


    【解决方案1】:

    请尝试以下方法。我已经分离了字体、样式,最后使用了你的 for 循环分配给 Cell 样式也修复了 for 循环内部的错误,你每次都只将样式分配给 Cells[0]。

    HSSFFont headerFont = (HSSFFont)workbook.CreateFont();
    headerFont.FontHeightInPoints = (short)12;
    headerFont.FontName = "Arial";
    headerFont.Color = IndexedColors.White.Index;
    headerFont.IsBold = true;
    headerFont.IsItalic = false;
    headerFont.Boldweight = 700;
    
    HSSFCellStyle headerStyle = (HSSFCellStyle)workbook.CreateCellStyle();
    headerStyle.WrapText = true;
    headerStyle.FillForegroundColor = IndexedColors.LightBlue.Index;
    headerStyle.FillPattern = FillPattern.SolidForeground;
    headerStyle.Alignment = HorizontalAlignment.Center;
    headerStyle.VerticalAlignment = VerticalAlignment.Center;
    headerStyle.BorderBottom = BorderStyle.Thin;
    headerStyle.BorderTop = BorderStyle.Thin;
    headerStyle.BorderLeft = BorderStyle.Thin;
    headerStyle.BorderRight = BorderStyle.Thin;
    headerStyle.SetFont(headerFont);
    
    for (int c = 0; c < headerRow.Cells.Count; c++)
    {                               
        headerRow.Cells[c].CellStyle = headerStyle;
    }
    

    【讨论】:

    • 这会将所有标题单元格颜色创建为浅蓝色,这是 OP 不想要的。 OP 只想填充选定的单元格颜色。
    【解决方案2】:

    替换这些行:

    for (int c = 0; c < headerRow.Cells.Count; c++)
    {
        headerRow.Cells[0].CellStyle.FillForegroundColor= IndexedColors.LightBlue.Index;
        headerRow.Cells[0].CellStyle.FillPattern = FillPattern.SolidForeground;
    }
    

    与:

    HSSFCellStyle cellStyleBlue = (HSSFCellStyle)workbook.CreateCellStyle();
    cellStyleBlue.FillForegroundColor = IndexedColors.LightBlue.Index;
    cellStyleBlue.FillPattern = FillPattern.SolidForeground;
    
    for (int c = 0; c < headerRow.Cells.Count; c++)
    {
        headerRow.Cells[c].CellStyle = cellStyleBlue;
    }
    

    然后,只有第一行的单元格会应用单元格样式。

    【讨论】:

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