A Way To Create Excel File In Spring Boot
Maven
1 | <dependency> |
Address
Windows: Download & Install Apache Poi -> Apache Poi
How To Use
use
HSSFWorkbook
to create excel fileuse
HSSFSheet
to create a sheetuse
HSSFRow
to create a rowuse a
HSSRow <object>.createCell().setCellValue()
to set valueCode
Create Excel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("医生排班表");
HSSFRow head = sheet.createRow(0);
head.createCell(0).setCellValue("序号");
head.createCell(1).setCellValue("所属大类");
head.createCell(2).setCellValue("门诊科室");
head.createCell(3).setCellValue("门诊类型");
head.createCell(4).setCellValue("时间");
head.createCell(5).setCellValue("医生姓名");
head.createCell(6).setCellValue("门诊地点");
head.createCell(7).setCellValue("适用时间");
List<SectionPlanDto> sectionPlanDtos = getPlanDetatilsByFlag(hospital_id,flag);
if (sectionPlanDtos != null && sectionPlanDtos.size() > 0){
for (SectionPlanDto sectionPlanDto : sectionPlanDtos) {
for (Plan plan : sectionPlanDto.getPlans()) {
for (DoctorPlan doctor : plan.getDoctors()) {
HSSFRow body = sheet.createRow(sheet.getLastRowNum()+1);
body.createCell(0).setCellValue(sheet.getLastRowNum());
body.createCell(1).setCellValue(sectionPlanDto.getParentName());
body.createCell(2).setCellValue(plan.getSection());
body.createCell(3).setCellValue(plan.getName());
String time = plan.getFlag() == 1 ? "上午" : "下午";
body.createCell(4).setCellValue(time);
body.createCell(5).setCellValue(doctor.getName());
body.createCell(6).setCellValue(plan.getPlace());
String suitTime = plan.getTime()==null ? "除特殊排号时间外" : simpleDateFormat.format(plan.getTime());
body.createCell(7).setCellValue(suitTime);
}
}
}
}Output
1
2
3
4
5
6
7
8
9
10
11try {
FileOutputStream fos = new FileOutputStream(path+"\\fileName.xls");
workbook.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
}
}
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !