EntranceGuardUploadEventModel eventModel = new EntranceGuardUploadEventModel();
List<EntranceGuardUploadEvent> dataList = entranceGuardUploadEventService.queryAll(eventModel);
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = workbook.createSheet(I18NUtil.getMessage(request, "employee.iodata.exportreport"));
// 第三步,在sheet中添加表头第0行
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
row.createCell(0).setCellValue(I18NUtil.getMessage(request, "employee.label.employee.id"));
row.createCell(1).setCellValue(I18NUtil.getMessage(request, "employee.label.employee.name"));
row.createCell(2).setCellValue(I18NUtil.getMessage(request, "table.coloum.depart.name"));
row.createCell(3).setCellValue(I18NUtil.getMessage(request, "employee.label.cardno"));
row.createCell(4).setCellValue(I18NUtil.getMessage(request, "queryCondition.cardtype.describe"));
row.createCell(5).setCellValue(I18NUtil.getMessage(request, "queryCondition.doorarea.name.select"));
row.createCell(6).setCellValue(I18NUtil.getMessage(request, "queryCondition.door.name.select"));
row.createCell(7).setCellValue(I18NUtil.getMessage(request, "queryCondition.doorstatus.describe"));
row.createCell(8).setCellValue(I18NUtil.getMessage(request, "report.iotime.statustime"));
//设置表头居中
row.getCell(0).setCellStyle(style);
row.getCell(1).setCellStyle(style);
row.getCell(2).setCellStyle(style);
row.getCell(3).setCellStyle(style);
row.getCell(4).setCellStyle(style);
row.getCell(5).setCellStyle(style);
row.getCell(6).setCellStyle(style);
row.getCell(7).setCellStyle(style);
row.getCell(8).setCellStyle(style);
//设置列宽
sheet.setColumnWidth(0, 2000);
sheet.setColumnWidth(1, 2500);
sheet.setColumnWidth(2, 5000);
sheet.setColumnWidth(3, 6000);
sheet.setColumnWidth(4, 3000);
sheet.setColumnWidth(5, 5000);
sheet.setColumnWidth(6, 5000);
sheet.setColumnWidth(7, 3000);
sheet.setColumnWidth(8, 5000);
//格式化日期 pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
// 第五步,写入实体数据
for (int i = 0; i < dataList.size(); i++) {
row = sheet.createRow(i + 1);
// 第六步,创建单元格,并设置值
row.createCell(0).setCellValue(dataList.get(i).getEmployeeId());
row.createCell(1).setCellValue(dataList.get(i).getEmployeeName());
row.createCell(2).setCellValue(dataList.get(i).getName());//部门
row.createCell(3).setCellValue(dataList.get(i).getSerial());
//卡类型
String key = dataList.get(i).getCardType() == null ? "" : dataList.get(i).getCardType().toString();
String cardDescribe;
if (key != "") {
cardDescribe = constantTableCache.getConstantValue(ConstantTableCache.ConstantTableName.CARD_TYPE, Integer.parseInt(key));
} else cardDescribe = "";
row.createCell(4).setCellValue(cardDescribe);
row.createCell(5).setCellValue(dataList.get(i).getAreaName());
row.createCell(6).setCellValue(dataList.get(i).getDoorName());
String doorStatusKey = dataList.get(i).getDoorStatus() == null ? "" : dataList.get(i).getDoorStatus().toString();
String doorStatusDescribe;
if (doorStatusKey != "") {
doorStatusDescribe = constantTableCache.getConstantValue(ConstantTableCache.ConstantTableName.DOOR_STATUS_TYPE, Integer.parseInt(doorStatusKey));
} else doorStatusDescribe = "";
row.createCell(7).setCellValue(doorStatusDescribe);
row.createCell(8).setCellValue(format.format(dataList.get(i).getEventDate()));
}
// 第七步,将文件存到指定位置
try {
String savePath = "E:/xxx.xls";
FileOutputStream fout = new FileOutputStream(savePath);
workbook.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
POI生成excel文件
最新推荐文章于 2024-05-07 15:35:31 发布