在实习的这几天中学到很多东西,每天都有新的任务需要我去完成,当然,我也不是全会,是在小老师的帮助下完成的。一般是她给我写一个例子,我再完成余下的几个类似功能的页面。现在就总结一下我今天学到的知识,如何在页面点击“导出”就可以将页面中dategrid表格中的数据导入到excel里面。
(我接下来所写的代码是后台客户端的代码,调用公司框架中写好的部分方法)
首先,在jsp页面上写上点击“导出”按钮调用的方法:
<a class="easyui-linkbutton pd10" style="height:28px;" onclick="exportData()">导出</a>
然后,javaScript中写入此方法:
function exportData(){
var startTime =$("#startTime").datebox("getValue");
var endTime =$("#endTime").datebox("getValue");
var areaId = $("#areaId").val();
window.open("/manage/exportFunctionalData?startTime=" + startTime + "&endTime=" + endTime + "&areaId=" + areaId);
}
最后,在跳转的页面写上导出excel的方法:public void exportFunctionalData(HttpServletRequest request, HttpServletResponse response,
String areaId, String startTime, String endTime)
throws RowsExceededException, WriteException, IOException {
// 导出的数据,这个是我自己另外写的方法
List<Map> result = manageService.getDataByFunctional(areaId,startTime,endTime);
//表名
String fileName = "职能部门工作统计";
//表的列名
String header = "<caption>" + fileName + "</caption><thead><tr><th width='200'>部门名称</th><th width='200'>总数</th><th width='200'>处置中</th><th width='200'>结案数</th><th width='200'>超期数</th></tr></thead><tbody>";
//表中每一行的数据
String content = "";for (Map map : result) {content += "<tr><td>" + map.get("ST_DEAL_UNIT") + "</td>" +"<td>" + map.get("MDATA") + "</td>" +"<td>" + map.get("CZ") + "</td>" +"<td>" + map.get("JA") + "</td>" +"<td>" + (map.get("CQDATA")==null?"0":map.get("CQDATA")) + "</td></tr>";}exportService.export(header + content, response);// 导出System.out.print("导出excel:" + fileName);}
这样最做完表就可以导出了,但是在导出的过程中遇到很多的问题,如下问题1:获得的数据为null时,解决方法是:
(map.get("CQDATA")==null?"0":map.get("CQDATA"))
问题2:将获得的数据进行计算,然后取两位小数:
String.format("%.2f", 要保留小数的值);
问题3:从oracle获得的数据,进行计算时,需要做一下的做法:
int mdata = Integer.parseInt(map.get("MDATA")).toString());
注意是:Integer.parseInt(map.get("列名")).toString();
今天的总结就到这里,感觉最近有点乱,而且思路不太清晰,需要继续锻炼编程的思维。加油,实习生!