Create Excel File In Spring Boot

A Way To Create Excel File In Spring Boot

Posted by MichaelChen on 2020-11-26
Estimated Reading Time 2 Minutes
Words 355 In Total
Viewed Times

A Way To Create Excel File In Spring Boot

Maven

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency> <!-- 操作File好用 可选 -->
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

Address

Windows: Download & Install Apache Poi -> Apache Poi

How To Use

  • useHSSFWorkbook to create excel file

  • use HSSFSheet to create a sheet

  • use HSSFRow to create a row

  • use a HSSRow <object>.createCell().setCellValue() to set value

    Code

    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
    32
    HSSFWorkbook 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
    11
    try {
    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 !