结果集到Excel文件

将结果集写入CSV文件有CSVWriter类。

是否有结果集输出到EXCEL文件的类似的类。

编辑:我的结果集数据将是dynamic的,所以我只想将结果集数据转储到Excel文件

Edit2:Ref http://www.codereye.com/2009/09/converting-resultset-to-excel-table-in.html 。 它将数据导出到Excel工作表,但我需要通过每列的数据types。我想要删除该依赖关系,以便哪些在结果集中,一味地应该导出到excelsheet。

我最近使用OpenCSV ,它为我工作得很好。 在这里,例如,如何将ResultSet写入CSV文件:

 java.sql.ResultSet myResultSet = ... writer.writeAll(myResultSet, includeHeaders); 

您可以从结果中获取数据,并使用Apache POI Jars在Excel文件中进行编写。

我不能使用链接的例子,所以我做了自己的方法作为基础在OpenCSVparsingSQL表。

 public class ResultSetToExcel { public static final int CLOBBUFFERSIZE = 2048; public void toXLS(ResultSet rs, WritableSheet sheet) throws SQLException, IOException, RowsExceededException, WriteException{ List<String> header = getColumnNames(rs); Integer i=0; for(String headCell : header){ sheet.addCell(new Label(i,0,headCell)); i++; } int index = 1; while(rs.next()){ List<String> rowValues = getColumnValues(rs); int j=0; for(String value: rowValues){ sheet.addCell(new Label(j,index,value)); j++; } index++; } } public List<String> getColumnNames(ResultSet rs) throws SQLException { List<String> names = new ArrayList<String>(); ResultSetMetaData metadata = rs.getMetaData(); for (int i = 0; i < metadata.getColumnCount(); i++) { names.add(metadata.getColumnName(i+1)); } return names; } public List<String> getColumnValues(ResultSet rs) throws SQLException, IOException{ List<String> values = new ArrayList<String>(); ResultSetMetaData metadata = rs.getMetaData(); for (int i = 0; i < metadata.getColumnCount(); i++) { values.add(getColumnValue(rs, metadata.getColumnType(i + 1), i + 1)); } return values; } private String handleObject(Object obj){ return obj == null ? "" : String.valueOf(obj); } private String handleBigDecimal(BigDecimal decimal) { return decimal == null ? "" : decimal.toString(); } private String handleLong(ResultSet rs, int columnIndex) throws SQLException { long lv = rs.getLong(columnIndex); return rs.wasNull() ? "" : Long.toString(lv); } private String handleInteger(ResultSet rs, int columnIndex) throws SQLException { int i = rs.getInt(columnIndex); return rs.wasNull() ? "" : Integer.toString(i); } private String handleDate(ResultSet rs, int columnIndex) throws SQLException { try { if(rs.getString(columnIndex) != null){ SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date fecha = new Date( formatter.parse(rs.getString(columnIndex)).getTime()); return formatter.format(fecha); } else return ""; } catch (ParseException e) { throw new SQLException("Fecha erronea o faltante"); } } private String handleTime(Time time) { return time == null ? null : time.toString(); } private String handleTimestamp(Timestamp timestamp) { SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); return timestamp == null ? null : timeFormat.format(timestamp); } private String getColumnValue(ResultSet rs, int colType, int colIndex) throws SQLException, IOException { String value = ""; switch (colType){ case Types.BIT: case Types.JAVA_OBJECT: value = handleObject(rs.getObject(colIndex)); break; case Types.BOOLEAN: boolean b = rs.getBoolean(colIndex); value = Boolean.valueOf(b).toString(); break; case Types.NCLOB: // todo : use rs.getNClob case Types.CLOB: Clob c = rs.getClob(colIndex); if (c != null) { value = read(c); } break; case Types.BIGINT: value = handleLong(rs, colIndex); break; case Types.DECIMAL: case Types.DOUBLE: case Types.FLOAT: case Types.REAL: case Types.NUMERIC: value = handleBigDecimal(rs.getBigDecimal(colIndex)); break; case Types.INTEGER: case Types.TINYINT: case Types.SMALLINT: value = handleInteger(rs, colIndex); break; case Types.DATE: value = handleDate(rs, colIndex); break; case Types.TIME: value = handleTime(rs.getTime(colIndex)); break; case Types.TIMESTAMP: value = handleTimestamp(rs.getTimestamp(colIndex)); break; case Types.NVARCHAR: // todo : use rs.getNString case Types.NCHAR: // todo : use rs.getNString case Types.LONGNVARCHAR: // todo : use rs.getNString case Types.LONGVARCHAR: case Types.VARCHAR: case Types.CHAR: value = rs.getString(colIndex); break; default: value = ""; } if (value == null){ value = ""; } return value; } private static String read(Clob c) throws SQLException, IOException { StringBuilder sb = new StringBuilder( (int) c.length()); Reader r = c.getCharacterStream(); char[] cbuf = new char[CLOBBUFFERSIZE]; int n; while ((n = r.read(cbuf, 0, cbuf.length)) != -1) { sb.append(cbuf, 0, n); } return sb.toString(); } 

}