日期:2014-05-20 浏览次数:20780 次
try { Class.forName(driverName); } catch(ClassNotFoundException e) { System.out.println("加载驱动程序失败!"); } try { con=DriverManager.getConnection(dbURL,userName,userPwd); // Statement sql=con.createStatement(); } catch(Exception e) { System.out.println("初始化数据库连接异常..." + e.getMessage()); } } public void execExport() { // System.out.println(); System.out.println("开始导出数据库......"); try { this.exportTable("1",""); this.exportTable("2","" ); } catch (Exception e) { System.out.println("检索库中所有表时异常..." + e.getMessage()); } } public void exportTable(String page,String sql) { System.out.println("正在导出表:pinspect"); try { Statement selectTable = con.createStatement(); sql="select * from '"+sjk.toString()+"'"; ResultSet rset = selectTable.executeQuery(sql); this.p = page; this.writeTable(rset, "客户信息"+page+".xls"); } catch (Exception e) { System.out.println("导出表时异常..." + e.getMessage()); } } private void writeTable(ResultSet rs, String xlsName) { try { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); // workbook.setSheetName(0,xlsName,HSSFWorkbook.ENCODING_UTF_16); workbook.setSheetName(0, "分页" + String.valueOf(0)); // (short) HSSFWorkbook.ENCODING_UTF_16); HSSFRow row = sheet.createRow((short) 0); HSSFCell cell = null; ResultSetMetaData md = rs.getMetaData(); // 写字段 int nColumn = md.getColumnCount(); for (int i = 1; i <= nColumn; i++) { cell = row.createCell((short) (i - 1)); cell.setCellType(HSSFCell.CELL_TYPE_STRING); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(md.getColumnLabel(i)+"您好"); switch (i) { case 1 :cell.setCellValue("请求的编号"); break; case 2 :cell.setCellValue("当前请求的执行方法数量"); break; case 3 :cell.setCellValue("当前请求的执行方法总时间"); break; case 4 :cell.setCellValue("当前请求的执行方法平均时间"); break; } } // 写记录 int iRow = 1; String uuidName = null; while (rs.next()) { row = sheet.createRow((short) iRow); for (int j = 1; j <= nColumn; j++) { cell = row.createCell((short) (j - 1)); cell.setCellType(HSSFCell.CELL_TYPE_STRING); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); String val = null; if(this.p.equals("1")) { if(j ==1) { if(rs.getObject(j).toString().equals(uuidName)) { val = " "; } else { uuidName = rs.getObject(j).toString(); val = uuidName; } } else if(j == 2) { val = rs.getObject(j).toString().split("classes/")[1]; } else { val = rs.getObject(j).toString(); } }else if(j == 1) { val = rs.getObject(j).toString().split("classes/")[1]; } else { val = rs.getObject(j).toString(); } if (val == null) { cell.setCellValue("NULL"); } else { cell.setCellValue(val); } } iRow++; } // 保存文件 FileOutputStream fOut = new FileOutputStream(xlsName); workbook.write(fOut); fOut.flush(); fOut.close(); System.out.println("导出数据完成!"); } catch (Exception e) { System.out.println("写表时异常..." + e.getMessage()); } }