"Realizing excel table export in springboot"

"Realizing excel table export in springboot"

Introduction

In Spring Boot, there are many ways to export Excel tables. The following are some common methods:

  1. Using Apache POI: Apache POI is an open source Java API for working with Microsoft Office document formats, including Excel spreadsheets. In Spring Boot, you can use Apache POI to create Excel documents and write them into HTTP responses to export Excel tables.
  2. Using EasyPOI: EasyPOI is an open source Java API for working with Excel spreadsheets. It is developed based on Apache POI and Jxls, and provides a simpler and easier-to-use API that can help us quickly export Excel tables.
  3. Using Jxls: Jxls is a Java library for generating Excel reports. In Spring Boot, you can use Jxls to create Excel documents and write them into HTTP responses to export Excel tables.
  4. Use third-party libraries: There are other third-party Java libraries that can be used to generate Excel spreadsheets, such as Aspose.Cells, JExcelApi, etc., which can also be used in Spring Boot to export Excel spreadsheets.

It should be noted that no matter which method you use, you need to write the Excel document into the HTTP response and set the correct Content-Type and Content-Disposition header information to ensure that the browser can correctly identify the Excel document and download it.

1. Apache POI

  • Maven depends on coordinates
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>4.1.2</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>4.1.2</version>
</dependency>

@RestController
public class ExcelController {
    
    

  @GetMapping("/export")
  public void exportExcel(HttpServletResponse response) throws Exception {
    
    
    // 创建Excel文档
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Sheet1");

    // 创建表头
    XSSFRow header = sheet.createRow(0);
    header.createCell(0).setCellValue("姓名");
    header.createCell(1).setCellValue("年龄");
    header.createCell(2).setCellValue("性别");

    // 填充数据
    List<User> users = getUserList();
    int rowIndex = 1;
    for (User user : users) {
    
    
      XSSFRow row = sheet.createRow(rowIndex++);
      row.createCell(0).setCellValue(user.getName());
      row.createCell(1).setCellValue(user.getAge());
      row.createCell(2).setCellValue(user.getGender());
    }

    // 设置响应头信息
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");

    // 将Excel文档写入响应流中
    ServletOutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
  }

  // 模拟获取用户数据
  private List<User> getUserList() {
    
    
    List<User> users = new ArrayList<>();
    users.add(new User("张三", 25, "男"));
    users.add(new User("李四", 30, "女"));
    users.add(new User("王五", 28, "男"));
    return users;
  }

  // 用户实体类
  private static class User {
    
    
    private String name;
    private int age;
    private String gender;

    public User(String name, int age, String gender) {
    
    
      this.name = name;
      this.age = age;
      this.gender = gender;
    }

    public String getName() {
    
    
      return name;
    }

    public void setName(String name) {
    
    
      this.name = name;
    }

    public int getAge() {
    
    
      return age;
    }

    public void setAge(int age) {
    
    
      this.age = age;
    }

    public String getGender() {
    
    
      return gender;
    }

    public void setGender(String gender) {
    
    
      this.gender = gender;
    }
  }
}

2. Easy POI

  • Maven depends on coordinates
<dependency>
  <groupId>cn.afterturn</groupId>
  <artifactId>easypoi-base</artifactId>
  <version>4.2.0</version>
</dependency>
<dependency>
  <groupId>cn.afterturn</groupId>
  <artifactId>easypoi-web</artifactId>
  <version>4.2.0</version>
</dependency>

@RestController
public class ExcelController {
    
    

  @GetMapping("/export")
  public void exportExcel(HttpServletResponse response) throws Exception {
    
    
    // 创建Excel文档
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户列表", "用户信息"), User.class, getUserList());

    // 设置响应头信息
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");

    // 将Excel文档写入响应流中
    ServletOutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
  }

  // 模拟获取用户数据
  private List<User> getUserList() {
    
    
    List<User> users = new ArrayList<>();
    users.add(new User("张三", 25, "男"));
    users.add(new User("李四", 30, "女"));
    users.add(new User("王五", 28, "男"));
    return users;
  }

  // 用户实体类
  private static class User {
    
    
    @Excel(name = "姓名", orderNum = "0")
    private String name;
    @Excel(name = "年龄", orderNum = "1")
    private int age;
    @Excel(name = "性别", orderNum = "2")
    private String gender;

    public User(String name, int age, String gender) {
    
    
      this.name = name;
      this.age = age;
      this.gender = gender;
    }

    public String getName() {
    
    
      return name;
    }

    public void setName(String name) {
    
    
      this.name = name;
    }

    public int getAge() {
    
    
      return age;
    }

    public void setAge(int age) {
    
    
      this.age = age;
    }

    public String getGender() {
    
    
      return gender;
    }

    public void setGender(String gender) {
    
    
      this.gender = gender;
    }
  }
}

3. Jxls

  • Maven depends on coordinates
<dependency>
  <groupId>org.jxls</groupId>
  <artifactId>jxls</artifactId>
  <version>2.14.0</version>
</dependency>
<dependency>
  <groupId>org.jxls</groupId>
  <artifactId>jxls-poi</artifactId>
  <version>2.14.0</version>
</dependency>

@RestController
public class ExcelController {
    
    

  @GetMapping("/export")
  public void exportExcel(HttpServletResponse response) throws Exception {
    
    
    // 加载Excel模板
    InputStream inputStream = getClass().getResourceAsStream("/templates/user_template.xlsx");
    Workbook workbook = WorkbookFactory.create(inputStream);

    // 填充数据
    List<User> users = getUserList();
    Map<String, Object> model = new HashMap<>();
    model.put("users", users);
    JxlsHelper.getInstance().processTemplate(model, workbook.getSheetAt(0));

    // 设置响应头信息
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");

    // 将Excel文档写入响应流中
    ServletOutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
  }

  // 模拟获取用户数据
  private List<User> getUserList() {
    
    
    List<User> users = new ArrayList<>();
    users.add(new User("张三", 25, "男"));
    users.add(new User("李四", 30, "女"));
    users.add(new User("王五", 28, "男"));
    return users;
  }

  // 用户实体类
  private static class User {
    
    
    private String name;
    private int age;
    private String gender;

    public User(String name, int age, String gender) {
    
    
      this.name = name;
      this.age = age;
      this.gender = gender;
    }

    public String getName() {
    
    
      return name;
    }

    public void setName(String name) {
    
    
      this.name = name;
    }

    public int getAge() {
    
    
      return age;
    }

    public void setAge(int age) {
    
    
      this.age = age;
    }

    public String getGender() {
    
    
      return gender;
    }

    public void setGender(String gender) {
    
    
      this.gender = gender;
    }
  }
}

Guess you like

Origin blog.csdn.net/weixin_52236586/article/details/129602624