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

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

繁星coding
浏览 528回答 1
1回答

慕虎7371278

public static List<Object> test(FileItem fileInput, String sheetName) {&nbsp; &nbsp; List<Object> list = null;&nbsp; &nbsp; if (null == fileInput) {&nbsp; &nbsp; &nbsp; &nbsp; throw new ServiceException("导入文件为空");&nbsp; &nbsp; }&nbsp; &nbsp; Workbook wb = null;&nbsp; &nbsp; InputStream is = null;&nbsp; &nbsp; Sheet sheet = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; is = fileInput.getInputStream();&nbsp; &nbsp; &nbsp; &nbsp; wb = new XSSFWorkbook(is);&nbsp; &nbsp; &nbsp; &nbsp; sheet = wb.getSheet(sheetName);&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; throw new ServiceException("上传excel版本文件解析失败");&nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; if (is != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (sheet != null) {&nbsp; &nbsp; &nbsp; &nbsp; // 初始化Excel栏目&nbsp; &nbsp; &nbsp; &nbsp; List<CellMapping> mappingList = InfrastrUtil.getModColumns(null);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list = ExcelUtils.excel2bean(sheet, InfrastrVo.class, mappingList);&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new ServiceException(ErrorCode.Upload_File_Error, "Excel解析失败");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; throw new ServiceException(ErrorCode.Upload_File_Error, "未找到模板对应sheet");&nbsp; &nbsp; }&nbsp; &nbsp; return list;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java