1. Preface struts2 for writing Excel data support Well, say not much, on the code
2.jsp page:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><%@ taglib prefix=" s "uri="/struts-tags "%><% String Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
3.action Code:Package Com.dwg.action;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.inputstream;import Java.util.arraylist;import Java.util.calendar;import Java.util.List;import Com.dwg.bean.student;import Org.apache.poi.xssf.usermodel.xssfcell;import Org.apache.poi.xssf.usermodel.xssfcellstyle;import Org.apache.poi.xssf.usermodel.xssfrow;import Org.apache.poi.xssf.usermodel.xssfsheet;import Org.apache.poi.xssf.usermodel.xssfworkbook;import Com.opensymphony.xwork2.actionsupport;import Com.dwg.service.treeservice;public class ExportExcelAction extends actionsupport{/** * */private static final long serialversionuid = 1l;private Treeservice treeservice;public TreeService Gettreeservice () {return treeservice;} public void Settreeservice (Treeservice treeservice) {treeservice = Treeservice;} Public list<?> list;public list<?> getList () {return List;} public void setlist (list<?> list) {this.list = list;} Private List<student> Slist;////puBlic list<student> getslist () {//return slist;//}//public void setslist (list<student> slist) {//this.slist = Slist;//}private InputStream excelstream; Public InputStream Getexcelstream () {return excelstream;} public void Setexcelstream (InputStream excelstream) {this.excelstream = Excelstream;} private string Filename;public string GetFileName () {return fileName;} public void Setfilename (String fileName) {this.filename = FileName;} Private list<student> slist = new arraylist<student> ();p ublic string Execute () throws exception{string sq L = "SELECT * from student"; List = this. Treeservice.gettreelist (SQL); Slist = DWG (list); for (int i=0; i<list.size (); i++) {Student Student =new Student (); Object[] objects = (object[]) list.get (i); Student.setid ((Integer) objects[0]). Intvalue ()); Student.setname (Objects[1].tostring ()); STUDENT.SETCLASS_ID ((Integer) objects[2]). Intvalue ());//Student.setid (i);//Student.setname ("Personnel" +i);// STUDENT.SETCLASS_ID (i); SYSTEM.OUT.PRINTLN ("Student Value:" +student.getid () +student.getname () +student.getclass_id ()); Student Student = (Student) list.get (i); Slist.add (student); SYSTEM.OUT.PRINTLN ("----nmd5----"); } Xssfworkbook workbook = This.getworkbook (slist); if (workbook! = null) {Calendar c = calendar.getinstance (); int year = C.get (calendar.year); int month = C.get (calendar.month) + 1; String month_ = new String ("" + month); if (Month <) {Month_ = "0" + month; } int day = C.get (calendar.day_of_month); String Day_ = new String ("" + day); if (Day < ten) {Day_ = "0" + day; } this.workbook2inputstream (Workbook, Year + "-" + Month_ + "-" + Day_ + "); } return SUCCESS;} public void Workbook2inputstream (Xssfworkbook workbook, String fileName) throws Exception {this.filename = filename;//Set file name Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Workbook.write (BAOs); Baos.flush (); byte[] AA = Baos.tobytearray (); Excelstream = new Bytearrayinputstream (AA, 0, aa.length); Baos.close (); } private Xssfworkbook Getworkbook (list<student> List) throws Exception {Xssfworkbook workbook = new XSSF Workbook (); Create a worksheet Xssfcellstyle style = Workbook.createcellstyle (); Style.setalignment (Xssfcellstyle.vertical_center); Style.setalignment (Xssfcellstyle.align_center); Xssfsheet sheet = workbook.createsheet ("Sheet1"); Create form Xssfrow row = sheet.createrow (0); Create first line title Xssfcell cell = null; for (int i = 0; i < 3; i++) {cell = Row.createcell (i); Cell.setcellvalue (i); Cell.setcellstyle (style); }//Createxportdata for(int i = 0; i < list.size (); i++) {row = Sheet.createrow (i + 1);//cell = Row.createcell (0); Cell.setcellvalue (List.get (i). GetId ()); Cell = Row.createcell (1); Cell.setcellvalue (List.get (i). GetName ()); Cell = Row.createcell (2); Cell.setcellvalue (List.get (i). getclass_id ()); Cell = Row.createcell (3); Cell.setcellvalue (List.get (i). getaddress_id ()); } return workbook; } }
4.struts.xml configuration: <action name= "Exportexcel" class= "exportexcelaction" > <result name= "Success" type= "Stream" > <param name= "ContentType" >application/vnd.ms-excel</param> <param name= "InputName" > excelstream</param> <param name= "contentdisposition" >attachment;filename= "${fileName}.xlsx" < /param> <param name= "buffersize" >1024</param> </result> </action>
5. Operating effect:
Java SHH Framework Implementation database query out of list import Excel and download