excel导入的相关代码请移步:
请戳这里!!!!
————————————————————————————
maven导一下包:
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
前端页面:
导出:
前端代码:
//导出按钮绑定点击事件,调用后台方法
$("#xiazai").click(function() {
window.location.href = "download";
})
后台download方法:
@RequestMapping("/download")
@ResponseBody
public void download(HttpServletResponse response,PlatformRecords pr,HttpSession session) {
try {
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment;fileName=_platformrecords.xls");
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = null;
wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("sheet1", 0);// 创建一个可写入的工作表
ws.setColumnView(0, 25);
ws.setColumnView(1, 25);
ws.setColumnView(2, 25);
ws.setColumnView(3, 25);
ws.setColumnView(4, 25);
ws.setColumnView(5, 25);
ws.setColumnView(6, 25);// 设置列的宽度
ws.addCell(new Label(0, 0, "访问时间"));
ws.addCell(new Label(1, 0, "渠道ID"));
ws.addCell(new Label(2, 0, "渠道IP"));
ws.addCell(new Label(3, 0, "接口交易码"));
ws.addCell(new Label(4, 0, "状态"));
ws.addCell(new Label(5, 0, "响应时间"));
ws.addCell(new Label(6, 0, "备注"));
//调用查询时的方法,保存到session里
/*
List<PlatformRecords> list = service.downloadPlatformRecords(pr);
session.setAttribute("list", list);
*/
List<PlatformRecords> list = (List<PlatformRecords>) session.getAttribute("list");
for (int i = 0,length = list.size(); i < length; i++) {
pr = list.get(i);
ws.addCell(new Label(0, i + 1, pr.getInterviewtime()));
ws.addCell(new Label(1, i + 1, pr.getPlatformId()));
ws.addCell(new Label(2, i + 1, pr.getPlatformIp()));
ws.addCell(new Label(3, i + 1, pr.getTransCode()));
ws.addCell(new Label(4, i + 1, "正常"));
ws.addCell(new Label(5, i + 1, pr.getProcTime()+""));
ws.addCell(new Label(6, i + 1, "正常"));
}
wwb.write();// 从内存中写入文件
wwb.close(); // 关闭资源,释放内存
os.close();
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
欢迎各位大佬交流交流!