Download excel in JSP
The open () function of JavaScript is used here, and the background uses JXL to dynamically generate an Excel file stream.
Front Desk: Window.opne ("downexcelaction");
Background:
public void Downexcelaction () throws Rowsexceededexception, writeexception{System.out.println ("Export Excel table");
String fileName = new SimpleDateFormat ("YYYYMMDDHHMMSS"). Format (new Date ()) + ". xls";
String result = "Success";
Query out data//populate the OutputStream OS in Excel;
Writableworkbook Wbook;
try {os = Response.getoutputstream ();
Response.reset ()//Empty output stream Response.setheader ("Content-disposition", "Attachment;filename=" + New String (Filename.getbytes ("GB2312"), "iso8859_1");/Set Output file header, <span style= "Font-size:18px;color: #FF6666;" > Here you can resolve the filename Chinese garbled problem </span> response.setcontenttype ("application/vnd.ms-excel");//define Output type WBO
OK = workbook.createworkbook (OS)//build Excel file Writablesheet Wsheet = Wbook.createsheet ("Membership card Information", 0);//Create Sheet Define cell styles Writablefont WF = new Writablefont (writablefont.arial, writAblefont.bold, False, Underlinestyle.no_underline, Jxl.format.Colour.BLUE);
Writablecellformat WCF = new Writablecellformat (WF);
Set alignment wcf.setalignment (Jxl.format.Alignment.CENTRE);
Freeze the first row of Wsheet.getsettings (). Setverticalfreeze (1);
Wsheet.addcell (New Label (0, 0, "Inner Code", WCF));
Wsheet.addcell (New Label (1, 0, "membership card", WCF));
Wsheet.addcell (New Label (2, 0, "Agent", WCF));
Wsheet.addcell (New Label (3, 0, "membership card Information", WCF));
Wsheet.addcell (New Label (4, 0, "Membership card status", WCF));
Wsheet.addcell (New Label (5, 0, "consumer info", WCF));
Wsheet.addcell (New Label (6, 0, "Current balance", WCF));
Wsheet.addcell (New Label (7, 0, "time", WCF));
Return to Excel Wbook.write ();
Wbook.close ();
Os.close (); The catch (Exception e) {result = "Download has an exception, please contact the administrator."
"; E.Printstacktrace (); }
}