您的位置:首页 > 其它

导出excel

2016-01-13 13:59 211 查看
@RequestMapping("/exportExcel")

@ResponseBody

public String exportExcel(HttpServletRequest request,HttpServletResponse response){

long startTime = System.currentTimeMillis();

String reqSignDate = request.getParameter("signDate");

String reqProductID = request.getParameter("productID");

String reqProductType = request.getParameter("productType");

String reqSalesDeptId = request.getParameter("salesDeptId");

Long userId = ApplicationContext.getUser().getId();

Integer userType = ApplicationContext.getUser().getUserType();

request.setAttribute("userId", userId);

request.setAttribute("userType", userType);

setDataDictionaryArr(new String[] { EnumConstants.PRODUCT_ID,EnumConstants.PRODUCT_TYPE, EnumConstants.LOAN_STATUS, EnumConstants.CONTRACT_SRC });

request.setAttribute(GRID_ENUM_JSON, getEnumJson());

Integer productID = null;

if(null != reqProductID && !"".equals(reqProductID)){

productID = Integer.parseInt(reqProductID);

}

Long salesDeptId = null;

if(null != reqSalesDeptId && !"".equals(reqSalesDeptId)){

salesDeptId=Long.parseLong(reqSalesDeptId);

}

Integer productType = null;

if(null != reqProductType && !"".equals(reqProductType)){

productType=Integer.parseInt(reqProductType);

}

RemainingPrincipalDetailVO vo = new RemainingPrincipalDetailVO();

UserSession user = ApplicationContext.getUser();

List<Integer> qualifiedProductIds = new ArrayList<Integer>();

//admin 财务

if ("admin".equals(user.getLoginName()) || user.getUserType().equals(EnumConstants.UserType.FINANCE.getValue())) {

//falg =3;

vo.setProductType(null);

} else {

List<Product> products = productService.findProductTypeByUserId(user.getId());

for (Product product : products) {

qualifiedProductIds.add(product.getProductType());

if(product.getProductType() == 1){

//falg =1; //小企业贷

vo.setProductType(1);

}else{

//falg =2; //车贷

vo.setProductType(2);

}

}

if (qualifiedProductIds.size() < 1) {

return null;

}

}

if(null!=productType){

vo.setProductType(productType);

}

if(userType == 10){

vo.setUserType(userType);

}

vo.setSignDate(reqSignDate);

vo.setProductID(productID);

vo.setSalesDeptId(salesDeptId);

List<RemainingPrincipalDetail> detailList = remainingService.getRemainingPrincipalList(vo);

GrantRepaymentSummaryVO vo1 = new GrantRepaymentSummaryVO();

vo1.setId(vo.getSalesDeptId());

String saleName = "";

if(null != vo.getSalesDeptId()){

saleName = summaryService.getSalesDeptName(vo1);

}

Map map = new HashMap();

if(null != saleName && saleName != ""){

map.put("saleName",saleName);

}else{

map.put("saleName", "全部");

}

Date nowDate =new Date();

// 文件名

String fileName = "RemainingPrincipalDetailList" + DateUtil.defaultFormatMSDate(nowDate) + ".xls";

String downloadPathfileName = "剩余本金明细表" + DateUtil.defaultFormatDay(nowDate) + ".xls";

String downloadPath = credit2Properties.getDownloadPath();

File file = new File(downloadPath + File.separator + "report" + File.separator + "remainingPrincipal"+ File.separator + DateUtil.defaultFormatDay(nowDate));

if (!file.exists()) {// 不存在则创建该目录

file.mkdirs();

}

OutputStream os = null;

try {

os = new FileOutputStream(downloadPath + File.separator + "report" + File.separator + "remainingPrincipal" + File.separator + DateUtil.defaultFormatDay(nowDate)+ File.separator + fileName.trim().toString());

//导出数据

exportDatas(detailList,vo,map,os,response);

} catch (FileNotFoundException e) {

e.printStackTrace();

return "导出excel出错";

}

// 下载Escel文件的路径

String filePath = downloadPath + File.separator + "report" + File.separator + "remainingPrincipal" + File.separator + DateUtil.defaultFormatDay(nowDate)+ File.separator + fileName;

try {

// 下载Excel文件到客户端

if (FileUtil.downLoadFile(filePath, response, downloadPathfileName, "xlsx")) {

// 导出成功后删除导出的文件

FileUtil.deleteFile(filePath);

} else {

return "导出excel出错";

}

} catch (Exception e) {

e.printStackTrace();

return "导出excel出错";

}

long endTime = System.currentTimeMillis();

// 插入系统日志

SysLog sysLog = new SysLog();

sysLog.setOptModule(EnumConstants.OptionModule.PRE_CREDIT_REPORT.getValue());

sysLog.setOptType(EnumConstants.OptionType.DELIVERY_SUMMARY_TABLE.getValue());

sysLog.setRemark("剩余本金明细表导出用时" +(double)(endTime - startTime) / 1000 +"秒");

sysLogService.insert(sysLog);

logger.info("插入系统日志结束");

return "success";

}

