NPOI操作EXCEL合并单元格及单元格背景色
/// <summary> /// XLSX /// </summary> static void CreateXLSX() { IWorkbook book = new XSSFWorkbook(); ISheet sheet = book.CreateSheet("Sheet1"); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 3, 0, 3));//合并单元格 ICellStyle style = book.CreateCellStyle(); style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Blue.Index; style.FillPattern = FillPattern.SolidForeground; sheet.CreateRow(5).CreateCell(5).CellStyle = style; //设置背景色 using FileStream fileStream = new FileStream(@"C:\Users\Administrator\Desktop\MergedAndColor.xlsx", FileMode.CreateNew, FileAccess.ReadWrite); book.Write(fileStream); } /// <summary> /// XLS /// </summary> static void CreateXLS() { HSSFWorkbook book = new HSSFWorkbook(); ISheet sheet = book.CreateSheet("Sheet1"); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 3, 0, 3));//合并单元格 ICellStyle style = book.CreateCellStyle(); style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Blue.Index; style.FillPattern = FillPattern.SolidForeground; sheet.CreateRow(5).CreateCell(5).CellStyle = style; using FileStream fileStream = new FileStream(@"C:\Users\Administrator\Desktop\MergedAndColor.xls", FileMode.CreateNew, FileAccess.ReadWrite); book.Write(fileStream); }