JS Code
$ (document). Ready (function () {
$ ("#btn_export"). Click (function () {
var area = $ ("#s-area option:selected"). Text ();
/ar B_s_num = $ ("#b_s_num option:selected"). Text ();
var bill_state = $ ("#bill_state option:selected"). Text ();
var remarks = $ ("#remarks option:selected"). Text ();
Window.location.href= "Exportconfirmbill";
/*$.ajax ({
URL: ' Exportconfirmbill ',
type: ' Post ',
data:{' area ': Area, ' bill_state ': bill_state, ' Remarks ': Remarks},
success:function (data) {
window.open (' Exportconfirmbill ');
}
}) */
})
});
Back-end servlet code
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//
TODO auto-generated Method Stub response.setcontenttype ("Octets/stream");
list<string> columns = new arraylist<string> ();
list<object> confirm_bill_lst = new arraylist<object> ();
String Excelname = "Billing Information"; Response.AddHeader ("Content-disposition", "attachment;filename=" + New String (Excelname.getbytes ("gb2312"), "
Iso8859-1 ") +". xls ");
Servletoutputstream out = Response.getoutputstream ();
Hssfworkbook wb = new Hssfworkbook ();
Hssfsheet sheet = Wb.createsheet ();
Hssfrow row1 = sheet.createrow (0);
Billmanage BM = new Billmanageimpl ();
columns = Bm.getcolumns ();
for (int i=0;i<columns.size () -1;i++) {Hssfcell cell =row1.createcell (i);
Cell.setcellvalue (Columns.get (i+1));
String area = request.getparameter (' area ');
String bill_state = Request.getparameter ("Bill_state"); String remarks = Request.getparameteR ("Remarks");
System.out.println (bill_state);
Confirm_bill_lst = Bm.exportconfirmbill (area, bill_state, remarks); for (int i=0;i<confirm_bill_lst.size (); i++) {list<object> lst = (list<object>) confirm_bill_lst.get (i)
;
Hssfrow row = Sheet.createrow (i+1);
for (int j=0;j<lst.size (); j + +) {cell cell = Row.createcell (j);
if (Lst.get (j) ==null) {Cell.setcellvalue ("");
}else{Cell.setcellvalue (String.valueof (Lst.get (j)));
}} wb.write (out);
Out.close ();
Wb.close ();
}
The first time, in the Ajax way, send the request and pass the appropriate parameters, the background to obtain the data and use the POI interface class to fill the data to Excel, but the foreground client does not appear the window of the download prompts, export Excel failed.
The second time, the way to submit a form, direct request, export success.
After Baidu, just know
The type that Ajax returns is only XML, text, JSON, HTML, and no stream type, so the download prompt box does not pop up.