这里我主要讲EasyUI内,通过button出发onClick()事件,把日期传入后台,由SpringMVC处理,导出excel
要求:根据分拣开始时间和完成分拣时间,导出对应的数据
一:easyui添加的button
toolbar :{
text : '导出分拣信息',
iconCls : 'icon-print',
handler : function() {
exportSortingOrder();
}
}
二:导出时间不能超过5天,因为数据量过大,以下是js逻辑
function exportSortingOrder(){
var startDate = $("input[name=startDate]").val();
var endDate = $("input[name=endDate]").val();
var iDays = getDays(startDate,endDate);
if(iDays>5){
alert("不能导出超过5天的数据!");
return false;
}else if(iDays > 0 && iDays <=5){
window.location.href=ctx+".../...?startDate="+startDate+"&endDate="+endDate;
}
}
function getDays(sd,ed){
if((sd == "" || sd == null) || (ed =="" || ed == null)){
alert("请选择开始分拣时间或结束分拣时间!");
}else{
var year = parseInt(sd.substring(0,4));
var sMonth = parseInt(sd.substring(5,7));
var eMonth = parseInt(ed.substring(5,7));
var strDateS = new Date(sd.substring(0,4),sd.substring(5,7),sd.substring(8,10),sd.substring(11,13),sd.substring(14,16),sd.substring(17,19));
var endDateE = new Date(ed.substring(0,4),ed.substring(5,7),ed.substring(8,10),ed.substring(11,13),ed.substring(14,16),ed.substring(17,19));
if(strDateS > endDateE){
alert("结束时间不能在开始时间之前!");
}else{
var iDays = parseInt((endDateE - strDateS ) / 1000 / 60 / 60 /24);
if(sMonth == eMonth){
return iDays;
}else{
if(sMonth==1 || sMonth==3 ||sMonth==5||sMonth==7||sMonth==8||sMonth==10||sMonth==12){
iDays += 1;
}else if(sMonth==2){
if((year%4==0 && year%100!=0)||(year%100==0 && year%400==0)){
iDays -= 2;
}else{
iDays -= 3;
}
}
}
return iDays;
}
}
}
三:后台处理
package com.genye.sorter.warehouse.action;
@RequestMapping(value = "/exportSortingOrder",method = RequestMethod.GET)
public void exportSortingOrder(HttpServletRequest request,HttpServletResponse response) throws IOException{
String parttern = DateUtil.DATETIME_FORMAT;
Date startDate = DateUtil.StringToDate(request.getParameter("startDate"), parttern);
Date endDate = DateUtil.StringToDate(request.getParameter("endDate"), parttern);
XSSFWorkbook wb = sortingOrderService.exportSortingOrder(startDate,endDate);
String[] sds = request.getParameter("startDate").split("\\s");
String[] eds = request.getParameter("endDate").split("\\s");
StringBuffer fileName=new StringBuffer("attachment;filename=");
fileName.append(sds[0]).append("_").append(eds[0]).append("_").append(new Date().getTime()).append(".xlsx");
response.setContentType("application/ms-excel");
response.setHeader("Content-disposition",fileName.toString());
OutputStream outputStream = response.getOutputStream();
wb.write(outputStream);
outputStream.flush();
outputStream.close();
}
技术点:1.项目开发过程中,考虑到数据量很大,需要使用2007版的excel
2.在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。
类型格式:type/subtype(;parameter)? type
主类型,任意的字符串,如text,如果是*号代表所有;
subtype 子类型,任意的字符串,如html,如果是*号代表所有;
parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。
3.已知MIME 类型的文件(比如:*.gif;*.txt;*.htm)能够在访问时弹出“文件下载”对话框,希望文件直接在浏览器上显示而不是弹出文件下载对话框
Response.AddHeader ("Content-disposition",fileName.toString())
4.注意:设置文件名的时候,出现中文或者空格,转换失败
解决办法:
response.setHeader( “Content-Disposition”, “attachment;filename=” + new String( fileName.getBytes(”gb2312″), “ISO8859-1″ ) );
5.导出excel
public XSSFWorkbook exportSortingOrder(Date startDate, Date endDate) {
String[] excelHeader = { "分拣单号", "分配订单数", "订单总数", "订单完成数", "商品数", "分拣数", "创建时间", "分拣状态", "开始分拣时间", "最后分拣时间",
"完成分拣时间" };
List<SortingOrderVo> lists = sortingOrderMapperCustom.listsortingOrderByDate(startDate, endDate);
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet1 = wb.createSheet("商品记录");
// 设置style
XSSFCellStyle headStyle = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 23);
headStyle.setFont(font);
headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
for (int i = 0; i < excelHeader.length; i++) {
sheet1.setDefaultColumnStyle(i, style);
}
XSSFRow row0 = sheet1.createRow(0);
for (int i = 0; i < excelHeader.length; i++) {
XSSFCell cell = row0.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(style);
sheet1.setColumnWidth(i, 5000);
}
XSSFRow row;
XSSFCell cell;
SortingOrderVo sortingOrderVo;
XSSFDataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy-mm-dd hh:mm:ss"));
for (int i = 0; i < lists.size(); i++) {
row = sheet1.createRow(i + 1);
sortingOrderVo = lists.get(i);
int state = sortingOrderVo.getState();
row.createCell(0).setCellValue(sortingOrderVo.getSortingOrderNo());
row.createCell(1).setCellValue(sortingOrderVo.getOrderCount());
row.createCell(2).setCellValue(sortingOrderVo.getAllotCount());
row.createCell(3).setCellValue(sortingOrderVo.getFinishCount());
row.createCell(4).setCellValue(sortingOrderVo.getItemAmount());
row.createCell(5).setCellValue(sortingOrderVo.getItemAmountFinish());
cell = row.createCell(6);
cell.setCellValue(sortingOrderVo.getCreateDate());
cell.setCellStyle(style);
switch (state) {
case 0:
row.createCell(7).setCellValue("未开始分拣");
break;
case 1:
row.createCell(7).setCellValue("分拣中");
break;
case 2:
row.createCell(7).setCellValue("完成分拣");
break;
default:
row.createCell(7).setCellValue("");
break;
}
cell = row.createCell(8);
cell.setCellValue(sortingOrderVo.getStartSortingDate());
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue(sortingOrderVo.getLastSortingDate());
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue(sortingOrderVo.getFinishDate());
cell.setCellStyle(style);
}
return wb;
}
问题:导出excel过程中,对于date类型数据,变成了4324.32,需要转换date
解决办法:
XSSFCellStyle style = wb.createCellStyle();
XSSFDataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy-mm-dd hh:mm:ss"));
cell.setCellValue(sortingOrderVo.getCreateDate());
cell.setCellStyle(style);
三:实现效果