easypoi数据库导出excel文件

背景: 使用easypoi 导出数据库数据,并以excel存放。
easypoi文档:

http://easypoi.mydoc.io/#text_217704

1、导入依赖

 <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>

2、编写实体类:
注:必需有构造函数,如果懒得手动添加课添加注解@AllArgsConstructor //全参构造函数、@NoArgsConstructor //无参构造函数。
在实体类如果没有添加@TableName("")注解,程序会自动到数据库中找excel_Entity表。

package io.renren.modules.sys.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import java.util.Date;

//@TableName("")
public class excelEntity {
    @Excel(name = "姓名", orderNum = "0")
    private String name;

    @Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "1")
    private String sex;

    @Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "2")
    private Date birthday;
    
    public excelEntity(String name, String sex, Date birthday) {
        this.name = name;
        this.sex = sex;
        this.birthday = birthday;
        //this.companyLogo = companyLogo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

3、编写service

import com.baomidou.mybatisplus.extension.service.IService;
import io.renren.modules.sys.entity.excelEntity;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Service
public interface ExcelImExportService extends IService<excelEntity> {
  String exexport(HttpServletResponse response) throws IOException;
  }

4、编写实现类impl


@Service
public class ExcelImExportServiceImpl  extends ServiceImpl<excelDao, excelEntity>  implements ExcelImExportService {


    //导出
    @Override
    public String exexport(HttpServletResponse response) throws IOException {


    //获取数据库信息
    //若果你是用的是renren 框架,在这里存在疑惑可参考另一篇博客:
    //https://editor.csdn.net/md/?articleId=106044901
     List<excelEntity> list1 = this.list(new QueryWrapper<excelEntity>());

        //构造数据
//        List <excelEntity> list = new ArrayList<>();
//        excelEntity ex1 = new excelEntity("路飞","1",  new Date());
//        excelEntity ex2 = new excelEntity("娜美","2",  new Date());
//        list.add(ex1);
//        list.add(ex2);

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生","学生"),
                excelEntity.class, list1);
        try {
      // HttpServletResponse response1 = null;

            File savefile = new File("E:/excel/");
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream("E:/excel/教师课程学生导出测试1.xls");
            workbook.write(fos);
            fos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    }

4、编写测试类


@SpringBootTest
@RunWith(SpringRunner.class)
public class ExcelImExportTest {
    @Autowired
    private ExcelImExportService excelImExportService;

//简单导出
    @Test
    public void exexport() throws IOException {
        HttpServletResponse response=null;
        excelImExportService.exexport(response);
    }

5、数据库信息与导出结果
5.1、数据库信息
在这里插入图片描述
5.2、导出结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值