java - 如何使用POI 的“事件模型Event API”读取excel数据?

 MINT米田 发布于 2022-10-29 13:25

如题,读取excel打文件的时候,内存老是溢出。后来在网上找了,可以使用事件模型解决这个问题。
但是太难懂了,谁能帮忙解说一下?

1 个回答
  • public static List<Object> test(FileItem fileInput, String sheetName) {
        List<Object> list = null;
        if (null == fileInput) {
            throw new ServiceException("导入文件为空");
        }
        Workbook wb = null;
        InputStream is = null;
        Sheet sheet = null;
        try {
            is = fileInput.getInputStream();
            wb = new XSSFWorkbook(is);
            sheet = wb.getSheet(sheetName);
        } catch (Exception e) {
            throw new ServiceException("上传excel版本文件解析失败");
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    
        if (sheet != null) {
            // 初始化Excel栏目
            List<CellMapping> mappingList = InfrastrUtil.getModColumns(null);
            try {
                list = ExcelUtils.excel2bean(sheet, InfrastrVo.class, mappingList);
            } catch (Exception e) {
                throw new ServiceException(ErrorCode.Upload_File_Error, "Excel解析失败");
            }
        } else {
            throw new ServiceException(ErrorCode.Upload_File_Error, "未找到模板对应sheet");
        }
    
        return list;
    }
    2022-10-31 00:03 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有