根据查询结果导出表格数据
bootstrap table 自定义导出excle
甲方非要根据查询结果导出数据
$("#导出按钮").on('click', function () {
var rows =$("#表格").bootstrapTable('getSelections');
if(rows.length==0){
var ids='1';
$.ajax({
type: "POST",
data: $('#zzform').serialize(),
url: root + "xx/saveIds",
success: function (datas) {
window.location=root+"xx/exportExcel";
}
});
}
ajax 先把查询条件存到session
/**
* 导出Excel先存ids
*/
@ResponseBody
@RequestMapping(value = "/xx/saveIds", method = RequestMethod.POST)
public void queryPersonInfoListInfo(Bean bean,HttpServletRequest request) throws Exception{
HttpSession session = request.getSession();
session.setAttribute("bean",bean);
}
/**
* @Author
* @Date 2019/05/15
* 导出Excel
*/
@ResponseBody
@RequestMapping(value = "/xx/exportExcel", method = RequestMethod.GET)
public ResponseJson exportExcel(HttpServletResponse response, HttpServletRequest request) throws Exception {
Bean bean= (Bean )request.getSession().getAttribute("bean");
xxxService.exportExcel(response,request,request.getSession().getAttribute("loginName").toString(),bean);
return VSUtils.getResponseJson("导出成功", true, null);
}
获取到实体对象
导出是根据实体对象 查询list
遍历list
导出数据
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("xxxx");
HSSFRow row = sheet.createRow(0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中
sheet.setColumnWidth(1, 4000);//设置
sheet.setColumnWidth(2, 4000);//
sheet.setColumnWidth(3, 4000);//
sheet.setColumnWidth(4, 5000);//
sheet.setColumnWidth(5, 5000);//
sheet.setColumnWidth(6, 4000);//
sheet.setColumnWidth(7, 4000);//
sheet.setColumnWidth(8, 4000);//
HSSFCell cell = row.createCell(0);
cell.setCellValue("aaaaaa");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("ssssss");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("xxx");
cell.setCellStyle(style);
List<Map<String,Object>> maps = xxMapper.selectInfo(loginName,bean);
for (int i = 0; i < maps.size(); i++) {//遍历id
Map<String,Object> map=maps.get(i);
if (HelpUtils.isNotEmpty(map)) {
HSSFRow row1 = sheet.createRow(i + 1);
HSSFCell cell1 = row1.createCell(0);
cell1.setCellStyle(style);
cell1.setCellValue(i + 1);
cell1 = row1.createCell(1);
cell1.setCellStyle(style);
cell1.setCellValue(map.get("xx").toString());
cell1 = row1.createCell(2);
cell1.setCellStyle(style);
Object original_room = map.get("xx");
if(original_room!=null && !original_room.equals("")){
cell1.setCellValue(original_room.toString());
}
Object original_cabinet = map.get("xx");
if( original_cabinet!=null && !original_cabinet.equals("") ){
cell1.setCellValue(original_cabinet.toString());
}
}
}
String path = request.getSession().getServletContext().getRealPath("") + "xxx.xls";
// 保存Excel文件
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
OutputStream outputStream = new FileOutputStream(path);
wb.write(outputStream);
outputStream.flush();
outputStream.close();
InputStream in = new FileInputStream(path);
int len = 0;
//创建数据缓冲区
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
//中文乱码编译
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
new String(file.getName().getBytes("gb2312"), "ISO8859-1"));
response.setHeader(headerKey, headerValue);
response.setContentType("application/vnd.ms-excel;");
while ((len = in.read(buffer)) > 0) {
//8.使用OutputStream将缓冲区的数据输出到客户端浏览器
out.write(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}