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