《springboot中实现excel表格导出》
简介
在Spring Boot中,实现Excel表格导出的方式有很多种,以下是几种常见的方法:
- 使用Apache POI:Apache POI是一个开源的Java API,用于处理Microsoft Office文档格式,包括Excel电子表格。在Spring Boot中,可以使用Apache POI创建Excel文档,并将其写入HTTP响应中,以实现Excel表格的导出。
- 使用EasyPOI:EasyPOI是一个开源的Java API,用于处理Excel电子表格。它基于Apache POI和Jxls开发,提供了更加简单易用的API,可以帮助我们快速实现Excel表格的导出。
- 使用Jxls:Jxls是一个用于生成Excel报表的Java库。在Spring Boot中,可以使用Jxls创建Excel文档,并将其写入HTTP响应中,以实现Excel表格的导出。
- 使用第三方库:还有其他一些第三方的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>
@RestControllerpublicclassExcelController{@GetMapping("/export")publicvoidexportExcel(HttpServletResponse response)throwsException{// 创建Excel文档XSSFWorkbook workbook =newXSSFWorkbook();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();}// 模拟获取用户数据privateList<User>getUserList(){List<User> users =newArrayList<>();
users.add(newUser("张三",25,"男"));
users.add(newUser("李四",30,"女"));
users.add(newUser("王五",28,"男"));return users;}// 用户实体类privatestaticclassUser{privateString name;privateint age;privateString gender;publicUser(String name,int age,String gender){this.name = name;this.age = age;this.gender = gender;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicintgetAge(){return age;}publicvoidsetAge(int age){this.age = age;}publicStringgetGender(){return gender;}publicvoidsetGender(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>
@RestControllerpublicclassExcelController{@GetMapping("/export")publicvoidexportExcel(HttpServletResponse response)throwsException{// 创建Excel文档Workbook workbook =ExcelExportUtil.exportExcel(newExportParams("用户列表","用户信息"),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();}// 模拟获取用户数据privateList<User>getUserList(){List<User> users =newArrayList<>();
users.add(newUser("张三",25,"男"));
users.add(newUser("李四",30,"女"));
users.add(newUser("王五",28,"男"));return users;}// 用户实体类privatestaticclassUser{@Excel(name ="姓名", orderNum ="0")privateString name;@Excel(name ="年龄", orderNum ="1")privateint age;@Excel(name ="性别", orderNum ="2")privateString gender;publicUser(String name,int age,String gender){this.name = name;this.age = age;this.gender = gender;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicintgetAge(){return age;}publicvoidsetAge(int age){this.age = age;}publicStringgetGender(){return gender;}publicvoidsetGender(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>
@RestControllerpublicclassExcelController{@GetMapping("/export")publicvoidexportExcel(HttpServletResponse response)throwsException{// 加载Excel模板InputStream inputStream =getClass().getResourceAsStream("/templates/user_template.xlsx");Workbook workbook =WorkbookFactory.create(inputStream);// 填充数据List<User> users =getUserList();Map<String,Object> model =newHashMap<>();
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();}// 模拟获取用户数据privateList<User>getUserList(){List<User> users =newArrayList<>();
users.add(newUser("张三",25,"男"));
users.add(newUser("李四",30,"女"));
users.add(newUser("王五",28,"男"));return users;}// 用户实体类privatestaticclassUser{privateString name;privateint age;privateString gender;publicUser(String name,int age,String gender){this.name = name;this.age = age;this.gender = gender;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicintgetAge(){return age;}publicvoidsetAge(int age){this.age = age;}publicStringgetGender(){return gender;}publicvoidsetGender(String gender){this.gender = gender;}}}
版权归原作者 就叫飞六吧 所有, 如有侵权,请联系我们删除。