框架中的导出excel表格实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_40214184/article/details/93725272

1、在前端页面中写入代码:

<a class="btn btn-outline btn-danger btn-xs" id="export"><i class="fa th-list"></i>导出excel</a>



//导出excel
        $("#export").click(function(){
    		var listForm = $("#listForm").serialize();
    		  window.location = "${base}/admin/withdraw_deposit/exportExcel.jhtml?"+listForm;
    		});

2、controller处理

/**
	 * 导出Excel
	 * 
	 * @param pageable
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/exportExcel" }, method = { RequestMethod.GET })
	@ResponseBody
	public void exportExcel(Pageable pageable, ModelMap model,HttpServletRequest request,HttpServletResponse response) {
		
		Map<String,Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
		Map map=new HashMap();
		  for (String key : paramter.keySet()) {  
	            if(!StringUtil.isEmpty( paramter.get(key).toString())){
	            	map.put(key,  paramter.get(key));
	            }
	        }  
		pageable.setParameter(map);
		pageable.setPageNumber(1);
		pageable.setPageSize(Integer.parseInt(paramter.get("total").toString()));
		Page<WithdrawDeposit> page = this.withdrawDepositService.selectByParams(pageable);
		/*格式化类型、状态字段*/
		List<WithdrawDeposit> content = page.getContent();
		for (WithdrawDeposit withdrawDeposit : content) {
			withdrawDeposit.setTypeStr(withdrawDeposit.getType()==1 ? "余额":"佣金");
			if(withdrawDeposit.getStatus() == 1){
				withdrawDeposit.setStatusStr("待处理");
			}else if(withdrawDeposit.getStatus() == 2){
				withdrawDeposit.setStatusStr("已处理");
			}else if(withdrawDeposit.getStatus()==3){
				withdrawDeposit.setStatusStr("已拒绝");
			}
		}
		/*格式化时间字段*/
		for (WithdrawDeposit withdrawDeposit : content) {
			try{
				if(withdrawDeposit.getCreateTime() != null){
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					withdrawDeposit.setCreateTimeStr(sdf.format(withdrawDeposit.getCreateTime()));
				}
				//处理时间ProcessTime
				if(withdrawDeposit.getProcessTime() != null){
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					withdrawDeposit.setProcessTimeStr(sdf.format(withdrawDeposit.getProcessTime()));
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	//report
	String[] titles ={"编号","昵称","手机号","类型","提现金额","账户姓名","开户行","银行卡号","备注","申请时间","状态","处理时间"};
	String[] properties ={"id","nickname","phone","typeStr","amount","accountName","bank","cardNum","remark","createTimeStr","statusStr","processTimeStr"};
	ExcelView ev = new ExcelView("提现管理表.xls", "提现管理表", properties, titles, new Integer[]{5000,5000,7000,5000,5000,5000,5000,8000,5000,7000,5000,7000}, null, content, null);
	HSSFWorkbook hwb = new HSSFWorkbook();		
	try {
		ev.buildExcelDocument(model, hwb, request, response);
		hwb.write(response.getOutputStream());
	} catch (Exception e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

 

展开阅读全文

没有更多推荐了,返回首页