实际事理是利用ajax返回一个须要下载的excel的文件名而已,在ssm框架的service层,帮我们把须要的excel通过流的办法下载到了某个文件夹下,然后ajax是实行成功后,实行一条
window.location.href=
该href指向这个刚刚已经下载好的excel表格,然后就会产生一个下载窗口,让开发者乍以为像ajax实现了下载功能一样。

一开始,我就被它误导了,走了好几天的弯路,现在终于理清思路了,好了,废话少说,直接上代码:
我的项目构造如下:
前端紧张在myForm.html页面进行操作,其核心代码如下:
本次紧张是按照用户选择的韶光段,从s123到e123之间的数据,导出到excel表格里面。window.location.href起到的浸染便是指向该资源,产生下载框。pathFile便是新天生的excel表格的名字,一样平常以韶光日期进行命名,详细见下方service层代码
springmvc层核心拦截功能代码写法如下:
download文件夹下面的是不要被拦截的,其余两个都是放的诸如js,css等静态资源。
web.xml配置文件的核心部分:
我设置的是所有要求都拦截
下面的后真个关键代码:
controller层核心代码:
s123,e123为起始和终止韶光,当用户没有选择的时候,这两个为null,req和resp后面要求路径的时候会用到
service层核心代码:
//导出为excel表格@Override@Transactional(propagation = Propagation.REQUIRES_NEW,isolation= Isolation.DEFAULT,rollbackFor=Exception.class)public ResData exportData(String s123, String e123,HttpServletRequest req,HttpServletResponse resp) { System.out.println(\公众进入meterService层的exportData方法\"大众); List<Meter> meterList; try { Para para=new Para(); para.setStartTime(s123); para.setEndTime(e123); int num=meterMapper.queryCount(para);//查询记录总数 para.setLimit(num); meterList = meterMapper.queryDataTime(para);//数据源 //利用poi下载文件 HSSFWorkbook workbook = new HSSFWorkbook(); //创建sheet HSSFSheet sheet = workbook.createSheet(\"大众燃气表数据\"大众); //创建row信息 HSSFRow row = sheet.createRow(0);//第一行 //创建单元格头标,标题行数据 row.createCell(0).setCellValue(\"大众序号\"大众); row.createCell(1).setCellValue(\"大众运用ID\"大众); row.createCell(2).setCellValue(\公众做事ID\"大众); row.createCell(3).setCellValue(\"大众设备ID\"大众); row.createCell(4).setCellValue(\"大众网关ID\"大众); row.createCell(5).setCellValue(\"大众数据\公众); row.createCell(5).setCellValue(\"大众韶光日期\"大众); //获取数据,从第二行开始以此往excel表格里面赋值 if (meterList != null && meterList.size()!=0) { for(int i=0;i<meterList.size();i++){ row = sheet.createRow(i+1);//从第二行开始 Meter e=meterList.get(i); row.createCell(0).setCellValue(e.getId()); row.createCell(1).setCellValue(e.getAppId()); row.createCell(2).setCellValue(e.getServiceId()); row.createCell(3).setCellValue(e.getDeviceId()); row.createCell(4).setCellValue(e.getGatewayId()); row.createCell(5).setCellValue(e.getStatus()); row.createCell(6).setCellValue(e.getTimestamp()); } } //将其天生一个excel文件,输出,担保每次天生的文件都不一样,则利用韶光日期命名 SimpleDateFormat sFormat=new SimpleDateFormat(\"大众yyyy-MM-dd HHmmss\"大众); Calendar calendar=Calendar.getInstance(); String excelName=sFormat.format(calendar.getTime()); /以下操作将获取支配的做事端地址,以http的格式显示 projectServerPath=http://localhost:8080/meter/statics/download/ / //String projectServerPath = req.getScheme() + \公众://\公众+req.getServerName()+\公众:\"大众 +req.getServerPort() + req.getContextPath() + \公众/statics/download/\公众; / path为/statics/download/在做事真个地址,将输呈现实哪个盘下的哪个文件夹 / String path = req.getSession().getServletContext().getRealPath(\"大众/statics/download/\公众); //String fileURL=projectServerPath+excelName+\"大众.xls\公众; String fileURL=path+\"大众\\\"大众+excelName+\"大众.xls\"大众; //fileURL=new String(fileURL.getByte(\公众iso8859-1\"大众),\"大众UTF-8\"大众); // String fileURL1=new String(fileURL.getBytes(),\公众ISO8859-1\"大众); //新建一个文件输出流 System.out.println(\"大众fileURL的值:\"大众+fileURL+\公众\"大众); System.out.println(\公众excelName的值:\"大众+excelName+\公众\公众); System.out.println(\公众path的值:\"大众+path+\公众\"大众); FileOutputStream outputStream = new FileOutputStream(fileURL); outputStream.flush(); //写入天生的excel表格到做事端/statics/download/下 workbook.write(outputStream); outputStream.close(); ResData resData =new ResData(); resData.setCode(\"大众200\"大众); resData.setTitle(\"大众操作信息\"大众); resData.setPages(\"大众0\"大众); resData.setData(excelName+\公众.xls\公众); resData.setMsg(\"大众导出了一个excel表格到/statics/download文件夹下\"大众); return resData; }catch(Exception ex){ System.out.println(\"大众返回到service层后catch后\"大众); ex.printStackTrace(); ResData resData =new ResData(); resData.setCode(\"大众400\"大众); resData.setTitle(\公众操作信息\"大众); resData.setPages(\"大众0\公众); resData.setData(\"大众errorURL\公众); resData.setMsg(ex.toString()); return resData; }}
须要把稳的几点:
一定要把稳路径的问题!
!
!
我在这里也走了几天的弯路,一个是fileoutstream流对应输出的地址,采取以下办法,可以灵巧的处理路径的问题。末了便是运行后的页面效果见下面
String path = req.getSession().getServletContext().getRealPath (\"大众/statics/download/\公众);