public void export(@RequestBody PassRecordFindAllVm model, HttpServletResponse response) throws Exception {
List<PassRecordEntity> allList = passRecordService.exportAll(model);
String[][] strArray = allList.stream().map(x -> {
String[] str = new String[9];
str[0] = x.getUserName();
str[1] = x.getDepartmentName();
str[2] = x.getCharacteristic();
str[3] = x.getDeviceName();
str[4] = x.getDevicePosition();
str[5] = x.getWayOfAccess();
str[6] = x.getStatus();
str[7] = x.getTransitTime();
return str;
}).toArray(String[][]::new);
String[] headers = {"人员姓名", "部门", "特征", "门禁名称", "设备位置","通行方式","验证结果","通行时间"};
ExcelUtil excelUtil = new ExcelUtil();
excelUtil.exporteExcel("通行记录", headers, strArray, response);
}
//List<PassRecordEntity> allList = passRecordService.exportAll(model)此方法为查询数据库获取一个list集合PassRecordEntity类内容随意,str数组从0开始依次赋予你想要导出的值
/**
* 导出excel文件
*
* @param title 表sheet的名字
* @param headers 表头
* @param dataList 正文单元格
* @param response 获取输出流
*/
public void exporteExcel(String title, String[] headers, String[][] dataList, HttpServletResponse response) throws UnsupportedEncodingException {
response.setContentType("application/vnd.ms-excel");
String fileName = URLEncoder.encode(title, "UTF-8").replaceAll("\\+", "%20");
response.addHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
OutputStream fos = null;
HSSFWorkbook workBook = new HSSFWorkbook();
createSheet(title, headers, dataList, workBook);
try {
fos = response.getOutputStream();
workBook.write(fos);
} catch (IOException e) {
log.info("写入文件失败" + e.getMessage());
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}