话很少说,粘代码:java
public void expMoreTBID() { try { //导出路径 FileOutputStream os = new FileOutputStream("d:\\test.xlsx"); Workbook workBook = new SXSSFWorkbook(100); // 只在内存中保留100行记录 Sheet sheet = workBook.createSheet(); Connection conn =BaseDao.getConnnection(); try { Statement st = conn.createStatement(); StringBuffer stringBuffer = new StringBuffer("select id,shuhao from bidui b where shuhao in "); stringBuffer.append("(select shuhao from bidui group by shuhao having count(shuhao) > 1)"); ResultSet rs = st.executeQuery(stringBuffer.toString()); int i=0; //这里是表头 Row row1 = sheet.createRow(i++); Cell cell = row1.createCell(0); cell.setCellValue("淘宝ID"); //蔚蓝ID cell = row1.createCell(1); cell.setCellValue("书号"); //书号 cell = row1.createCell(2); while(rs.next()){ //循环出表内容 row1 = sheet.createRow(i++); cell = row1.createCell(0); cell.setCellValue(rs.getString("id")); cell = row1.createCell(1); cell.setCellValue(rs.getString("shuhao")); cell = row1.createCell(2); } //写入 workBook.write(os); st.close(); conn.close(); }catch (Exception e) { e.printStackTrace(); } }catch (Exception e) { e.printStackTrace(); } }
对了,运行这个导出的时候,运行内存会比较大,若是数据量过大【过万】会报 java内存溢出的错误。app
解决办法,设定运行内存:个人电脑是i3 4G 32位的,运行内存设定的是 -Xmx1500M,个人是经过右键运行的配置是在这里配置的,如图:ui