POI导出#JQuery+SpringMVC+POI

前端

html

<div id="kbDrawInfoTb" style="padding:5px 10px;">
<form id="kbDrawSearchForm" method="get">
手机号:<input class="easyui-textbox" type="text" name=mobileNumber style="width:150px" data-options="validType:['maxLength[11]']" />
领奖时间:<input class="easyui-datetimebox"  name="start" id="kbDrawStart" data-options="showSeconds:true,validType:'datatime'" style="width:150px"/>
~<input class="easyui-datetimebox"  name="last"  data-options='showSeconds:true,validType:["datatime","md[\"#kbDrawStart\"]"]' style="width:150px"/>&nbsp;
  <a href="javascript:;" class="easyui-linkbutton" οnclick="kbDrawInfoSearch()" data-options="iconCls:'icon-search'" style="width:80px">查询</a>
<a href="javascript:;" class="easyui-linkbutton" οnclick="kbDrawInfoRedo()" data-options="iconCls:'icon-redo'" style="width:80px">重置</a>
   
    <a href="javascript:;" class="easyui-linkbutton"  data-options="iconCls:'icon-redo'" οnclick="kbDrawExport()" style="width:80px">导出</a>
</form>
</div>

js

function kbDrawExport () {
var isValid = $( "#kbDrawSearchForm" ).form( 'validate' );
if ( !isValid ) {
return;
}
var form_start, form_last, form_mobileNumber, from_prizeCode, form_prizeKey, form_status;
form_start = $( '#kbDrawSearchForm input[name="start"]' ).val();
form_last = $( '#kbDrawSearchForm input[name="last"]' ).val();
form_mobileNumber = $( '#kbDrawSearchForm input[name="mobileNumber"]' ).val();
var param = JSON.stringify( {
'mobileNumber' : form_mobileNumber,
'start' : form_start,
'last' : form_last
} );
window.location.href = "xxxxxxx.ctrl?mobileNumber=" + form_mobileNumber + "&&prizeKey=" + form_prizeKey + "&&status=" + form_status + "&&start=" + form_start + "&&last=" + form_last;
}

后端

controller

     @RequestMapping(value = "/xxxxxx.ctrl")
public void exportBPDrawInfo(HttpServletResponse response, HttpServletRequest request) {
try {
String mobileNumber = request.getParameter("mobileNumber");
String start = request.getParameter("start");
String last = request.getParameter("last");
param.put("mobileNumber", mobileNumber);
param.put("start", start);
param.put("last", last);
bpService.exportBPDrawInfo(param, response);
}
catch (IllegalStateException i) {
}
}

service

public void exportBPDrawInfo(Map<String, Object> param, HttpServletResponse response) throws Exception {
HSSFWorkbook rankPoints = new HSSFWorkbook();
HSSFSheet sheet = rankPoints.createSheet();
// 样式
HSSFCellStyle style = rankPoints.createCellStyle();
style.setAlignment(CellStyle.ALIGN_RIGHT);
sheet.setDefaultColumnStyle(7, style);
List<BPDraw> dataList = bpMapper.bpDrawExport(param);
ExportExcel exportExcel = new ExportExcel(rankPoints, sheet);
// 计算该报表的列数
int number = 2;
// 给工作表列定义列宽(实际应用自己更改列数)
for (int i = 0; i < number; i++) {
sheet.setColumnWidth(i, 5000);
}
// 设置列头
HSSFRow row0 = sheet.createRow(0);
// 设置行高
row0.setHeight((short) 800);
HSSFCell row3Cell = null;
// 创建不同的LIST的列标题
row3Cell = row0.createCell(0);
row3Cell.setCellValue(new HSSFRichTextString("手机号"));
row3Cell = row0.createCell(1);
row3Cell.setCellValue(new HSSFRichTextString("奖品名称"));
row3Cell = row0.createCell(2);
row3Cell.setCellValue(new HSSFRichTextString("领奖时间"));
// 循环创建中间的单元格的各项的值
for (int i = 0; i < dataList.size(); i++) {
HSSFRow row = sheet.createRow((short) i + 1);
BPDraw elmDraw = dataList.get(i);
String mobileNumber = "";
if (elmDraw.getMobileNumber() != null) {
mobileNumber = String.valueOf(elmDraw.getMobileNumber());
}
String claimAt = "";
if (elmDraw.getClaimAt() != null) {
claimAt = String.valueOf(elmDraw.getClaimAt());
}
exportExcel.cteateCell(rankPoints, row, 0, mobileNumber);
exportExcel.cteateCell(rankPoints, row, 1, elmDraw.getName() != null?elmDraw.getName():"");
exportExcel.cteateCell(rankPoints, row, 2, claimAt);
}
OutputStream os = response.getOutputStream();
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date.format(new Date());
String currentTime = date.format(new Date()).toString();
String newDate = currentTime.substring(2, 4) + currentTime.substring(5, 7) + currentTime.substring(8, 10) + "_";
newDate += currentTime.substring(11, 13) + currentTime.substring(14, 16) + currentTime.substring(17, 19);
try {
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("美颜抽奖信息", "UTF8") + "_" + newDate + ".xls");
response.setContentType("application/octet-stream; charset=utf-8");
os = response.getOutputStream();
rankPoints.write(os);
os.flush();
}
catch (Exception e) {
LogManager.getLogger("transaction_s").error("exportPoints() err : ? ", e);
}
finally {
if (os != null) {
os.close();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值