重要的事情说三遍!!!!
jQuery的ajax函数、及ajaxSubmit等函数的返回类型(dataType)只有xml、text、json、html等类型,没有“流”类型.
jQuery的ajax函数、及ajaxSubmit等函数的返回类型(dataType)只有xml、text、json、html等类型,没有“流”类型.
jQuery的ajax函数、及ajaxSubmit等函数的返回类型(dataType)只有xml、text、json、html等类型,没有“流”类型.
所以想要弹框下载,就不能用Ajax来做!!!!
JSP:
<span class="btn" onclick="excel_export()">EXCEL导出</span>
<div style="display: none;">
<form action= "login/excel_download.action" method="get">
<input type="text" id="startime" name="startime">
<input type="text" id="stoptime" name="stoptime">
<input type="text" id="mintemp" name="mintemp">
<input type="text" id="maxtemp" name="maxtemp">
<input type="text" id="winds" name="winds">
<input type="text" id="id" name="id">
<input type="submit" id="sub" value="Submit" />
</form>
</div>
JS
function excel_export(){
$("#startime").val(begaintime);
$("#stoptime").val(endtime);
$("#mintemp").val(wind_sp);
$("#maxtemp").val(min_temp);
$("#winds").val(max_temp);
$("#id").val(a1);
$("#sub").click();
}
后台:
@RequestMapping(value = "excel_download")
@ResponseBody
public void excel_download(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.setHeader("Cache-Control", "no-cache");
String fileName = "excel文件";
String startime = req.getParameter("startime");
String stoptime = req.getParameter("stoptime");
String mintemp = req.getParameter("mintemp");
String maxtemp = req.getParameter("maxtemp");
String winds = req.getParameter("winds");
String id = req.getParameter("id");
System.out.println(startime+","+stoptime+","+mintemp+","+maxtemp+"<"+winds+","+id);
Map<String, Object> param=new HashMap<String, Object>();
param.put("id", id.substring(1, id.length()));
param.put("startime", startime);
param.put("stoptime", stoptime);
param.put("mintemp", mintemp);
param.put("maxtemp", maxtemp);
param.put("winds", winds);
List<RecordInfo> r = recordService.GetRecordByCondition_excel(param);
System.out.println(r);
// PoiExpExcel.main(r);
String[] title = {"序号","风速","记录时间","温度"};
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个工作表sheet
HSSFSheet sheet = workbook.createSheet();
//创建第一行
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
//插入第一行数据title
for(int i= 0; i < title.length; i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
//追加数据
for(int i= 1; i <= r.size(); i++){
HSSFRow nextrow = sheet.createRow(i);
HSSFCell cell2 = nextrow.createCell(0);
cell2.setCellValue(i);
cell2 = nextrow.createCell(1);
cell2.setCellValue(r.get(i-1).getRecord_wind_speed());
cell2 = nextrow.createCell(2);
cell2.setCellValue(r.get(i-1).getRecord_time());
cell2 = nextrow.createCell(3);
cell2.setCellValue(r.get(i-1).getRecord_temp());
}
//创建一个文件
/*File file = new File("C:/Users/Administrator/Desktop/poi_test.xls");
try {
file.createNewFile();
//将EXCEL内容存盘
FileOutputStream stream = FileUtils.openOutputStream(file);
workbook.write(stream);
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
resp.reset();
resp.setContentType("application/vnd.ms-excel;charset=utf-8");
resp.setHeader("Content-Disposition", "attachment; filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}