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

相关文章

golang中reflect包的常用方法

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

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

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

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到