【问题标题】:Generate Excel with merged header using NPOI?使用 NPOI 生成带有合并标题的 Excel?
【发布时间】:2020-11-25 17:39:25
【问题描述】:

我尝试使用如下所示的 NPOI 导出 excel 表,但我无法更改背景颜色并设置第二个单元格值。下面是我的代码。

var workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("Commission");
var row = sheet.CreateRow(0);

var bStylehead = workbook.CreateCellStyle();
bStylehead.BorderBottom = BorderStyle.Thin;
bStylehead.BorderLeft = BorderStyle.Thin;
bStylehead.BorderRight = BorderStyle.Thin;
bStylehead.BorderTop = BorderStyle.Thin;
bStylehead.Alignment = HorizontalAlignment.Center;
bStylehead.VerticalAlignment = VerticalAlignment.Center;         
bStylehead.FillBackgroundColor = HSSFColor.Green.Index;

row.CreateCell(0);
row.CreateCell(1);

var r2 = sheet.CreateRow(1);
r2.CreateCell(0, CellType.String).SetCellValue("Name");
r2.CreateCell(1, CellType.String).SetCellValue("Address");
r2.CreateCell(2, CellType.String).SetCellValue("city");
r2.CreateCell(3, CellType.String).SetCellValue("state");

var cra = new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 1);  
var cra1 = new NPOI.SS.Util.CellRangeAddress(0, 0, 2, 3);               
sheet.AddMergedRegion(cra);                         
sheet.AddMergedRegion(cra1);         

ICell cell = sheet.GetRow(0).GetCell(0);
cell.SetCellType(CellType.String);
cell.SetCellValue("Supplier Provided Data");
cell.CellStyle = bStylehead;

ICell cell1  = sheet.GetRow(0).GetCell(1);
cell1.SetCellType(CellType.String);
cell1.SetCellValue("Deal Provided Data");
cell1.CellStyle = bStylehead;       

所需格式:

【问题讨论】:

    标签: c# excel npoi


    【解决方案1】:

    您需要再创建两个单元格,因为您的标题有 2x2 合并单元格:

    供应商提供的数据
    交易提供的数据

    然后,您将能够访问您的第二个标题单元格:

    ICell cell1 = sheet.GetRow(0).GetCell(2);
    

    要设置背景颜色,需要使用FillForegroundColor属性:

    cellStyleColorGreen.FillPattern = FillPattern.SolidForeground;
    cellStyleColorGreen.FillForegroundColor = IndexedColors.Green.Index;
    

    或者制作自定义颜色:

    cellStyleColorCustom.FillPattern = FillPattern.SolidForeground;
    ((XSSFCellStyle)cellStyleColorCustom).SetFillForegroundColor(new XSSFColor(new byte[] { 198, 239, 206 })); 
    

    要克隆一种样式,例如使用单元格样式作为边框并添加背景颜色,可以使用:

    cellStyleColorAndBorder.CloneStyleFrom(cellStyleBorder);
    

    完整代码:

    var workbook = new XSSFWorkbook();
    var sheet = workbook.CreateSheet("Commission");
    var row = sheet.CreateRow(0);
    
    var cellStyleBorder = workbook.CreateCellStyle();
    cellStyleBorder.BorderBottom = BorderStyle.Thin;
    cellStyleBorder.BorderLeft = BorderStyle.Thin;
    cellStyleBorder.BorderRight = BorderStyle.Thin;
    cellStyleBorder.BorderTop = BorderStyle.Thin;
    cellStyleBorder.Alignment = HorizontalAlignment.Center;
    cellStyleBorder.VerticalAlignment = VerticalAlignment.Center;
    
    var cellStyleBorderAndColorGreen = workbook.CreateCellStyle();
    cellStyleBorderAndColorGreen.CloneStyleFrom(cellStyleBorder);
    cellStyleBorderAndColorGreen.FillPattern = FillPattern.SolidForeground;
    ((XSSFCellStyle)cellStyleBorderAndColorGreen).SetFillForegroundColor(new XSSFColor(new byte[] { 198, 239, 206 }));
    
    var cellStyleBorderAndColorYellow = workbook.CreateCellStyle();
    cellStyleBorderAndColorYellow.CloneStyleFrom(cellStyleBorder);
    cellStyleBorderAndColorYellow.FillPattern = FillPattern.SolidForeground;
    ((XSSFCellStyle)cellStyleBorderAndColorYellow).SetFillForegroundColor(new XSSFColor(new byte[] { 255, 235, 156 }));
    
    row.CreateCell(0);
    row.CreateCell(1);
    row.CreateCell(2);
    row.CreateCell(3);
    
    var r2 = sheet.CreateRow(1);
    r2.CreateCell(0, CellType.String).SetCellValue("Name");
    r2.Cells[0].CellStyle = cellStyleBorderAndColorGreen;
    r2.CreateCell(1, CellType.String).SetCellValue("Address");
    r2.Cells[1].CellStyle = cellStyleBorderAndColorGreen;
    r2.CreateCell(2, CellType.String).SetCellValue("city");
    r2.Cells[2].CellStyle = cellStyleBorderAndColorYellow;
    r2.CreateCell(3, CellType.String).SetCellValue("state");
    r2.Cells[3].CellStyle = cellStyleBorderAndColorYellow;
    var cra = new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 1);
    var cra1 = new NPOI.SS.Util.CellRangeAddress(0, 0, 2, 3);
    sheet.AddMergedRegion(cra);
    sheet.AddMergedRegion(cra1);
    
    ICell cell = sheet.GetRow(0).GetCell(0);
    cell.SetCellType(CellType.String);
    cell.SetCellValue("Supplier Provided Data");
    cell.CellStyle = cellStyleBorderAndColorGreen;
    sheet.GetRow(0).GetCell(1).CellStyle = cellStyleBorderAndColorGreen;
    
    ICell cell1 = sheet.GetRow(0).GetCell(2);
    cell1.SetCellType(CellType.String);
    cell1.SetCellValue("Deal Provided Data");
    cell1.CellStyle = cellStyleBorderAndColorYellow;
    sheet.GetRow(0).GetCell(3).CellStyle = cellStyleBorderAndColorYellow;
    
    using (FileStream fs = new FileStream(@"c:\temp\excel\test.xlsx", FileMode.Create, FileAccess.Write))
    {
        workbook.Write(fs);
    }
    

    【讨论】:

    • 非常感谢。箭在目标上。
    【解决方案2】:
    HSSFWorkbook wb = new HSSFWorkbook();
    var sheet = wb.CreateSheet("FileName");
    var cra = new NPOI.SS.Util.CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
    sheet.AddMergedRegion(cra);
    

    希望能帮到你! (使用 lib NPOI)

    【讨论】:

      猜你喜欢
      • 2022-06-29
      • 2020-10-18
      • 2017-05-25
      • 2023-01-08
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      相关资源
      最近更新 更多