I. Here I will mainly post the configuration and action writing in struts. As for the persistence layer and business layer, I will not post it again.
2. Use a poi-3.2.jar to export Excel Data
Iii. configuration in the Struts. xml configuration file
<Action name = "chapteraction _ *" class = "chapteraction" method = "{1}">
<Result name = "success" type = "stream">
<Param name = "contenttype"> application/vnd. MS-Excel </param>
<Param name = "inputname"> excelstream </param>
<Param name = "contentdisposition"> attachment; filename = "registry.filename=.xls" </param>
<Param name = "buffersize"> 1024 </param>
</Result>
<Result name = "error">/WEB-INF/page/academic/error. jsp </result>
</Action>
Iv. Export data encapsulation class
Public class expchapter {
// Unit name
Private string chaptername;
// Course name
Private string coursename;
// Excel Header
Private string [] columnnames = new string [] {"Chapter name", "Course name "};
// Method name Array
Private string [] columnmethods = new string [] {"getchaptername", "getcoursename "};
// The get and set methods are omitted.
}
5. Specific Code implementation in action
// ID Array
Private string [] chapterids;
Private inputstream excelstream;
Private string filename;
@ Resource (name = chapterservice. bean_name)
Private chapterservice;
// The get and set methods are omitted.
Public String exp () throws exception {
If (null! = Chapterids ){
// Query the required data from the database
List <chapter> chapters = chapterservice. Find (chapterids );
// Export a data set
List <expchapter> ECs = new arraylist <expchapter> ();
For (Chapter chapter: Chapters ){
Expchapter EC = new expchapter ();
EC. setchaptername (chapter. getchaptertitle ());
EC. setcoursename (chapter. getcourse (). getcoursename ());
ECS. Add (EC );
}
// Create an Excel file
Hssfworkbook workbook = getworkbook (ECS );
If (workbook! = NULL ){
Try {
Calendar c = calendar. getinstance ();
Int year = C. Get (calendar. year );
Int month = C. Get (calendar. month) + 1;
String month _ = new string ("" + month );
If (month <10 ){
Month _ = "0" + month;
}
Int day = C. Get (calendar. day_of_month );
String day _ = new string ("" + Day );
If (day <10 ){
Day _ = "0" + Day;
}
// Step 4: Write the workbook to the inputstream stream defined above -- the name is excelstream, which corresponds to the inputname parameter configured in struts. xml
This. workbook2inputstream (workbook, year + "-" + month _
+ "-" + Day _ + "");
Return success;
} Catch (ioexception e ){
E. printstacktrace ();
Request. setattribute ("message", "Excel creation failed ");
Return Error;
}
} Else {
System. Out. println ("failed to create ");
Return Error;
}
}
Return Error;
}
/*
* Write a workbook to inputstream
*/
Private void workbook2inputstream (hssfworkbook workbook, string filename) throws exception {
This. filename = filename; // set filename
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Workbook. Write (baos );
Baos. Flush ();
Byte [] AA = baos. tobytearray ();
Excelstream = new bytearrayinputstream (AA, 0, AA. Length );
Baos. Close ();
}
/*
* Convert list to Excel worksheet
*/
Private hssfworkbook getworkbook (list <expchapter> expchapters)
Throws securityexception, nosuchmethodexception,
Illegalargumentexception, illegalaccessexception,
Invocationtargetexception {
Hssfworkbook workbook = new hssfworkbook ();
Hssfsheet sheet = Workbook. createsheet ("sheet1 ");
String [] columnnames;
String [] columnmethods;
Expchapter c = expchapters. Get (0 );
Columnnames = C. getcolumnnames ();
Columnmethods = C. getcolumnmethods ();
Hssfrow ROW = sheet. createrow (0 );
Hssfcell cell;
For (INT I = 0; I <columnnames. length; I ++ ){
Cell = row. createcell (I); // create column I
Cell. setcellvalue (New hssfrichtextstring (columnnames [I]);
}
// The data of each row is output below
For (INT I = 0; I <expchapters. Size (); I ++ ){
C = expchapters. Get (I );
Row = sheet. createrow (I + 1); // create row I + 1
For (Int J = 0; j <columnmethods. length; j ++ ){
Cell = row. createcell (j); // create column J.
Method method;
Method = C. getclass (). getmethod (columnmethods [J]); // The reflection mechanism is used here, and the result object returned by the corresponding method is obtained through the method name.
Object OBJ = method. Invoke (C );
Cell. setcellvalue (obj. tostring ());
}
}
Return workbook;
}
6. Page Content omitted
VII. Note: The Code cannot be run directly, but the main part is pasted.
This article from the "Su Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1562268
Export an Excel file in an SSH Project