SpringBoot整合EasyExcel

    本文演示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: 8989

三、配置类
①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。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值