Bootstrap

《springboot中实现excel表格导出》

《springboot中实现excel表格导出》

简介

在Spring Boot中,实现Excel表格导出的方式有很多种,以下是几种常见的方法:

  1. 使用Apache POI:Apache POI是一个开源的Java API,用于处理Microsoft Office文档格式,包括Excel电子表格。在Spring Boot中,可以使用Apache POI创建Excel文档,并将其写入HTTP响应中,以实现Excel表格的导出。
  2. 使用EasyPOI:EasyPOI是一个开源的Java API,用于处理Excel电子表格。它基于Apache POI和Jxls开发,提供了更加简单易用的API,可以帮助我们快速实现Excel表格的导出。
  3. 使用Jxls:Jxls是一个用于生成Excel报表的Java库。在Spring Boot中,可以使用Jxls创建Excel文档,并将其写入HTTP响应中,以实现Excel表格的导出。
  4. 使用第三方库:还有其他一些第三方的Java库可以用于生成Excel电子表格,例如Aspose.Cells、JExcelApi等,它们也可以在Spring Boot中使用,实现Excel表格的导出。

需要注意的是,无论使用哪种方法,都需要将Excel文档写入HTTP响应中,并设置正确的Content-Type和Content-Disposition头信息,以确保浏览器能够正确地识别Excel文档并下载它。

一、Apache POI

  • maven依赖坐标
<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;
    }
  }
}

二、Easy POI

  • maven依赖坐标
<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;
    }
  }
}

三、Jxls

  • maven依赖坐标
<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;
    }
  }
}

;