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

相关文章

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

如何解决线上平台抽佣高 线下门店客流少的痛点!

目前,许多传统零售店铺正遭遇客源下降的难题。尽管广告推广能带来一定的客流,但其费用昂贵。鉴于此,众多零售商纷纷选择加入像美团、饿了么和抖音这样的大型在线平台,但这些平台的高佣金率导致了利润的大幅缩水。在这样的市场环境下,商家之间的合作网络逐渐成为一种有效的解决方案,通过资源和客户基础的共享,实现共同的利益增长。 以最近在上海兴起的一个跨行业合作平台为例,该平台融合了环保消费积分系统,在短

浅谈主机加固,六种有效的主机加固方法

在数字化时代,数据的价值不言而喻,但随之而来的安全威胁也日益严峻。从勒索病毒到内部泄露,企业的数据安全面临着前所未有的挑战。为了应对这些挑战,一种全新的主机加固解决方案应运而生。 MCK主机加固解决方案,采用先进的安全容器中间件技术,构建起一套内核级的纵深立体防护体系。这一体系突破了传统安全防护的局限,即使在管理员权限被恶意利用的情况下,也能确保服务器的安全稳定运行。 普适主机加固措施:

webm怎么转换成mp4?这几种方法超多人在用!

webm怎么转换成mp4?WebM作为一种新兴的视频编码格式,近年来逐渐进入大众视野,其背后承载着诸多优势,但同时也伴随着不容忽视的局限性,首要挑战在于其兼容性边界,尽管WebM已广泛适应于众多网站与软件平台,但在特定应用环境或老旧设备上,其兼容难题依旧凸显,为用户体验带来不便,再者,WebM格式的非普适性也体现在编辑流程上,由于它并非行业内的通用标准,编辑过程中可能会遭遇格式不兼容的障碍,导致操

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多

【北交大信息所AI-Max2】使用方法

BJTU信息所集群AI_MAX2使用方法 使用的前提是预约到相应的算力卡,拥有登录权限的账号密码,一般为导师组共用一个。 有浏览器、ssh工具就可以。 1.新建集群Terminal 浏览器登陆10.126.62.75 (如果是1集群把75改成66) 交互式开发 执行器选Terminal 密码随便设一个(需记住) 工作空间:私有数据、全部文件 加速器选GeForce_RTX_2080_Ti

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip