一般使用springmvc做网页的项目,都会有报表的需求,使用jxl或者iText导出excel或者pdf,让用户在浏览器下载到本地。这就需要让后台先在服务端的webapp/download文件下面,然后客户端的前端可以使用a标签,赋值href地址的方式,提示浏览器进行拦截下载,这样就可以完成。
注意点是后台生成的路径和传给前端的路径(获取客户端的IP地址,参考链接:http://blog.csdn.net/springmvc_springdata/article/details/43935113),具体代码如下(部分省略):
// 导出excel信息模板
@RequestMapping(value = "/outputexcelmodeal", method = RequestMethod.GET)
@ResponseBody
@SystemLog(apiPath = "/outputexcelmodeal")
@ApiOperation(value = "导出excel信息模板", httpMethod = "GET", notes = "导出excel信息模板")
public ResultSimple outputExcelModeal(HttpServletRequest request) {
JSONObject responseData = new JSONObject();
ResultSimple retireResult = new ResultSimple();
// Result<List<RetireInforItem>> retireResult = new
// Result<List<RetireInforItem>>();
List<RetireInforItem> listRetire = null;
try {
HttpSession session = request.getSession();
String token = (String) session.getAttribute(Constants.TOKEN);
// 发送服务端进行校验
// 从数据库获取所有的字段
responseData = retireInforService.getImportRetireInfoItems(token);
// JSONObject先转换成json字符串
String respnJSON = JSON.toJSONString(responseData);
Result<JSONArray> allResult = (Result<JSONArray>) JSON.parseObject(respnJSON,
new TypeReference<Result<JSONArray>>() {
});
// 将json转换成list数组
listRetire = JSONObject.parseArray(JSON.toJSONString(allResult.getContent()), RetireInforItem.class);
String fileName = "retireinfor.xls";
// 在本地服务器生成一个文件,路径是本地的实际地址,然后将路径给前端是一个ip地址的访问
String filePath = System.getProperty("RMS.root") + File.separator + "download" + File.separator + fileName;
// D:\tomcat7.0\apache-tomcat-7.0.72-windows-x64\apache-tomcat-7.0.72\webapps\RMSClient\\download\retireinfor.xls
System.out.println("服务器excel文件生成的地址为服务端的download文件夹下面:" + filePath + "==============");
// 前段通过ip地址可以访问到的地址
String returnFileExcelPath = request.getScheme() + "://" + getClientIp.getIpAddr(request) + ":"
+ request.getServerPort() + request.getContextPath() + "/" + "download/retireinfor.xls";
// http://192.168.40.55:8080/RMSClient/download/retireinfor.xls
System.out.println("前端需要使用ip地址的方式访问我存在服务端download文件夹下的地址为" + returnFileExcelPath);
try {
// 调用工具类,从数据库读取excel标题的字段,然后导出一个excel模板
ExcelOutputUtil.retireInforExcelModel("导出信息excel模板", filePath, listRetire);
} catch (Exception e) {
e.printStackTrace();
LogUtil.error(e);
retireResult.setCode(2000);
retireResult.setMessage("导出异常,请检查日志,返回2000");
return retireResult;
}
retireResult.setContent(returnFileExcelPath);
retireResult.setCode(allResult.getCode());
retireResult.setMessage("下载信息模板成功!!!");
} catch (Exception e) {
LogUtil.error(e);
}
return retireResult;
}
还需要在web.xml里面设置获取根路径如下:
<!-- webRoot根路径key值配置 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>RMS.root</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
服务端生成的文件路径地址:D:\tomcat7.0\apache-tomcat-7.0.72-windows-x64\apache-tomcat-7.0.72\webapps\RMSClient\\download\retireinfor.xls
传给前端直接访问的href地址为:http://192.168.40.55:8080/RMSClient/download/retireinfor.xls
注:服务端需要先建立一个download文件夹,如下截图,前端的a标签可以参考网上其他的大神。
: