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

利用POI进行Excel的工作表(Sheet)复制时,如果复制的工作表(Sheet)较多(100个左右)或者Excel行数较多(5000+),会报告 workbook 的 cellstyle 创建不能超过4000 的错误.
The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook
(注:网上大多解决方案为把CreateCellStyle放在循环外面,但是对于Excel 的复制 是行不通的,为了最大限度的利用CellStyle,且控制在4000个之内.构造了一个缓存对象.来缓存创建的CellStyle,所有的CellStyle获取,先通过从缓存取,如果不存在再创建.代码如下)

public class CellStyleCache {

    private static List<HSSFCellStyle> list = null;

    public CellStyleCache(){

    }

    public static void Initialized(){
        if(list==null||CacheManager.getCacheInfo("CellStyleCache").getValue()==null){
            list = new ArrayList<HSSFCellStyle>();
            CacheManager.putCacheInfo("CellStyleCache", list, 7200);
        }
    }

    public static void addCellStyle(HSSFCellStyle style){
        list.add(style);
    }

    public static HSSFCellStyle getCellStyle(HSSFCellStyle cellStyle){

        for(HSSFCellStyle style : list){
            if(style.getAlignment() == cellStyle.getAlignment()
                    //边框和边框颜色    
                    && style.getBorderBottom() == cellStyle.getBorderBottom()
                    && style.getBorderLeft() == cellStyle.getBorderLeft()
                    && style.getBorderRight() == cellStyle.getBorderRight()
                    && style.getBorderTop() == cellStyle.getBorderTop()
                    && style.getTopBorderColor() == cellStyle.getTopBorderColor()
                    && style.getBottomBorderColor() == cellStyle.getBottomBorderColor()
                    && style.getRightBorderColor() == cellStyle.getRightBorderColor()
                    && style.getLeftBorderColor() == cellStyle.getLeftBorderColor()
                    //背景和前景    
                    && style.getFillBackgroundColor() == cellStyle.getFillBackgroundColor()
                    && style.getFillForegroundColor() == cellStyle.getFillForegroundColor()
                    && style.getHidden() == cellStyle.getHidden()
                    && style.getVerticalAlignment() == cellStyle.getVerticalAlignment()){
                return style;
            }
        }
        addCellStyle(cellStyle);
        return null;
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值