本文演示SpringBoot整合EasyExcel,并导出数据。
一、项目搭建
新建一个SpringBoot项目,引入依赖:
<properties>
<java.version>1.8</java.version>
<easyexcel.version>2.2.6</easyexcel.version>
<lombok.version>1.16.10</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--swagger配置-->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- lombok配置 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- EasyExcel配置 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
</dependencies>
二、配置文件
配置项目端口号:
server:
port: 8899
三、配置类
①Swagger配置类
@Configuration
@EnableSwagger2
public class Swagger2Configure {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any()).build();
}
/**
定义展示的信息,例如标题、描述、版本等
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("api文档")
.version("1.0").build();
}
}
②EasyExcel工具类
public class ExcelUtils {
public static void download(HttpServletResponse response, String fileName,String sheetName,Class cls, List data) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fname = URLEncoder.encode(fileName, "utf-8");
response.setHeader("Content-disposition","attachment;filename=" + fname + ExcelTypeEnum.XLSX.getValue());
LongestMatchColumnWidthStyleStrategy longestMatchColumnWidthStyleStrategy = new LongestMatchColumnWidthStyleStrategy();
EasyExcel.write(response.getOutputStream(), cls)
.sheet(sheetName)
.registerWriteHandler(longestMatchColumnWidthStyleStrategy)
.doWrite(data);
response.flushBuffer();
}
}
③数据实体类
@Data
public class Student {
@ExcelProperty(value = {"编号"}, index = 0)
private Integer id;
@ExcelProperty(value = {"姓名"}, index = 1)
private String name;
@ExcelProperty(value = {"年龄"}, index = 2)
private Integer age;
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
四、编码实现
①编写Controller类及测试请求方法:
@Api(tags = "demo测试")
@RestController
@RequestMapping("/")
public class TestController {
@PostMapping("export")
@ApiOperation("导出学生数据")
public void export(HttpServletResponse response) {
List<Student> data = buildData();
try {
ExcelUtils.download(response, "StudentInfo", "Test", Student.class, data);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static List<Student> buildData(){
List<Student> data = new ArrayList<>();
for (int i = 1; i < 51; i++) {
data.add(new Student(i,"学生"+i,i));
}
return data;
}
}
②启动类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
③启动项目并访问http://localhost:8989/swagger-ui.html:
下载文件后验证:
测试验证OK。