Import the Jxl.jar package with the following code:
PackageCom.gree;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;Importjava.io.UnsupportedEncodingException;Importjava.sql.PreparedStatement;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Locale;ImportJavax.servlet.ServletOutputStream;ImportJavax.servlet.http.Cookie;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpServletRequest;ImportJXL. Workbook;ImportJxl.format.Colour;ImportJxl.format.UnderlineStyle;ImportJxl.write.Label;ImportJxl.write.WritableCellFormat;ImportJxl.write.WritableFont;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook;Importjxl.write.WriteException;Importjxl.write.biff.RowsExceededException; Public classCreateexcel { Public voidCreatexcel (HttpServletResponse res,httpservletrequest req)throwsexception{outputstream os=NULL; Writableworkbook WWB=NULL; CONNDB DB=Newconndb (); String Str= "SELECT * from User"; ArrayList<Student> list=db.getlist (str); Try{Java.text.SimpleDateFormat DF=NewJava.text.SimpleDateFormat ("Yyyy-mm-dd"); String Today= Df.format (Newjava.util.Date ()); String FileName= "Change Time report-" +today; //System.out.println (fileName);Filename=NewString (Filename.getbytes ("gb2312"), "Iso8859_1"); //System.out.println (fileName);HttpServletResponse response=Res; HttpServletRequest Request=req; Response.reset (); Response.setcontenttype ("Application/octet-stream"); if(Request.getheader ("User-agent"). IndexOf ("MSIE 5.5")! =-1) {//MSResponse.setheader ("Content-disposition", "Filename=" +filename+ ". xls"); } Else{Response.AddHeader ("Content-disposition", "Attachment;filename=" +filename+ ". xls"); } /** /Os = Response.getoutputstream (); WWB = Workbook.createworkbook (OS); Writablesheet sheet = wwb.createsheet ("Sheet1", 0); Jxl.write.WritableCell cell = null; Writablefont wf = new Writablefont (Writablefont.arial,12,writablefont.no_bold,false, Underlinestyle.no_underlin E,colour.blue); Writablecellformat Formath = new Writablecellformat (WF); Formath.setalignment (Jxl.format.Alignment.CENTRE); Cell = new Label (0, 0, "User table", Formath); Sheet.addcell (cell); Sheet.mergecells (0,0,14,0); Formath = GetFormat ("BOLD", 9, Colour.black); Formath.setalignment (Jxl.format.Alignment.CENTRE); Formath.setborder (Jxl.format.border.all,jxl.format.borderlinestyle.thin); Writablecellformat format = GetFormat ("Nobold", 9, Colour.black); Format.setalignment (Jxl.format.Alignment.left); Format.setborder (Jxl.format.border.all,jxl.format.borderlinestyle.thin); Cell = new Label (0, 1, "First name", Formath); Sheet.addcell (cell); Cell = new Label (1, 1, "password", Formath); Sheet.addcell (cell); Cell = new Label (2, 1, "Last Login Time", Formath); Sheet.addcell (cell); /**/ for(intI=0;i<list.size (); i++) {cell=NewLabel (0, i+2, List.get (i). GetName (), format); Sheet.addcell (cell); Cell=NewLabel (1, i+2, List.get (i). GetPassword (), format); Sheet.addcell (cell); Cell=NewLabel (2, i+2, List.get (i). Getlastlogintime (), format); Sheet.addcell (cell); Sheet.setcolumnview (0,10); Sheet.setcolumnview (1,20); Sheet.setcolumnview (2,20); }wwb.write (); Wwb.close (); Os.close (); Response.setstatus (response. SC_OK); Response.flushbuffer (); } Catch(rowsexceededexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(WriteException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }} Writablecellformat GetFormat (String style,intsize, colour color) {Writablefont WFC=NULL; if("BOLD". Equals (Style)) WFC=NewWritablefont (writablefont.arial, size, Writablefont.bold,false, underlinestyle.no_underline, color); Else if("Nobold". Equals (Style)) WFC=NewWritablefont (writablefont.arial, size, Writablefont.no_bold,false, underlinestyle.no_underline, color); return NewWritablecellformat (WFC);} }
Index.jsp the code below, create the object and call the Createxcel method to export the Excel table
<% intopt; if(Request.getparameter ("opt")==NULL) opt=0; Elseopt=Integer. parseint (Request.getparameter ("opt")); if(opt==1) {createexcel Excel=NewCreateexcel (); Excel.createxcel (response, request); } Else{ %> <ahref= "Index.jsp?opt=1">Export Excel Table</a> <ahref= "importexcel.jsp">Import an Excel table</a> <%} } %>
Excel Export Jxl.jar Package