Export all data in the database to Excle

Source: Internet
Author: User

1Workbook WB =NewHssfworkbook ();//Create a workbook2Connection conn = Datasourceutils.getdatasource (). getconnection ();//Get database connection3Statement stmt =conn.createstatement ();4DatabaseMetaData Dbmd = Conn.getmetadata ();//get all the information for the result set Conn5ResultSet Dnset = Dbmd.getcatalogs ();//Get Database directory6          while(Dnset.next ()) {//Traverse all Databases7String dbName = dnset.getstring ("Table_cat");//get all database names8             {9ResultSet Tset = Dbmd.gettables (DbName, DbName,NULL,NewString[] {"TABLE" });Ten                  while(Tset.next ()) {//traverse all tables in the database OneString tname = tset.getstring ("table_name"); AStmt.execute (" Use"+ DbName);// -String sql ="SELECT * from"+Tname; -Sheet Sheet = Wb.createsheet (tname);//Create a sheet for a table theRow row = Sheet.createrow (0);// -ResultSet RSet =stmt.executequery (SQL); -ResultSetMetaData RSMD =Rset.getmetadata (); -                     intCount =Rsmd.getcolumncount (); +list<string> list =NewArraylist<string>(); -                      for(inti =0; I < count; i++) {//get the table header and save it to the cell +String name = Rsmd.getcolumnname (i +1); A Row.createcell (i). Setcellvalue (name); at List.add (name); -                     } -                     inti =0; -                      while(Rset.next ()) {//Save the query data to the cell -i++; -                         intj =0; inRow Row2 =Sheet.createrow (i); -                          for(String s:list) { toString value =rset.getstring (s); +Cell cell =Row2.createcell (j); - Cell.setcellvalue (value); theJ + +; *                         } $                     }Panax NotoginsengFileOutputStream out=NewFileOutputStream ("D:/a.xls");//Write Workbook -Wb.write ( out); the                      out. Close (); +                 } A             } the         } +          -System. out. println ("Success");

Export all data in the database to Excle

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.