// 查询所有数据装在list里
List<DeathRecordView> all = deathRecordViewDAO.findAll();
if (all.isEmpty()) {
throw new ParamException("没有数据可以导出");
}
// 定义excel文件名
String fileName = "死亡记录列表";
Workbook workbook = ExcelUtils.createWorkBook(fileName);
try {
OutputStream os = this.getOutputStream(response, fileName);
Sheet sheet = workbook.getSheetAt(0);
//默认行宽列高
sheet.setDefaultColumnWidth(11);
sheet.setDefaultRowHeightInPoints(15);
//定义表头
String[] header = new String[]{
"防疫任务编号", "养殖户名称", "户主姓名", "联系电话", "户主身份证", "一卡通号码", "防疫人员", "注射时间", "死亡数量", "记录时间"
};
//表头对应的对象字段
List<String> headerField = Lists.newArrayList(
"taskNum", "breedName", "userName",
"telphone", "cardNum", "icCard", "epUserName", "requiredTime", "deathCount", "recordTime"
);
CellStyle headerStyle = this.getHeaderStyle(workbook);
Row headerRow = sheet.createRow(0);
headerRow.setHeightInPoints(26);
//设置表头
for (int i = 0; i < header.length; i++) {
Cell headerCell = headerRow.createCell(i);
headerCell.setCellStyle(headerStyle);
headerCell.setCellValue(header[i]);
}
try {
CellStyle dataStyle = this.getDataStyle(workbook);
for (int i = 0; i < all.size(); i++) {
Row dataRow = sheet.createRow(i + 1);
DeathRecordView deathRecordView = all.get(i);
for (int j = 0; j < headerField.size(); j++) {
Cell dataCell = dataRow.createCell(j);
dataCell.setCellStyle(dataStyle);
Field field = deathRecordView.getClass().getDeclaredField(headerField.get(j));
field.setAccessible(true);
dataCell.setCellValue(field.get(deathRecordView) == null ? "" : field.get(deathRecordView).toString());
if ("requiredTime".equals(headerField.get(j))) {
// 时间进行转换
LocalDate o = (LocalDate) field.get(deathRecordView);
if (o != null) {
dataCell.setCellValue(field.get(deathRecordView) == null ? "" : o.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
}
if ("recordTime".equals(headerField.get(j))) {
// 时间进行转换
LocalDateTime o = (LocalDateTime) field.get(deathRecordView);
if (o != null) {
dataCell.setCellValue(field.get(deathRecordView) == null ? "" : o.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
}
}
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new WarnException("导出失败,读取字段时出错!");
}
workbook.write(os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
throw new WarnException("导出任务列表失败:" + e.getMessage());
}
public static Workbook createWorkBook(String sheetName) {
// 创建excel工作簿
Workbook wb = new HSSFWorkbook();
// 创建第一个sheet(页),并命名
wb.createSheet(sheetName);
return wb;
}
## 得到outputStream对象
private OutputStream getOutputStream(HttpServletResponse response, String fileName) {
OutputStream os = null;
try {
response.setHeader("content-Type", "application/vnd.ms-excel");
os = new BufferedOutputStream(response.getOutputStream());
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
} catch (IOException e) {
e.printStackTrace();
}
return os;
}
设置表头样式
private CellStyle getHeaderStyle(Workbook workbook) {
Font font = this.getHeaderFont(workbook);
//表头单元格样式
CellStyle style = workbook.createCellStyle();
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setAlignment(HorizontalAlignment.CENTER);
style.setWrapText(true);
style.setFont(font);
return style;
}
设置数据区样式
private CellStyle getDataStyle(Workbook workbook) {
Font font = this.getDataFont(workbook);
//表头单元格样式
CellStyle style = workbook.createCellStyle();
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setAlignment(HorizontalAlignment.LEFT);
style.setFont(font);
return style;
}
java导出excel完成