Ajax + servlet + POI export Excel

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.