Skip to content

以注解的方式完成excel的导入导出,一行代码即可,基于poi

Notifications You must be signed in to change notification settings

limiaogithub/yt_excel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

yt_excel

介绍

相信大多数公司都基于poi封装了生成和读取excel的工具,yt_excel就是其中的一个实现,但是他比较简单,基于注解,支持表达式转义等特点,值得一用。
您可以下载https://github.com/limiaogithub/yt_mybatis_example 直接运行示例代码。

特性

1.基于apache poi
2.注解的方式进行配置
3.支持多sheet
4.使用简单

准备

1.idea开发工具
2.大约10分钟

集成

1.引入maven依赖
<dependency>
    <groupId>com.github.limiaogithub</groupId>
    <artifactId>yt_excel</artifactId>
    <version>1.0.0</version>
</dependency>

2.编写你的Pojo类,这里@ImportExcel指导入的字段,@ExportExcel指导出的字段,属性一看就懂了。

public class TestBean {

    private int id;

    @ExportExcel(title = "姓名", order = 1)
    @ImportExcel(order = 1)
    private String name;

    @ExportExcel(title = "密码", order = 2)
    @ImportExcel(order = 2)
    private int password;

    @ExportExcel(title = "日期", order = 3)
    @ImportExcel(order = 3)
    private Date date;

    @ExportExcel(title = "BigDecimal", order = 4)
    @ImportExcel(order = 4)
    private BigDecimal big;

    public TestBean() {
    }

    public TestBean(String name, int password, Date date, BigDecimal big) {
        this.name = name;
        this.password = password;
        this.date = date;
        this.big = big;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getPassword() {
        return password;
    }

    public void setPassword(int password) {
        this.password = password;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public BigDecimal getBig() {
        return big;
    }

    public void setBig(BigDecimal big) {
        this.big = big;
    }

    @Override
    public String toString() {
        return "TestBean{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password=" + password +
                ", date=" + date +
                ", big=" + big +
                '}';
    }
}

3.导出

@ApiOperation(value = "export")
@RequestMapping(value = "/export", method = RequestMethod.GET)
public void export() throws Exception {
    ServletOutputStream out = response.getOutputStream();
    String fileName = EncodeUtils.toUtf8String(request.getHeader("User-Agent"), "导出文件.xls");
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    response.setContentType("application/octet-stream; charset=utf-8");

    ExcelUtils.createExcel(out, TestBean.class, getTestList());

    out.flush();
}

private static List getTestList() {
    List list = new ArrayList<>();
    TestBean testBean1 = new TestBean("name1", 123, new Date(), new BigDecimal(1111));
    list.add(testBean1);
    TestBean testBean2 = new TestBean("name2", 456, new Date(), new BigDecimal(2222));
    list.add(testBean2);
    return list;
}

4.导入

@ApiOperation(value = "import")
@RequestMapping(value = "/import", method = RequestMethod.GET)
public void import1(MultipartFile file) throws Exception {
    File file1=new File("D:\\导出文件.xls");
    
    List list = ExcelUtils.readExcel(file1, TestBean.class, new ExcelConfig().setStartRow(2));
    
    System.out.println(list.size());
    for (TestBean testBean : list) {
        System.out.println(testBean.toString());
    }
}

About

以注解的方式完成excel的导入导出,一行代码即可,基于poi

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages