2012-06-26 151 views
2

將結果集寫入CSV文件有CSVWriter類。將結果集轉換爲Excel文件

是否有任何類似的類將結果集輸出寫入EXCEL文件。

編輯:我的結果集的數據將是動態的,所以我只想轉儲結果集數據轉換成Excel文件

EDIT2:參考http://www.codereye.com/2009/09/converting-resultset-to-excel-table-in.html。 它將數據導出到excel工作表,但我需要通過每列的數據類型。我想要移除該依賴關係,以便結果集中的任何內容都應該導出到excelsheet。

+0

如果你想要做的是在Excel中打開CSV數據,只是保存文件用「的.xls」擴展名。然後它在Excel中打開。但是,如果你正在看一個真正的工作簿,我不認爲這是要走的路 – itsols

+0

我想將結果集輸出寫入excel文件。 – happy

+0

你可以利用這個項目:https://sourceforge.net/projects/automatic-report-generator/ –

回答

2

我最近使用OpenCSV,它對我來說工作得很好。這裏,例如,如何寫ResultSet到CSV文件:

java.sql.ResultSet myResultSet = ... 

writer.writeAll(myResultSet, includeHeaders); 
+0

OpenCSV有用於編寫CSV文件的CSVWriter類。我想寫在Excel文件中,以便我可以把密碼放在上面。 – happy

+0

對不起,我誤解了你的問題,正如Stauz說Apache POI可能是唯一的解決方案 – Maxx

0

我不能使用鏈接的例子,所以我使用作爲基地之一,在我自己的方法OpenCSV解析SQL表。

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(); 
} 

}