Java code
Public static boolean DB2Excel (ResultSet rs, String fileName ){
Boolean flag = false;
WritableWorkbook workBook = null;
WritableSheet sheet = null;
Label label = null;
Try {
// Create an Excel table
String path = AutoStatisticUser. class. getResource (""). getPath ()
+ FileName;
WorkBook = Workbook. createWorkbook (new File (path ));
// Create a sheet in an Excel table
Sheet = workBook. createSheet ("First Sheet", 0 );
// Add data to Excel
ResultSetMetaData rsmd = rs. getMetaData ();
Int columnCount = rsmd. getColumnCount ();
String columnName = null;
Int row = 0;
// Add a title
For (int I = 0; I <columnCount; I ++ ){
ColumnName = rsmd. getColumnName (I + 1 );
Label = new Label (I, row, columnName );
System. out. println ("title:" + I + "---" + row + "---"
+ ColumnName );
Sheet. addCell (label );
}
Row ++;
System. out. println ("the title is successfully written ");
While (rs. next ()){
For (int I = 0; I <columnCount; I ++ ){
Label = new Label (I, row, rs. getString (I + 1 ));
System. out. println ("title:" + I + "---" + row + "---"
+ Rs. getString (I + 1 ));
Sheet. addCell (label );
}
Row ++;
}
System. out. println ("successfully written ");
// Close the file
WorkBook. write ();
System. out. println ("data is successfully written into Excel ");
Flag = true;
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
WorkBook. close ();
} Catch (Exception e2 ){
E2.printStackTrace ();
}
}
Return flag;
}