//open这种方式有个弊端,就是转的参数值不能太大,所以只能用jquery创建表单form在提交(ajax异步请求不会触发浏览器下载文件的功能)
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器
window.location.href(url);
}else{
window.open(url,"xxx");
}
方法2 js前端
//导出
$("#exportData").click(function() {
var myqx =document.getElementById("qxId").contentWindow.document.getElementById("myqx");
var myqxD = $(myqx).val();
var id = $("input[name='myHidId']").val();
var url = __ctx+'/hyddrbnew/hyddrbnew/hyddrb/exportExcelDdrb.ht';
var form = $("<form>");
form.attr("style","display:none");
form.attr("target","_blank");
form.attr("method","post");
form.attr("action",url);
var input1 = $("<input>");
input1.attr("type","hidden");
input1.attr("name","id");
input1.attr("value",id);
var input2 = $("<input>");
input2.attr("type","hidden");
input2.attr("name","myqxD");
input2.attr("value",myqxD);
$("body").append(form);
form.append(input1);
form.append(input2);
form.submit();
form.remove();
});
方法2(java后端)
@RequestMapping({ "exportExcelDdrb" })
@Action(description = "导出excel")
public void exportExcelDdrb(HttpServletRequest request, HttpServletResponse response) throws Exception {
String id = RequestUtil.getString(request, "id");
String imageMsg = RequestUtil.getString(request, "myqxD");
//取得实体类数据
Hyddrb yjbb = new Hyddrb();
yjbb = (Hyddrb) hyddrbService.getById(Long.valueOf(Long.parseLong(id)));
String dirPath = FileUtil.getRootPath() + File.separator + "commons" + File.separator + "template" + File.separator + "hydlbbTemplate" + File.separator;
String fileName = "expHyTabDdrb.xls";
// String fileName = "expHyTabDdrb.xlsx";
FileInputStream inStream = new FileInputStream(new File(dirPath + fileName));
//xls2003版
HSSFWorkbook wb = new HSSFWorkbook(inStream);
this.drawImage(wb, imageMsg);
this.initSheetCell(wb, yjbb);
//xlsx2007版
// XSSFWorkbook wbxlsx = new XSSFWorkbook(inStream);
// this.drawImage_xlsx(wbxlsx, imageMsg);
// this.initSheetCell_xlsx(wbxlsx, yjbb);
fileName = "调度日报_" + new SimpleDateFormat("yyyy-MM-dd").format(yjbb.getRq()) ;
//其实两套代码都可以,最终结果以下面给定的后缀作为生成excel的后缀,即03版还是07版
fileName += ".xls";
// fileName += ".xlsx";
response.setContentType("application/excel");
response.addHeader("Content-Disposition", "attachment;filename=" + new String((fileName).getBytes("gb2312"), "ISO8859-1"));
OutputStream out = response.getOutputStream();
wb.write(out);
// wbxlsx.write(out);
out.flush();
out.close();
}