public void exportExcel() throws Exception {
String name = "";
//创建HSSWordBook对象 SXSSFWorkbook hssfWorkbook = new SXSSFWorkbook(100); //创建工作薄 Date time = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Sheet sheet = hssfWorkbook.createSheet(name); //合并单元格 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 10)); //设置第一行样式 // 2.生成样式对象 CellStyle style = hssfWorkbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 CellStyle style1 = hssfWorkbook.createCellStyle(); style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //设置字体 Font font = hssfWorkbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体加粗 font.setFontHeightInPoints((short) 10); style.setFont(font); //创建第一行 Row hssfRow = sheet.createRow(0); hssfRow.setHeight((short) 500); //创建单元格 Cell hssfCell = hssfRow.createCell(0); //设置第一行单元格标题内容 hssfCell.setCellValue(name); hssfCell.setCellStyle(style); //在sheet莉创建行 Row row2 = sheet.createRow(1);
//出账导出 flag = new String[]{"账号", "来源", "提现金额", "剩余金额", "提现卡号", "状态", "提现时间", "到账时间", "操作人"}; for (int i = 0; i < flag.length; i++) { Cell cell = row2.createCell(i); cell.setCellStyle(style); //创建标题行 cell.setCellValue(flag[i]); } PageInfo pageInfo = new PageInfo(); AccountDto accountDto = new AccountDto(); accountDto.setFlag("2"); pageInfo.setSizeOfPage(5000000); List<AccountTradeRecord> list = iAccountTradeRecordService.selectAccountTradeRecord(accountDto, pageInfo); //出账 List<AppearAccountVo> appearAccount = new ArrayList<>(); //出账 if (list != null && list.size() > 0) { for (AccountTradeRecord account : list) { AppearAccountVo appear = new AppearAccountVo(); if (account.getOwnerType().equals("business")) { BusinessMan businessMan = businessManService.getBusinessManById(account.getOwnerId()); if (businessMan != null) { appear.setPhone(businessMan.getMobile()); } } else { Company company = companyService.getByComId(account.getOwnerId()); if (company != null) { appear.setPhone(company.getPhone()); } } //交易金额 appear.setAmount(account.getAmount()); //来源 appear.setSource(account.getSource()); appear.setAuditName(account.getAuditName()); appear.setCardNo(account.getCardNo()); //状态 appear.setStep(account.getStep()); appear.setCreateTime(account.getCreateTime()); appear.setDoneTime(account.getDoneTime()); appear.setRemnant(account.getRemnant()); appearAccount.add(appear); } } //拼接表格数据 sheet.setColumnWidth(0, 5000); sheet.setColumnWidth(1, 5000); sheet.setColumnWidth(2, 5000); sheet.setColumnWidth(3, 5000); sheet.setColumnWidth(4, 5000); sheet.setColumnWidth(5, 5000); sheet.setColumnWidth(6, 6000); sheet.setColumnWidth(7, 6000); sheet.setColumnWidth(8, 5000); if (appearAccount.size() > 0) { for (int i = 0; i < appearAccount.size(); i++) { AppearAccountVo appearAccountVo = appearAccount.get(i); Row row3 = sheet.createRow(i + 2); Cell cell = row3.createCell(0); cell.setCellStyle(style1); //账号 cell.setCellValue(appearAccountVo.getPhone() == null ? "" : appearAccountVo.getPhone()); cell = row3.createCell(1); cell.setCellStyle(style1); //来源 cell.setCellValue(appearAccountVo.getSource() == null ? "" : appearAccountVo.getSource()); cell = row3.createCell(2); cell.setCellStyle(style1); //提现金额 cell.setCellValue((appearAccountVo.getAmount() == null) ? 0 : appearAccountVo.getAmount()); cell = row3.createCell(3); cell.setCellStyle(style1); //剩余金额 cell.setCellValue(appearAccountVo.getRemnant() == null ? 0 : appearAccountVo.getRemnant()); cell = row3.createCell(4); cell.setCellStyle(style1); //提现卡号 cell.setCellValue(appearAccountVo.getCardNo() == null ? "" : appearAccountVo.getCardNo()); cell = row3.createCell(5); cell.setCellStyle(style1); //状态 if (StringUtils.isNotBlank(appearAccountVo.getStep())) { if (appearAccountVo.getStep().equals("ing")) { appearAccountVo.setStep("进行中"); } else if (appearAccountVo.getStep().equals("success")) { appearAccountVo.setStep("成功"); } else if (appearAccountVo.getStep().equals("fail")) { appearAccountVo.setStep("失败"); } else if (appearAccountVo.getStep().equals("create")) { appearAccountVo.setStep("创建"); } } cell.setCellValue(appearAccountVo.getStep() == null ? "" : appearAccountVo.getStep()); cell = row3.createCell(6); cell.setCellStyle(style1); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //提现时间 cell.setCellValue(appearAccountVo.getCreateTime() == null ? "" : sd.format(appearAccountVo.getCreateTime())); cell = row3.createCell(7); cell.setCellStyle(style1); //到账时间 cell.setCellValue(appearAccountVo.getDoneTime() == null ? "" : sd.format(appearAccountVo.getDoneTime())); cell = row3.createCell(8); cell.setCellStyle(style1); //操作人 cell.setCellValue(appearAccountVo.getAuditName()); } }
//获取输出流 OutputStream output = response.getOutputStream(); response.reset(); //设置分区中文名 String filename = name + sdf.format(time); //设置响应的编码 response.setContentType("application/x-download");//下面三行是关键代码,处理乱码问题 response.setCharacterEncoding("utf-8"); //设置浏览器响应头对应的Content-disposition response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gbk"), "iso8859-1") + ".xls"); // response.setHeader("Content-disposition", "attachment;filename=account_data_"+filename+".xls"); // response.setHeader("Content-disposition", "attachment;filename="+filename+".xls"); hssfWorkbook.write(output); output.close(); }