話很少說,粘代碼: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