EasyExcel通用导出功能

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

一、准备工作

1.导入依赖

		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

二、代码示例如下

1.目录结构

在这里插入图片描述

2.详细代码

TestExportController

package com.example.easyexcel.controller;

import com.example.easyexcel.entity.ExportStudent;
import com.example.easyexcel.service.ExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MUKE
 */
@RestController
public class TestExportController {

    @Autowired
    private ExportService exportService;

    @PostMapping("/preExportJsdList")
    public void preExportJsdList(HttpServletResponse response) {
        List<ExportStudent> list = new ArrayList<>();
        ExportStudent exportStudent = new ExportStudent();
        exportStudent.setName("MUKE");
        exportStudent.setAge(22);
        list.add(exportStudent);
        exportService.exportDataList(response, list, ExportStudent.class);
    }
}

ExportStudent(导出模板类)

package com.example.easyexcel.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;

/**
 * @author A80222
 */
@Data
public class ExportStudent {

    @ExcelProperty( value = "姓名")
    @ColumnWidth(5)
    private String name;

    @ExcelProperty( value = "年龄")
    @ColumnWidth(5)
    private Integer age;
}


ExportServiceImpl(导出实现类)

package com.example.easyexcel.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.example.easyexcel.entity.ExportStudent;
import com.example.easyexcel.service.ExportService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service
public class ExportServiceImpl implements ExportService {
    @Override
    public <E> void exportDataList(HttpServletResponse response, List<E> dataList, Class<E> clazz) {
        try {
            response.setHeader("Content-disposition", "attachment;filename=" + new String("学生信息表".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream(), clazz);
            writerBuilder
                    .autoCloseStream(true)
                    .excelType(ExcelTypeEnum.XLSX)
                    .needHead(true)
                    .sheet().sheetName("安装结算单数据")
                    .doWrite(dataList);
        } catch (Exception e) {

        }
    }
}

ExportService(导出接口)

package com.example.easyexcel.service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

public interface ExportService {
	//dataList 导出的数据, clazz导出数据的模板类
    <E> void exportDataList(HttpServletResponse response, List<E> dataList, Class<E> clazz);
}

3.效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值