ajax + servlet + poi 导出Excel

前端之家收集整理的这篇文章主要介绍了ajax + servlet + poi 导出Excel前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

js代码

$(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');
			}
		})*/
	})
});

后端servlet代码
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 = "账单信息";
		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();
	}
第一次,采用ajax的方式,发送请求并传递相应的参数时,后台获取了的数据并且利用POI接口类将数据填充到Excel,但是前台客户端并没有出现下载提示的窗口,导出Excel失败。

第二次,采用提交表单的方式,直接请求,导出成功。

经过百度,才知道

ajax返回的类型只有xml、text、json、HTML等类型,没有流类型,因此不会弹出下载提示框。

原文链接:https://www.f2er.com/ajax/161037.html

猜你在找的Ajax相关文章