excel导出出现弹框
@RequestMapping(value = "exportExcel")
public void exportExcel(@Valid Date startPayDate, @Valid Date endPayDate,
HttpServletResponse response) {
String startDate = null;
String endDate = null;
String context = "";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
if(startPayDate == null && endPayDate != null){
endDate = sf.format(endPayDate);
context = "(到"+endDate+"为止)";
}else if(startPayDate != null && endPayDate == null){
startDate = sf.format(startPayDate);
context = "("+startDate+"至今)";
}else if(startPayDate != null && endPayDate != null){
startDate = sf.format(startPayDate);
endDate = sf.format(endPayDate);
context = "("+startDate+"至"+endDate+")";
}
//中文会出现乱码,将其换成中文格式输出
String zhStr = "商品订单详情";
String filename = null;
try {
/* 根据request的locale 得出可能的编码,中文操作系统通常是gb2312 */
filename = new String(zhStr.getBytes("GB2312"), "ISO_8859_1");
context = new String(context.getBytes("GB2312"), "ISO_8859_1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
zhStr = filename+context;
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition",
"attachment;filename="+zhStr+".xlsx");
OutputStream out = null;
try {
out = new BufferedOutputStream(response.getOutputStream());
ExportExcel excel = shopUserOrderService.exportExcel(startPayDate,
endPayDate);
excel.write(out);
} catch (Exception e) {
throw new RuntimeException("系统异常");
} finally {
try {
out.flush();
out.close();
} catch (Exception e) {
throw new RuntimeException("导出数据有误");
}
}
}