In the management system, often used to download files, especially Excel report creation and download, the following to a simple demonstration, STRUTS2 implementation of the Excel file download function.
Because this experiment is to dynamically create Excel files, you need some jars:
Java read-write Excel package is Apache POI (Project address: http://poi.apache.org/), so you need to obtain the POI jar package, this experiment is using the POI 3.9 stable version.
Apache POI code example address: http://poi.apache.org/spreadsheet/quick-guide.html
1. Configure Struts.xml
<struts> <package name= "Export" namespace= "/export" extends= "Struts-default" > <action name= "*" class= ' excelexportaction ' method= ' {1} '/> <!--test Excel download--> <action name= ' exportexcel ' class= ' E Xcelexportaction "method=" Exportexcel "> <result name=" Success "type=" Stream "> <!-- Download the type of file, if you do not know what format, you can go to Tomcat\conf\web.xml to find--> <param name= "ContentType" >application/vnd.ms-e Xcel</param> <!--return stream Excelstream to the name of the stream variable in action--> <param name= "InputName"
>excelStream</param> <!--attachment The parameters of this position are very special, can be set to download, whether there is a download prompt box, or direct download and so on. filename Specifies the name of the generated file (suitable for dynamically generating file names, such as when making a report, usually to say a few months of statistical data, etc.) for the action variable--> <param name= "Contentdisposition" > attachment;filename=${excelfilename} </param> <param nam E= "BufferSize" >1024</param> </result> </action> </package> </struts>
2. Write Action class
The MAVEN configuration for POI is as follows:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactid>poi</artifactid >
<version>3.9</version>
</dependency>
The action class is as follows:
Import org.apache.poi.hssf.usermodel.*;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.InputStream;
Import Java.text.SimpleDateFormat;
Import Java.util.Date; public class Excelexportaction extends Actionsupport {/** export Excel Test */Public String Exportexcel () {try
{//First step, create a webbook, corresponding to an Excel file Hssfworkbook wb = new Hssfworkbook ();
The second step is to add a sheet to the WebBook, corresponding to the sheet hssfsheet sheet = wb.createsheet in the Excel file ("Test table 1");
In the third step, add the No. 0 row of the header in the sheet, noting that the old version poi has a limit on the number of rows in Excel hssfrow row = sheet.createrow (0);
Step fourth, create a cell style: center Hssfcellstyle style = Wb.createcellstyle ();
Style.setalignment (Hssfcellstyle.align_center);
Step fifth, create the header cell, and set the style Hssfcell cell;
Cell = Row.createcell (0);
Cell.setcellvalue ("Employee work number");
Cell.setcellstyle (style);
Cell = Row.createcell (1); Cell.setcellvalue ("Employee Name");
Cell.setcellstyle (style);
Cell = Row.createcell (2);
Cell.setcellvalue ("subordinate department");
Cell.setcellstyle (style);
Cell = Row.createcell (3);
Cell.setcellvalue ("position");
Cell.setcellstyle (style);
Cell = Row.createcell (4);
Cell.setcellvalue ("Date of entry");
Cell.setcellstyle (style);
Cell = Row.createcell (5);
Cell.setcellvalue ("Remarks");
Cell.setcellstyle (style);
The sixth step is to write the entity data, which is actually used to get date from the database today = new Date ();
Long Aday = 1000l*60*60*24;
SimpleDateFormat FMT = new SimpleDateFormat ("Yyyy-mm-dd");
for (int i = 1; I <= i++) {row = Sheet.createrow (i);
Row.createcell (0). Setcellvalue (i);
Row.createcell (1). Setcellvalue ("staff" + i);
Row.createcell (2). Setcellvalue ("head Office"); Row.createcell(3). Setcellvalue ("ordinary staff");
Row.createcell (4). Setcellvalue (Fmt.format (New Date (Today.gettime () + i * aday)));
Row.createcell (5). Setcellvalue ("staff remark");
//step seventh, save the file to the stream bytearrayoutputstream OS = new Bytearrayoutputstream ();
Wb.write (OS);
byte[] filecontent = Os.tobytearray ();
Bytearrayinputstream is = new Bytearrayinputstream (filecontent); Excelstream = is; File stream excelfilename = "Report.xls";
Set file name for download} catch (Exception e) {e.printstacktrace ();
Return to "success"; }//-------------------------------------------------------------private InputStream excelstream; Output rheological volume private String excelfilename;
Download filename public inputstream Getexcelstream () {return excelstream;
public void Setexcelstream (InputStream excelstream) {this.excelstream = Excelstream;
} Public String Getexcelfilename () {return excelfilename;
} public void Setexcelfilename (String excelfilename) {this.excelfilename = Excelfilename; }
}
Note: This article refers to an online article, but forgot the address, the author deeply apologized.