Dictionary<string, ICellStyle> _colorCache = new Dictionary<string, ICellStyle>();//缓存机制,避免重复创建单元格样式

 if (!_colorCache.TryGetValue(value, out ICellStyle cachedStyle))
 {
     // 创建新样式
     cachedStyle = mergedWorkbook.CreateCellStyle();

     // 设置填充模式
     cachedStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;

     var sourececellcolor = sourceCell.CellStyle.FillForegroundColorColor as XSSFColor;

     // 检查工作簿类型并设置颜色
     if (mergedWorkbook is XSSFWorkbook)
     {
         XSSFColor color = sourececellcolor;
         ((XSSFCellStyle)cachedStyle).SetFillForegroundColor(color);
     }


     _colorCache.Add(value, cachedStyle);
 }
 newsheetcell.CellStyle = cachedStyle;

更多推荐