//控制层
@RequestMapping(value = "/download/downloadrechargedata", method = RequestMethod.GET)
public void download(HttpServletResponse response,
HttpServletRequest request) throws Exception {
//生成一个客户端临时文件夹里
File temp = SystemUtils.getJavaIoTmpDir();
String basePath = FilenameUtils.concat(temp.getAbsolutePath(),
getTempFilename());
File baseDir = new File(basePath);
FileOutputStream fos = null;//输出流
InputStream input = null;
ServletOutputStream output = null;
String excelName = "rechargeData.xlsx";
try {
//创建路径
FileUtils.forceMkdir(baseDir);
String filePath = FilenameUtils.concat(basePath, excelName);//文件的下载路径
File buildfile = new File(filePath);
fos = new FileOutputStream(buildfile);
//生成对账表,并生成excel写入输出流里。
fundsExcelHandlerService.buildRechargeAccount(excelName,fos);
//把生成的路径写到一个输入流
input = new FileInputStream(buildfile);
// 文件下载
//aapplication单词并没有错,如果去掉一个a就会报错了哈。
response.setContentType("aapplication/vnd.ms-excel ;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("content-disposition",
"attachment; filename="+excelName);
output = response.getOutputStream();
IOUtils.copy(input, output);
//fos.flush();
output.flush();
} catch (Exception e) {
log.error(e.getMessage());
} finally {
//IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(fos);
if (baseDir != null)
try {
FileUtils.forceDelete(baseDir);
} catch (Exception e) {
}
}
}
//service层
@Override
public synchronized void buildRechargeAccount(String excelName,
FileOutputStream fos) {
// 获取数据
List<FundsRecharge> list = fundsRechargeService.findAll();
// 生成充值对账表
Workbook wb = new XSSFWorkbook(); //
// 创建一个表
Sheet sheet = wb.createSheet();
// 创建表头
// 创建一行
Row row_one = sheet.createRow(0);
setCellGBKValue(wb, row_one.createCell((short) 0), "资金流水号");
setCellGBKValue(wb, row_one.createCell((short) 1), "资金账号");
setCellGBKValue(wb, row_one.createCell((short) 2), "用户名");
setCellGBKValue(wb, row_one.createCell((short) 3), "真实姓名");
setCellGBKValue(wb, row_one.createCell((short) 4), "充值金额(元)");
setCellGBKValue(wb, row_one.createCell((short) 5), "实际到账(元)");
setCellGBKValue(wb, row_one.createCell((short) 6), "充值时间 ");
setCellGBKValue(wb, row_one.createCell((short) 7), "充值渠道");
setCellGBKValue(wb, row_one.createCell((short) 8), "可用余额(元)");
// 增加数据
Row row_index = null;
FundsRecharge fundsRecharge = null;
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
fundsRecharge = list.get(i);
row_index = sheet.createRow(i + 2);
setCellGBKValue(wb, row_index.createCell((short) 0),
fundsRecharge.getFundsSerialNumber());
setCellGBKValue(wb, row_index.createCell((short) 1),
fundsRecharge.getFundsAccount());
if (fundsRecharge.getBaseAccount() != null) {
setCellGBKValue(wb, row_index.createCell((short) 2),
fundsRecharge.getBaseAccount().getUsername());
setCellGBKValue(wb, row_index.createCell((short) 3),
fundsRecharge.getBaseAccount().getDisplayName());
} else {
setCellGBKValue(wb, row_index.createCell((short) 2), " ");
setCellGBKValue(wb, row_index.createCell((short) 3), " ");
}
setCellGBKValue(wb, row_index.createCell((short) 4),
fundsRecharge.getRechargeAmount());
setCellGBKValue(wb, row_index.createCell((short) 5),
fundsRecharge.getArriveAmount());
setCellGBKValue(wb, row_index.createCell((short) 6),
fundsRecharge.getRechargeTime());
setCellGBKValue(wb, row_index.createCell((short) 7),
fundsRecharge.getRechargeTime());
setCellGBKValue(wb, row_index.createCell((short) 8),
fundsRecharge.getAvailableAmount());
}
}
// Write the output to a file
// FileOutputStream fileOut = null;
// File excel = null;
//把数据写到一个临时文件夹里。
//生成一个客户端临时文件夹里
// File temp = SystemUtils.getJavaIoTmpDir();
// String basePath = FilenameUtils.concat(temp.getAbsolutePath(),getTempFilename());
// String filePath = null;
// File excel = null;
try {
// //获得临时文件夹基本路径
// File baseDir = new File(basePath);
// FileUtils.forceMkdir(baseDir);//创建此路径
// filePath = FilenameUtils.concat(basePath,excelName);//组合此路径
//
// log.info("下载的路径"+filePath);
// excel = new File(filePath);
//fos = new FileOutputStream(excel);
//把excel数据写进输出流中。
wb.write(fos);
} catch (Exception e) {
log.error("{}", e.getMessage());
} finally {
try {
fos.close();
} catch (IOException e) {
log.error("{}", e.getMessage());
}
}
//return filePath;
}