最近在做一个报表导出,根据时间导出相应的表格数据;接下来我就来总结一下:
1、大概就是长这个样子,点击按月统计,会弹出一个时间 日期,查询对应的年月,显示对应 的数据,
2、具体的实现方法如下:
js中的代码
$("#btn_export").click(function(){
var getSelectRows = $("#order-completion").bootstrapTable('getSelections', function (row) {
return row;
});
if(getSelectRows.length<=0){
ToastrOptions();
toastr.warning('请勾选需要导出的订单');
}else{
var orderItemid="";
for (var i = 0; i < getSelectRows.length; i++){
//获取选中的订单编号
orderItemid = orderItemid+getSelectRows[i].returnOrderItemId+",";
}
//获取xxx编号
var supplierId=getSelectRows[0].itemSupplieid;
var state=getSelectRows[0].itemState;
//对应的后台导出路径 以及传递的参数
location.href = "/exportOrderExcellReturn?supplierId="+supplierId+"&orderItemid="+orderItemid
}
})
3、controller层代码:
设置表头以及表格名称:
private static String[] columnNames= {"序号","订单编号","商品名称","品牌","规格","单价","数量","总价","下单时间","收货人","联系电话","收货地址"};
private String allExcelFileName = "订单信息";
2、具体导出方法的实现
@ResponseBody
@RequestMapping("/exportOrderExcellReturn")
public String findorderforexcelRrturn(Integer state,String supplierId,String orderItemid,HttpServletResponse response) throws Exception {
List itemids= new ArrayList();
//截取传递过来的编号,并且以逗号进行分割
String ids[]=orderItemid.split(",");
for (String string : ids) {
itemids.add(Integer.parseInt(string));
}
//自定义的实体类,里面有你所需要的属性
OrederTJPojo1 orderTJPojo=new OrederTJPojo1();
orderTJPojo.setSupplierId(supplierId);//供应商id
orderTJPojo.setItemState(state);;//产品状态
orderTJPojo.setList(itemids);//商品集合
//根据条件查询的方法
List orderList = orderServices.findAllReturnOrderTime(orderTJPojo);
int v=1;
HSSFWorkbook wb = new HSSFWorkbook();
response.setContentType("application/x-download");//下面三行是关键代码,处理乱码问题
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+new String(allExcelFileNameT.getBytes("gbk"), "iso8859-1")+".xls");
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, allExcelFileNameT);
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < columnNamesT.length; i++) {
HSSFCell cell = row.createCell((short) i);
cell.setCellValue(new HSSFRichTextString(columnNamesT[i]));
}
int xh=0;
for (ReturnTableBySelf com : orderList) {
xh++;
String xuhao = String.valueOf(xh);
String[] columnAsset = {xuhao,
com.getReturnOrderserialnumber(),//退货订单号
com.getProduct().getProductName(),//商品名
com.getProduct().getBrandName(),//品牌
com.getProduct().getProductSpec(),//规格
com.getProduct().getProductPrice().toString(),//单价
com.getReturnOrdernumber().toString(),//数量
com.getProduct().getProductPrice().multiply(new BigDecimal(com.getReturnOrdernumber())).toString(),//计算总价
sdf.format(com.getOrderTable().getOrderTime()),
com.getOrderTable().getUserName(),//收货人姓名
com.getOrderTable().getOrderPhone().substring(0,3)+"****"+com.getOrderTable().getOrderPhone().substring(7, com.getOrderTable().getOrderPhone().length()),//收货人手机号
// com.getOrderTable().getOrderShouhuodizhi(),//收货人地址
};
HSSFRow row01 = sheet.createRow(v++);
for (int i = 0; i < columnNamesT.length; i++) {
HSSFCell cell = row01.createCell((short) i);
if("null".equals(columnAsset[i])){
columnAsset[i]="";
}
cell.setCellValue(columnAsset[i]);
}
}
Date currentTime = new Date();
String dateString = DateUtil.data2String(currentTime);
//将文件夹名字于对应格式拼接起来
allExcelFileNameT+=dateString+".xls";
try {
response.setContentType("application/vnd.ms-excel;charset=utf-8");
wb.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
4、注意点:
介于是多表联查,所以字段比较多。映射关系的时候千万要仔细;
关于数据库是Date类型的,而前台传递过来的却是String类型的数据,我有必要说明一下:
后台处理可能会出现转换格式错误,当时解决这个办法找了好久,最后总结了一下往sql语句去变通
select * form t_table where date_format(o.order_time ,'%Y-%m')=#{betime}
这个是将它转为String类型的,后台and数据库通用。