POI处理超过65536条记录

来源:互联网 发布:软件著作权保护中心 编辑:程序博客网 时间:2024/06/06 05:17

03格式的excel文件(.xls)最多只支持65536行,所以当需要处理的数据超过65536时,便不能使用HSSFWorkbook了,即不能用03格式的excel了

07格式的excel文件(.xlsx)对应的方法是:XSSFWorkbook

先读取一个表头:

SXSSFWorkbook wb2 = new SXSSFWorkbook(6000);File file = new File("C:/Users/DLTB/Desktop/1.xlsx");  //File file = new File( request.getSession().getServletContext().getRealPath("/")+"/resources/model/logmodel.xlsx");  XSSFWorkbook x = null;try {InputStream is = new FileInputStream(file);x = new XSSFWorkbook(new BufferedInputStream(is));wb2 = new SXSSFWorkbook(x,6000);} catch (Exception e) {e.printStackTrace();} 

读取该文件的第一张表:

Sheet sheet = wb.getSheetAt(0);

<span style="white-space:pre"></span>List<Log> logList = logMapper.getLogList();//写入的数据(随便改)for (int rownum = 0; rownum < logList.size(); rownum++) {Log log = logList.get(rownum);int i = 0;Row row1 = sheet.createRow(rownum + 3);row1.createCell(i++).setCellValue(log.getCol1());/<span style="font-family: Arial, Helvetica, sans-serif;">/写入的数据(随便改)</span>row1.createCell(i++).setCellValue(log.getCol2());//写入的数据(随便改)}
浏览器生成下载链接:

<span style="white-space:pre"></span>String fileName = "操作记录";fileName += ".xlsx";response.setContentType("application/vnd.ms-excel");try {response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));OutputStream ouputStream;ouputStream = response.getOutputStream();wb.write(ouputStream);ouputStream.flush();ouputStream.close();} catch (IOException e) {e.printStackTrace();}



0 0
原创粉丝点击