/**
*
* Export
* @throws FileNotFoundException
*/
Public String Excel () throws filenotfoundexception{
Put it under Tomcat.
String filename= "User Management";
String pathname=servletactioncontext.getrequest (). GetSession (). Getservletcontext (). Getrealpath ("") + "\\export\\" +filename+ ". xls";
4.1 Defining a table header
String tablename= "T_user";
String tablecname = selectdataservice.getstring ("Select Comments from user_tab_comments WHERE table_name = '" +tablename+ "‘");
4.2 Get the Chinese comment for the table
List<map> list=selectdataservice.queryforlist ("Select t.column_name,t.comments from User_col_comments t where table_name = ' "+tablename+" ' ");
4.2 Creating a list
List<string> columns=new arraylist<string> ();
Map<string, string> colmap=new treemap<string, string> ();
for (Map map:list) {
Take the comments out of the database and put them in the columns.
Columns.Add (Map.get ("COMMENTS"). toString ());
Put the Chinese note of the repository into the map collection
Colmap.put (Map.get ("COMMENTS"). ToString (), Map.get ("column_name"). ToString ());
}
1 Creating Hssfworkbook
Hssfworkbook hssfwork=new Hssfworkbook ();
2 Creating sheet
Hssfsheet sheet = hssfwork.createsheet (tablecname);
3 Creating a title row (row)
Hssfrow row = sheet.createrow (0);
4 Creating cells
for (int i = 0; i < columns.size (); i++) {
Create a single cell
Hssfcell Createcell = Row.createcell (i);
Put the value taken in the cell.
Createcell.setcellvalue (List.get (i). Get ("COMMENTS"). toString ());
}
5 Remove all data from the database
List<map> datalist=selectdataservice.queryforlist ("select * from" +tablename);
6 Convenience of all data
The I loop is a loop, and the J loop is a loop that loops through each value in a single piece of data.
for (int i = 0; i < datalist.size (); i++) {
Get a row of data
Map Cols=datalist.get (i);
Create a header row
Hssfrow RowData = Sheet.createrow (i+1);
System.out.println ("cols>>>" +cols);
A J loop is a loop that loops through each value in a single piece of data
for (int j = 0; J < Cols.size (); j + +) {
Put it in every cell.
String Fangyuange = Columns.get (j);
Go to English writing section
String ing = colmap.get (fangyuange);
Put in a list of
Object value = cols.get (ing);
Create cells (through rows) into cells
Hssfcell cell = Rowdata.createcell (j);
Cell.setcellvalue (Value.tostring ());
}
}
FileOutputStream fos=new FileOutputStream (pathName);
Put in the path
try {
Hssfwork.write (FOS);
Servletactioncontext.getresponse (). Getwriter (). Write ("OK");
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Fos.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
return null;
}
PDF Schema Database data export