Poi should be a popular Excel tool at present. In the past few days, struts2 and poi have been used in combination to export the EXCEL function. I personally think it is more practical, it's easy to read the code. Let's share my experiences.
1. struts2 Excel File Download Mechanism
In struts2 action, poi and input/output streams are used to send binary data to the client in the form of an over-current. The client browser processes the response. For example, the file download dialog box is displayed.
2 How to Use poi
There are a lot of online documents for poi parsing or generating Excel files. I will not provide the code here.
3. Struts file configuration
<result name="export" type="stream"> <param name="contentType">application/xls;charset=UTF-8</param><param name="contentDisposition">attachment;filename="${downloadFileName}"</param><param name="inputName">excelFile</param></result>
Downloadfilename is the name of the downloaded file, which can be fixed or dynamic. excelfile is the attribute name of inputstream.
4. Main content of action
(1) getter and setter methods of related attributes
private InputStream excelFile;private String downloadFileName=String.valueOf(Calendar.getInstance().getTimeInMillis())+".xls";public InputStream getExcelFile() {return excelFile;}public void setExcelFile(InputStream excelFile) {this.excelFile = excelFile;}public String getDownloadFileName() {return downloadFileName;}public void setDownloadFileName(String downloadFileName) {this.downloadFileName = downloadFileName;}
(2) Export the main Excel Code
Public void excelfile (string startcardnum, string num) throws exception {hssfworkbook workbook = new hssfworkbook (); hssfsheet sheet = workbook. createsheet (); hssfrow ROW = sheet. createrow (0); // set the encoding for both cells and the workbook name; otherwise, garbled workbook will appear when there is a Chinese character. setsheetname (0, "card number info table", hssfworkbook. encoding_utf_16); hssfcell cell1 = row. createcell (short) 0); cell1.setencoding (hssfworkbook. encoding_utf_16); cell1.setcellvalue ("card number"); hssfcell cell2 = row. createcell (short) 1); cell2.setencoding (hssfworkbook. encoding_utf_16); cell2.setcellvalue ("password"); hssfcell cell3 = row. createcell (short) 2); cell3.setencoding (hssfworkbook. encoding_utf_16); cell3.setcellvalue ("Creation Time"); // obtain the list of card numbers that meet the conditions <card> All = cardservice. querycardbycondition (startcardnum, num); iterator <card> it = All. iterator (); Int J = 1; simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); While (it. hasnext () {ROW = sheet. createrow (j); card c = it. next (); row. createcell (short) 0 ). setcellvalue (C. getcardnum (); row. createcell (short) 1 ). setcellvalue (C. getPassword (); row. createcell (short) 2 ). setcellvalue (SDF. format (C. getcreatedate (); j ++;} bytearrayoutputstream baos = new bytearrayoutputstream (); try {workbook. write (baos);} catch (ioexception e) {e. printstacktrace ();} byte [] AA = baos. tobytearray (); excelfile = new bytearrayinputstream (AA, 0, AA. length); try {baos. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}
The exported code is just for your reference. Of course, you can use jxl to export excel.
(3) general method of exporting Excel
public String exportCardInfo() throws Exception {String startNum=this.getRequest().getParameter("startCardNum");String num=this.getRequest().getParameter("num");ExcelFile(startNum,num);this.setDownloadFileName(String.valueOf(Calendar.getInstance().getTimeInMillis())+".xls");return "export";}
Is the code very simple? If you have any questions, write a comment for me.
Use struts2 and poi to export Excel documents