//真正导出数据

public void exportDatas(List<RemainingPrincipalDetail> detailList,RemainingPrincipalDetailVO vo,Map map,OutputStream os,HttpServletResponse response){

try{

XSSFWorkbook wb = new XSSFWorkbook();

//行显示样式

XSSFCellStyle cellStyle_CN = wb.createCellStyle();//创建数据单元格样式(数据库数据样式)

cellStyle_CN.setBorderBottom(XSSFCellStyle.BORDER_THIN);//单元格边线为细线

cellStyle_CN.setBorderLeft(XSSFCellStyle.BORDER_THIN);

cellStyle_CN.setBorderRight(XSSFCellStyle.BORDER_THIN);

cellStyle_CN.setBorderTop(XSSFCellStyle.BORDER_THIN);

cellStyle_CN.setBorderBottom(XSSFCellStyle.BORDER_THIN);

cellStyle_CN.setAlignment(XSSFCellStyle.ALIGN_CENTER);//左右居中

cellStyle_CN.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//上下居中

cellStyle_CN.setWrapText(false);//不换行显示

//标头

XSSFCellStyle condition_cellStyle = wb.createCellStyle();//创建一个单元格样式

XSSFFont condition_font = wb.createFont();

condition_font.setFontName("宋体");//设置头部字体为宋体

condition_font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体

//condition_font.setFontHeightInPoints((short)20);

condition_cellStyle.setFont(condition_font);//单元格样式使用字体

condition_cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//上下居中

condition_cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);

condition_cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);

condition_cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);

condition_cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);

condition_cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//左右居中

//添加表头背景颜色

condition_cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());

condition_cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());

condition_cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

//内容

XSSFCellStyle con_style = wb.createCellStyle();//创建一个单元格样式

con_style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//上下居中

con_style.setBorderLeft(XSSFCellStyle.BORDER_THIN);

con_style.setBorderRight(XSSFCellStyle.BORDER_THIN);

con_style.setBorderTop(XSSFCellStyle.BORDER_THIN);

con_style.setBorderBottom(XSSFCellStyle.BORDER_THIN);

con_style.setAlignment(XSSFCellStyle.ALIGN_CENTER);//左右居中

//标题

XSSFCellStyle titlest = wb.createCellStyle();//创建一个单元格样式

XSSFFont title_font = wb.createFont();

title_font.setFontName("宋体");//设置头部字体为宋体

title_font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体

title_font.setFontHeightInPoints((short)40);

titlest.setFont(title_font);//单元格样式使用字体

titlest.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//上下居中

titlest.setBorderLeft(XSSFCellStyle.BORDER_THIN);

titlest.setBorderRight(XSSFCellStyle.BORDER_THIN);

titlest.setBorderTop(XSSFCellStyle.BORDER_THIN);

titlest.setAlignment(XSSFCellStyle.ALIGN_CENTER);//左右居中

XSSFCellStyle style = wb.createCellStyle();

style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);

style.setWrapText(true);

/*XSSFFont font = wb.createFont();

font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);

font.setFontName("宋体");

style.setFont(font);*/

//标题

String[] title = {"营业部","贷款种类","客户姓名","身份证号码","还款日","放款金额","贷款期限","合同金额","剩余本金","剩余期数","机构名称","最后一次还款日期","审核员","客服","业务主任工号","签约日期","合同来源","主业类型","借款次数","上笔结清时间","账户状态","逾期180天"};

logger.info("sheet 脚本创建成功。");

XSSFSheet sheet = wb.createSheet();

sheet.setDefaultColumnWidth(22);

XSSFCell cell1 = null;

XSSFRow row = sheet.createRow(0);

sheet.addMergedRegion(new CellRangeAddress(0,0,0,21));

sheet.getNumMergedRegions() ;

row.setHeight((short)1000);

cell1 = row.createCell(0);

cell1.setCellStyle(titlest);

cell1.setCellValue("剩余本金明细表");

XSSFCell cell2 = null;

row = sheet.createRow(1);

sheet.addMergedRegion(new CellRangeAddress(1,1,0,21));

