ssm框架 POI导出Excel表格

@RequestMapping("/exportExcel/")

@ResponseBody

public void exportExcel(@PathVariable( "currencyName" ) String currencyName, GameStatisticIO param,HSSFWorkbook workbook,HttpServletRequest request,HttpServletResponse response ) throws Exception {

//对时间进行处理,前台传的日期转换成时间戳
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
format.setTimeZone( TimeZone.getTimeZone( "GMT+8" ) );
long beginTime = 0;
if( !StringUtils.isEmpty( param.getBeginDate() ) ) {
beginTime = format.parse( param.getBeginDate() + " 00:00:00" ).getTime();
}
long endTime = 0;
if( !StringUtils.isEmpty( param.getEndDate() ) ) {
endTime = format.parse( param.getEndDate() + " 23:59:59" ).getTime();
}

currencyName = currencyName.toUpperCase();

                //调用service层方法获取要导出数据

CurrencyInfoVO vo = this.currencyStatisticService.getGameCurrencyStatistic( currencyName, beginTime, endTime );

//2.创建空excel,创建sheet,创建标题,并且获取响应头
HSSFSheet sheet;
String filename="exportExcel";
response.setContentType("application ns.ms-excel");
response.setHeader("Content-disposition","attachment;filename=" + filename+".xls");
sheet = workbook.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("币种");
row.createCell(1).setCellValue(currencyName);
row= sheet.createRow(1);//创建标题行
row.createCell(0).setCellValue("开始日期");
String beginData = format.format(beginTime);
String endData = format.format(endTime);

row.createCell(1).setCellValue(beginData);
row.createCell(2).setCellValue("结束日期");
row.createCell(3).setCellValue(endData);

row = sheet.createRow(2);
row.createCell(0).setCellValue("竞猜");
row.createCell(2).setCellValue("赢利");
//3.遍历导出数据,填充到创建好的excel中
java.util.List gameStatistics = vo.getGameStatistics();
String withdrawalTotalAmount = vo.getWithdrawalTotalAmount();
String rechargeTotalAmount = vo.getRechargeTotalAmount();
String rechargeUsers = vo.getRechargeUsers();
if(null!=gameStatistics&&gameStatistics.size()>0) {
//有数据要导出
int index=3;
for(CurrencyStatisticVO currencyStatistics:gameStatistics) {
//3.1创建新行
row = sheet.createRow(index++);
//3.2在新行中创建新列,赋值
row.createCell(0).setCellValue(currencyStatistics.getGameName());
row.createCell(2).setCellValue(currencyStatistics.getProfitStr());
}

}
row = sheet.createRow(7);
row.createCell(0).setCellValue("用户充值金额");
row.createCell(2).setCellValue(rechargeTotalAmount);
row = sheet.createRow(8);
row.createCell(0).setCellValue("币种提现金额");
row.createCell(2).setCellValue(withdrawalTotalAmount);
row = sheet.createRow(9);
row.createCell(0).setCellValue("新增充值用户");
row.createCell(2).setCellValue(rechargeUsers);
row = sheet.createRow(10);
row.createCell(0).setCellValue("导出表格时间");
row.createCell(2).setCellValue(format.format(new Date()));
workbook.write(response.getOutputStream());

}



jsp页面

ssm框架 POI导出Excel表格_第1张图片

js写一个点击事件。切记不要用ajax请求,他返回的是json或者XML数据,而下载是直接下载文件不返回任何数据

ssm框架 POI导出Excel表格_第2张图片

Excel表格页面

ssm框架 POI导出Excel表格_第3张图片


你可能感兴趣的:(Java,POI,ssm,导出表格)