页面代码:
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; }