日期:2014-05-16 浏览次数:20404 次
public void run(){ String sql = "select * from SFA_CUST_WZ"; try{ FileOutputStream fos = new FileOutputStream("d:\\foo.xls"); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); wb.setSheetName(0, "Matrix"); _pstmt = _conn.getConn().prepareStatement(sql); //取结果集 ResultSet _rs = _pstmt.executeQuery(); //取数据结构 ResultSetMetaData _rsm = _rs.getMetaData(); //取表字段总数 int i = _rsm.getColumnCount(); //获取字段集合 String [] columnName = new String[i]; int j = 0; for (int ri = 1; ri <= i; ri ++,j++) { columnName[j] = _rsm.getColumnName(ri); } //取值并写入xls int k = 0; while(_rs.next()){ HSSFRow row = s.createRow(k); for (j = 0; j < i; j++ ) { HSSFCell cell = row.createCell(j); cell.setCellValue(_rs.getString(columnName[j])); } k++; } wb.write(fos); fos.close(); _rs.close(); }catch(Exception e){e.printStackTrace();} }