列表数据导出为Excel

页面代码:

function exportAsExcel(){

var ppErInd=$("#ppErInd").val();

var psOdInd=$("#psOdInd").val();

var repaymentTimeRange=$("#repaymentTimeRange").val();

var repaymentTimeRange=$("#repaymentYesTimeRange").val();

window.location.href='<%=basePath%>/appr/hrharepaymentregisterdetail/exportAsExcel.do?ppErInd='+ppErInd+'&psOdInd='+psOdInd+'&repaymentTimeRange='+repaymentTimeRange+'&repaymentYesTimeRange='+repaymentYesTimeRange;}

controller代码:

/**
*
* @Description 列表excel导出
* @return
* @throws IOException
* @see 需要参考的类或方法
*/
@RequestMapping("exportAsExcel")
public void exportAsExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
PageData pd = this.getPageData();
List<HrhaBorrowDetailVo> list = null;
String tableName=null;
String sheetName="放款信息";
Class<HrhaBorrowDetailVo> clazz=HrhaBorrowDetailVo.class;
Map<String, Map<String, String>> map=new HashMap<String, Map<String,String>>();//字典项转码
Map<String, String> dict=new HashMap<String,String>();
dict.put("111", "待审批");
dict.put("991", "准入否决");
dict.put("997", "通过");
dict.put("998", "审批否决");
map.put("dealStatus", dict);
String[] excelHeader = { //表头
"借款编号",
"批次号",
"申请流水号",
"商户名称",
"申请人姓名",
"申请金额",
"身份证号",
"批次发送日期",
"审批状态"};
String[] showColumn = { //对应字段名
"borrowId",
"batchNo",
"applSeq",
"bchName",
"custName",
"applyAmt",
"idNo",
"sendDate",
"dealStatus"};
UNIDProducer uniCode = new UNIDProducer();
String docNo = uniCode.getUNID();
SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
String date = format.format(new Date());
tableName="放款信息"+docNo+date+".xls";
Page<List<HrhaBorrowDetailVo>> page=PageUtils.getPage(pd);
page.setShowCount(Integer.MAX_VALUE);
try {
list = hrhaBorrowDetailService.findAllHrBatchDetailListWithCond(page, pd);//导出查询的所有数据,不涉及分页
} catch (Exception e) {
e.printStackTrace();
}
HSSFWorkbook wb = hrhaBorrowDetailService.export(list,clazz,response,tableName,sheetName,excelHeader,showColumn,map);
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}

Service代码:

/**
	 * 
	 * @Description  列表excel导出
	 * @param list 需要导出的list
	 * @param Class 需要导出的list的类型
	 * @param HttpServletResponse
	 * @param tableName 文件名
	 * @param sheetName sheet名称
	 * @param excelHeader 表头字段名称
	 * @param showColumn 对应字段名称
	 * @param dictMap 相应字段字典项
	 * @return
	 * @see 需要参考的类或方法
	 */
	public HSSFWorkbook export(List<?> list,Class<?> clazz,HttpServletResponse response,String tableName,String sheetName,String[] excelHeader,String[] showColumn,Map<String, Map<String, String>> dictMap) {
		HSSFWorkbook wb = new HSSFWorkbook();
		Map map=new HashMap();
        HSSFSheet sheet = wb.createSheet(sheetName);//sheet命名  
        response.setContentType("application/vnd.ms-excel");    
        response.setHeader("Content-disposition", "attachment;filename="+tableName);//文件名 
        HSSFRow row = sheet.createRow((int) 0);    
        HSSFCellStyle style = wb.createCellStyle();    
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);    
    
        for (int i = 0; i < excelHeader.length; i++) {    
            HSSFCell cell = row.createCell(i);    
            cell.setCellValue(excelHeader[i]);    
            cell.setCellStyle(style);    
            sheet.autoSizeColumn(i);    
        }    
        for (int i = 0; i < list.size(); i++) {  //为每列赋值  
            row = sheet.createRow(i + 1);    
            Object vo = list.get(i);
            vo=clazz.cast(vo);
            map=ComponentHelperUtils.getBeanToMap(vo);
            for(int j=0;j<showColumn.length;j++){
        		if(dictMap.containsKey(showColumn[j])){
        			row.createCell(j).setCellValue(dictMap.get(showColumn[j]).get(String.valueOf(map.get(showColumn[j]))));
        		}else{
        			row.createCell(j).setCellValue(String.valueOf(map.get(showColumn[j])));    
        		}
            }
        
        }    
        return wb; 
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值