cell2 = row.createCell(0);

cell2.setCellValue("统计日期:" + vo.getSignDate());

row = sheet.createRow(2);

cell2 = row.createCell(0);

sheet.addMergedRegion(new CellRangeAddress(2,2,0,21));

cell2.setCellValue("营 业 部:" + map.get("saleName"));

for(int i=0;i<detailList.size();i++){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

row = sheet.createRow(i+3);

if(i==0){

XSSFCell cell4 = null;

for(int ti = 0; ti < title.length; ti++){

cell4 = row.createCell(ti);

cell4.setCellStyle(condition_cellStyle);

cell4.setCellValue(title[ti]);

}

}else{

XSSFCell cell = null;

//for(int j=0;j<title.length;j++){

cell = row.createCell(0);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getSalesDept())

cell.setCellValue(detailList.get(i).getSalesDept());

else

cell.setCellValue("");

cell = row.createCell(1);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getLoanType())

cell.setCellValue(detailList.get(i).getLoanType());

else

cell.setCellValue("");

cell = row.createCell(2);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getPersonName())

cell.setCellValue(detailList.get(i).getPersonName());

else

cell.setCellValue("");

cell = row.createCell(3);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getIdNum())

cell.setCellValue(detailList.get(i).getIdNum());

else

cell.setCellValue("");

cell = row.createCell(4);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getReturnDate())

cell.setCellValue(detailList.get(i).getReturnDate());

else

cell.setCellValue("");

cell = row.createCell(5);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getGrantMoney())

cell.setCellValue(detailList.get(i).getGrantMoney().doubleValue());

else

cell.setCellValue("");

cell = row.createCell(6);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getLoanTimeLimit())

cell.setCellValue(detailList.get(i).getLoanTimeLimit());

else

cell.setCellValue("");

cell = row.createCell(7);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getPactMoney())

cell.setCellValue(detailList.get(i).getPactMoney().doubleValue());

else

cell.setCellValue("");

cell = row.createCell(8);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getResidualPactMoney())

cell.setCellValue(detailList.get(i).getResidualPactMoney().doubleValue());

else

cell.setCellValue("");

cell = row.createCell(9);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getResidualTime())

cell.setCellValue(detailList.get(i).getResidualTime());

else

cell.setCellValue("");

cell = row.createCell(10);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getOrganName())

cell.setCellValue(detailList.get(i).getOrganName());

else

cell.setCellValue("");

cell = row.createCell(11);

cell.setCellStyle(con_style);

if(null == detailList.get(i).getFactreturndate()){

cell.setCellValue("");

}else{

cell.setCellValue(sdf.format(detailList.get(i).getFactreturndate())); //

}

cell = row.createCell(12);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getAssessorName())

cell.setCellValue(detailList.get(i).getAssessorName());

else

cell.setCellValue("");

cell = row.createCell(13);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getServiceName())

cell.setCellValue(detailList.get(i).getServiceName());

else

cell.setCellValue("");

cell = row.createCell(14);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getDirectorCode())

cell.setCellValue(detailList.get(i).getDirectorCode());

else

cell.setCellValue("");

cell = row.createCell(15);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getSignDate())

cell.setCellValue(sdf.format(detailList.get(i).getSignDate())); //

else

cell.setCellValue("");

cell = row.createCell(16);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getContractSrc())

cell.setCellValue(detailList.get(i).getContractSrc());

else

cell.setCellValue("");

cell = row.createCell(17);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getMajorBusiness() && !"".equals(detailList.get(i).getMajorBusiness()))

cell.setCellValue(detailList.get(i).getMajorBusiness());

else

cell.setCellValue("");

cell = row.createCell(18);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getLoanNumTime())

cell.setCellValue(detailList.get(i).getLoanNumTime());

else

cell.setCellValue("");

cell = row.createCell(19);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getLastEndRepayDate() && !"".equals(detailList.get(i).getLastEndRepayDate()))

cell.setCellValue(sdf.format(detailList.get(i).getLastEndRepayDate())); //

else

cell.setCellValue("");

cell = row.createCell(20);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getAccountStatus())

cell.setCellValue(detailList.get(i).getAccountStatus());

else

cell.setCellValue("");

cell = row.createCell(21);

cell.setCellStyle(con_style);

if(null != detailList.get(i).getBeyondHalfYears())

cell.setCellValue(detailList.get(i).getBeyondHalfYears());

else

cell.setCellValue("");

//}

}

}

wb.write(os);

os.flush();

os.close();

logger.info("导出结束!");

} catch (Exception exception) {

logger.error("导出失败!",exception);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: