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

相关文章

Spring三级缓存解决循环依赖的解析过程

《Spring三级缓存解决循环依赖的解析过程》:本文主要介绍Spring三级缓存解决循环依赖的解析过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、循环依赖场景二、三级缓存定义三、解决流程(以ServiceA和ServiceB为例)四、关键机制详解五、设计约

解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException: org.junit.Test问题

《解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException:org.junit.Test问题》:本文主要介绍解决tomcat启动时报Junit相... 目录tomcat启动时报Junit相关错误Java.lang.ClassNotFoundException

解决Maven项目报错:failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题

《解决Maven项目报错:failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题》这篇文章主要介... 目录Maven项目报错:failed to execute goal org.apache.maven.pl

nginx负载均衡及详细配置方法

《nginx负载均衡及详细配置方法》Nginx作为一种高效的Web服务器和反向代理服务器,广泛应用于网站的负载均衡中,:本文主要介绍nginx负载均衡及详细配置,需要的朋友可以参考下... 目录一、 nginx负载均衡策略1.1 基本负载均衡策略1.2 第三方策略1.3 策略对比二、 nginx配置2.1

Java调用Python的四种方法小结

《Java调用Python的四种方法小结》在现代开发中,结合不同编程语言的优势往往能达到事半功倍的效果,本文将详细介绍四种在Java中调用Python的方法,并推荐一种最常用且实用的方法,希望对大家有... 目录一、在Java类中直接执行python语句二、在Java中直接调用Python脚本三、使用Run

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法

《SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法》在SQLyog中执行存储过程时出现的前置缩进问题,实际上反映了SQLyog对SQL语句解析的一个特殊行为,本文给大家介绍了详... 目录问题根源正确写法示例永久解决方案为什么命令行不受影响?最佳实践建议问题根源SQLyog的语句分

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见