npoi workbook 的 cellstyle 创建不能超过4000的解决方法

2023-11-23 19:32

本文主要是介绍npoi workbook 的 cellstyle 创建不能超过4000的解决方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

利用NPOI进行Excel的工作表(Sheet)复制时,如果复制的工作表(Sheet)较多(100个左右),会报告 workbook 的 cellstyle 创建不能超过4000 的错误.
The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

代码如下:

public static void CopySheet(ISheet fromSheet, ISheet toSheet, bool copyValueFlag){//合并区域处理  MergerRegion(fromSheet, toSheet);System.Collections.IEnumerator rows = fromSheet.GetRowEnumerator();while (rows.MoveNext()){IRow row = null;if (fromSheet.Workbook is HSSFWorkbook)row = rows.Current as HSSFRow;elserow = rows.Current as HSSFRow;IRow newRow = toSheet.CreateRow(row.RowNum);CopyRow(fromSheet.Workbook, toSheet.Workbook, row, newRow, copyValueFlag);}}

 

public static void CopyRow(IWorkbook fromWb, IWorkbook toWb, IRow fromRow, IRow toRow, bool copyValueFlag){System.Collections.IEnumerator cells = fromRow.GetEnumerator(); //.GetRowEnumerator();  toRow.Height = fromRow.Height;while (cells.MoveNext()){ICell cell = null;//ICell cell = (wb is HSSFWorkbook) ? cells.Current as HSSFCell : cells.Current as NPOI.XSSF.UserModel.XSSFCell;  if (toWb is HSSFWorkbook)cell = cells.Current as HSSFCell;elsecell = cells.Current as HSSFCell;ICell newCell = toRow.CreateCell(cell.ColumnIndex);CopyCell(fromWb, toWb, cell, newCell, copyValueFlag);}}
public static void CopyCell(IWorkbook fromWb,IWorkbook toWb, ICell srcCell, ICell distCell, bool copyValueFlag){ICellStyle newstyle = toWb.CreateCellStyle();  CopyCellStyle(fromWb,toWb, srcCell.CellStyle, newstyle);//样式  distCell.CellStyle = newstyle;//评论  if (srcCell.CellComment != null){distCell.CellComment = srcCell.CellComment;}// 不同数据类型处理  CellType srcCellType = srcCell.CellType;distCell.SetCellType(srcCellType);if (copyValueFlag){if (srcCellType == CellType.Numeric){if (HSSFDateUtil.IsCellDateFormatted(srcCell)){distCell.SetCellValue(srcCell.DateCellValue);}else{distCell.SetCellValue(srcCell.NumericCellValue);}}else if (srcCellType == CellType.String){distCell.SetCellValue(srcCell.RichStringCellValue);}else if (srcCellType == CellType.Blank){// nothing21  }else if (srcCellType == CellType.Boolean){distCell.SetCellValue(srcCell.BooleanCellValue);}else if (srcCellType == CellType.Error){distCell.SetCellErrorValue(srcCell.ErrorCellValue);}else if (srcCellType == CellType.Formula){distCell.SetCellFormula(srcCell.CellFormula);}else{// nothing29  }}}
public static void CopyCellStyle(IWorkbook fromWb, IWorkbook toWb, ICellStyle fromStyle, ICellStyle toStyle){toStyle.Alignment = fromStyle.Alignment;//边框和边框颜色  toStyle.BorderBottom = fromStyle.BorderBottom;toStyle.BorderLeft = fromStyle.BorderLeft;toStyle.BorderRight = fromStyle.BorderRight;toStyle.BorderTop = fromStyle.BorderTop;toStyle.TopBorderColor = fromStyle.TopBorderColor;toStyle.BottomBorderColor = fromStyle.BottomBorderColor;toStyle.RightBorderColor = fromStyle.RightBorderColor;toStyle.LeftBorderColor = fromStyle.LeftBorderColor;//背景和前景  toStyle.FillBackgroundColor = fromStyle.FillBackgroundColor;toStyle.FillForegroundColor = fromStyle.FillForegroundColor;toStyle.DataFormat = fromStyle.DataFormat;toStyle.FillPattern = fromStyle.FillPattern;toStyle.IsHidden = fromStyle.IsHidden;toStyle.Indention = fromStyle.Indention;//首行缩进  toStyle.IsLocked = fromStyle.IsLocked;toStyle.Rotation = fromStyle.Rotation;//旋转  toStyle.VerticalAlignment = fromStyle.VerticalAlignment;toStyle.WrapText = fromStyle.WrapText;//IFont fromFont = fromStyle.GetFont(fromWb);//字体//toStyle.SetFont(fromFont);}

网上有方法说要把CreateCellStyle放在循环外面,这个方法不适用于复制的工作表(Sheet)较多(100个左右)的场景,且不是解决问题的根本方法.

为了最大限度的复用CellStyle,且控制在4000个之内.构造了一个缓存对象.来缓存创建的CellStyle,所有的CellStyle获取,先通过从缓存取,如果不存在再创建.代码如下:

public class CellStyleCache:ArrayList{public ICellStyle this[ICellStyle fromStyle]{get{foreach (object o in this){ICellStyle toStyle = o as ICellStyle;if (toStyle.Alignment == fromStyle.Alignment//边框和边框颜色  && toStyle.BorderBottom == fromStyle.BorderBottom&& toStyle.BorderLeft == fromStyle.BorderLeft&& toStyle.BorderRight == fromStyle.BorderRight&& toStyle.BorderTop == fromStyle.BorderTop&& toStyle.TopBorderColor == fromStyle.TopBorderColor&& toStyle.BottomBorderColor == fromStyle.BottomBorderColor&& toStyle.RightBorderColor == fromStyle.RightBorderColor&& toStyle.LeftBorderColor == fromStyle.LeftBorderColor//背景和前景  && toStyle.FillBackgroundColor == fromStyle.FillBackgroundColor&& toStyle.FillForegroundColor == fromStyle.FillForegroundColor                            && toStyle.IsHidden == fromStyle.IsHidden                            && toStyle.VerticalAlignment == fromStyle.VerticalAlignment//&& toStyle.WrapText == fromStyle.WrapText//&& toStyle.Indention == fromStyle.Indention//首行缩进  //&& toStyle.IsLocked == fromStyle.IsLocked//&& toStyle.Rotation == fromStyle.Rotation//旋转  //&& toStyle.DataFormat == fromStyle.DataFormat//&& toStyle.FillPattern == fromStyle.FillPattern ){return toStyle;}}return null;}set{this.Add(fromStyle);}}}

public static ICellStyle CreateCellStyle(IWorkbook wb,ICellStyle fromStyle){ICellStyle newStyle = styleCache[fromStyle];if (newStyle == null){newStyle = wb.CreateCellStyle();styleCache[newStyle] = newStyle;}//ICellStyle newStyle = wb.CreateCellStyle(); return newStyle;}

public static void CopyCell(IWorkbook fromWb,IWorkbook toWb, ICell srcCell, ICell distCell, bool copyValueFlag){//ICellStyle newstyle = toWb.CreateCellStyle();  ICellStyle newstyle = CreateCellStyle(toWb, srcCell.CellStyle);//复制样式CopyCellStyle(fromWb,toWb, srcCell.CellStyle, newstyle);//样式  distCell.CellStyle = newstyle;//评论  if (srcCell.CellComment != null){distCell.CellComment = srcCell.CellComment;}// 不同数据类型处理  CellType srcCellType = srcCell.CellType;distCell.SetCellType(srcCellType);if (copyValueFlag){if (srcCellType == CellType.Numeric){if (HSSFDateUtil.IsCellDateFormatted(srcCell)){distCell.SetCellValue(srcCell.DateCellValue);}else{distCell.SetCellValue(srcCell.NumericCellValue);}}else if (srcCellType == CellType.String){distCell.SetCellValue(srcCell.RichStringCellValue);}else if (srcCellType == CellType.Blank){// nothing21  }else if (srcCellType == CellType.Boolean){distCell.SetCellValue(srcCell.BooleanCellValue);}else if (srcCellType == CellType.Error){distCell.SetCellErrorValue(srcCell.ErrorCellValue);}else if (srcCellType == CellType.Formula){distCell.SetCellFormula(srcCell.CellFormula);}else{// nothing29  }}}


测试通过.

这篇关于npoi workbook 的 cellstyle 创建不能超过4000的解决方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/420232

相关文章

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin