Import data from the ResultSet result set to an Excel table

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.