导出excel文件


@RequestMapping(value = "/exportUserSourceConsumeFundList", method = RequestMethod.GET)
public void doExportUserSourceConsumeFundList(HttpServletResponse response, UserProfileVO userProfile, Integer exportType) throws Exception {

LOGGER.info("用户消费金额导出:usersource/exportUserSourceConsumeFundList");

if (null != userProfile.getBeginTime()) {
userProfile.setBeginDateTime(DateUtil.parseDate(userProfile.getBeginTime()));
}
if (null != userProfile.getEndTime()) {
userProfile.setEndDateTime(DateUtil.parseDate(userProfile.getEndTime()));
}
List<UserProfileVO> userSourceConsumeFundList = userProfileBO.exportUserSourceConsumeFundList(userProfile);

String[] columns = {"账户用户名","用户真实姓名","手机号","消费金额","加金额","注册时间"};
String[] dataFields = {"username","name","mobile","fundAmount","goldAmount","registerTime"};

// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");

ServletOutputStream out = response.getOutputStream();

if (exportType == ExportFileType.XLS.getKey()) {
Workbook wb = new HSSFWorkbook();
response.setHeader("Content-Disposition", "attachment;filename=" + new String((EXPORT_NAME_USER_CONSUME_FUND + DateUtil.formatStandardDate(new Date()) + EXTENSION_XLS).getBytes(), "iso-8859-1"));
FileUtil.<UserProfileVO>exportExcel(out, columns, dataFields, userSourceConsumeFundList, wb, EXPORT_NAME_USER_CONSUME_FUND);
} else if (exportType == ExportFileType.XLSX.getKey()) {
Workbook wb = new XSSFWorkbook();
response.setHeader("Content-Disposition", "attachment;filename=" + new String((EXPORT_NAME_USER_CONSUME_FUND + DateUtil.formatStandardDate(new Date()) + EXTENSION_XLSX).getBytes(), "iso-8859-1"));
FileUtil.<UserProfileVO>exportExcel(out, columns, dataFields, userSourceConsumeFundList,wb, EXPORT_NAME_USER_CONSUME_FUND);
} else if (exportType == ExportFileType.CSV.getKey()) {
response.setHeader("Content-Disposition", "attachment;filename=" + new String((EXPORT_NAME_USER_CONSUME_FUND + DateUtil.formatStandardDate(new Date()) + EXTENSION_CSV).getBytes(), "iso-8859-1"));
FileUtil.<UserProfileVO>exportCsvFile(out, columns, dataFields, userSourceConsumeFundList, EXPORT_NAME_USER_CONSUME_FUND);
}
}



public static <T> void exportExcel(OutputStream out,String[] columns, String[] dataFields, List<T> dataList, Workbook wb, String fileName) throws Exception {
// 创建第一个sheet(页),并命名
Sheet sheet = wb.createSheet(fileName);
// 创建第一行
Row row = sheet.createRow((short) 0);
Cell cell = null;

// 创建title
for (int i = 0; i < columns.length; i++) {
cell = row.createCell(i);
cell.setCellValue(columns[i]);
}

for (int i = 0; i < dataList.size(); i++) {
row = sheet.createRow(i + 1);
T userTemp = dataList.get(i);
HashMap<String, Object> userMap = HashMapUtil.objToHash(userTemp);

for (int j = 0; j < dataFields.length; j++) {
cell = row.createCell(j);
String dataField = dataFields[j];
Object value = userMap.get(dataField);
if (value != null) {
cell.setCellValue(value.toString());
} else {
cell.setCellValue("");
}
}
}

ByteArrayOutputStream os = new ByteArrayOutputStream();

try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}

byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);


BufferedInputStream bis = null;
BufferedOutputStream bos = null;

try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);

byte[] buff = new byte[2048];
int bytesRead;

// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}

} catch (final IOException e) {
e.getMessage();
} finally {
if (bis != null) {
bis.close();
}
if (bos != null){
bos.close();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值