Struts.xml Configuration
<!--download-
<action name= "Download" class= "Nam.menu.downloadAC" >
<result name= "input" type= "stream" >
<param name= "ContentType" >application/vnd.ms-excel</param><!--indicates the type of file--
<param name= "InputName" >fis</param><!--the name of the method that indicates how the file was downloaded--
<!--the Attachment property is to be opened separately when opened, instead of opening in the browser, the default is inline. Indicates the name of the file at download--
<param name= "contentdisposition" >attachment;filename= "${exportfilename}.xls" </param>
<param name= "BufferSize" >4096</param>
</result>
</action>
Java Action Layer Code
Import Java.io.inputstream;import Java.io.unsupportedencodingexception;public class Downloadac extends basemenudataac{ Private String IDs; Private InputStream FIS = null; Private String exportfilename = null; Public String GetIDs () {return IDs;} public void Setids (String ids) {this.ids = IDs;} Public InputStream Getfis () {return FIS;} public void Setfis (InputStream fis) {This.fis = FIS;} Public String Getexportfilename () {return exportfilename;} public void Setexportfilename (String exportfilename) {this.exportfilename = Exportfilename;} Public String Execute () {string name = "table name"; try {setexportfilename (new string (Name.getbytes ("GB2312"), "iso8859-1");} catch (Unsupportedencodingexception e) {//Todo:handle exceptione.printstacktrace ();} FIS = dowloadbs.exportcostinfolist (IDs); return INPUT;} }
BS Layer
Public InputStream exportcostinfolist (String IDs) {//Create Excel Export object Poiexcelobjectadvance Excel = new Poiexcelobjectadvance (); Poiexcelsheet Sheet1 = excel.createsheet ("table");//Initialize the list of exported regions list<poiexcelcontent> arealist = new arraylist< Poiexcelcontent> ();//Set font Hssffont font = Excel.createdefaultfont (); Font.setfontname ("Arial"); Font.setboldweight ( Hssffont.boldweight_bold); font.setfontheightinpoints ((short) 10); Hssffont Font1=excel.createdefaultfont (); Font1.setfontname ("blackbody"); font1.setfontheightinpoints (short); int[] cols = {Ten, ten, 30};sheet1.setcolumnwidth (cols);//Initialize the contents list of the Export area 1 list<string[]> title1list = new Arraylist<s Tring[]> ();//Add export contents to the Export Area 1 Content list string[] titles = {"Ordinal", "MINGCHENGF", "nianling", "Jiankang};title1list.add (titles) ; Poiexcelcontent title1 = new Poiexcelcontent (0, 0, title1list, font1); Arealist.add (Title1); list<date> data = downloaddatadao.queryexportmenudatalist (IDS); list<string[]> title2list = new arraylist<string[]> (); for (inti = 0; I < data.size (); i++) {string[] content = new String[titles.length];content[0] = string.valueof (i + 1); content[1] = Data.get (i). GetName (); CONTENT[2] = Data.get (i). Getage (); content[3] = Data.get (i). Getjakang (); Title2list.add (content);} Poiexcelcontent title2 = new Poiexcelcontent (0, 1, title2list, font); Arealist.add (title2); Sheet1.setcontentlist ( arealist); return Excel.toinputstream ();}
Excel table download feature