java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot excel表格导出

springboot实现excel表格导出几种常见方法

作者:就叫飞六吧

在日常的开发中避免不了操作Excel,下面这篇文章主要给大家介绍了关于springboot实现excel表格导出的几种常见方法,文中通过代码介绍的非常详细,需要的朋友可以参考下

简介

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

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

一、Apache POI

<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

<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

<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;
    }
  }
}

总结 

到此这篇关于springboot实现excel表格导出几种常见方法的文章就介绍到这了,更多相关springboot excel表格导出内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文