POI之easypoi操作(二)

2018-06-06 11:58:16 浏览数 (2)

pom

代码语言:javascript复制
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-web</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-annotation</artifactId>
        <version>3.1.0</version>
    </dependency>   
  • 简单应用
代码语言:javascript复制
@Data
public class People implements Serializable{
    @Excel(name = "学生Id")
    private Integer id ;
    @Excel(name = "姓名")
    private String name ;
    @Excel(name = "学生地址")
    private String site ;
    @Excel(name = "保存日期")
    private LocalDate localDate ;
}

 People people = new People() ;
        people.setId(1);
        people.setName("abc");
        people.setSite("江西南昌");
        people.setLocalDate(LocalDate.now());
        List<People> list = new ArrayList() ;
        list.add(people) ;
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("物联一班","测试"),People.class, list) ;
        String fileName = "D:\test.xls";
        FileOutputStream out = new FileOutputStream(fileName) ;
        workbook.write(out);

连接数据库 导入使用

  1. Dao 使用JPA
代码语言:javascript复制
@Repository
public interface PeopleDao extends JpaRepository<People,Serializable> {
}
  1. Entity 的定义 ps:使用了lombok,不了解的可以去看笔者之前的文章
代码语言:javascript复制
@Data
@Entity
public class People implements Serializable{

    @Id@GeneratedValue
    @Excel(name = "学生Id")
    private Integer id ;
    @Excel(name = "姓名")
    private String name ;
    @Excel(name = "学生地址")
    private String site ;
    @Excel(name = "保存日期")
    private String date ;
}
  1. 测试
代码语言:javascript复制
@Test
    public void test2() throws IOException {

        List<People> list = peopleDao.findAll() ;
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("物联一班","测试"),People.class, list) ;
        String fileName = "D:\test.xls";
        FileOutputStream out = new FileOutputStream(fileName) ;
        workbook.write(out);
        out.close();
        System.out.println("写入成功");
    }

运行结果

读取

代码语言:javascript复制
@Test
    public void read1(){
        String fileName = "D:\test.xls";
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        long start = new Date().getTime();
        List<People> list = ExcelImportUtil.importExcel(
                new File(fileName),
                People.class, params);

        System.out.println("-----------------------------n所耗时间:");
        System.out.println(new Date().getTime() - start);
        list.forEach(s-> System.out.println(s));
    }

这个项目托管在码云,欢迎去star给与它支持https://gitee.com/lemur/easypoi

关于API的使用笔者本来想介绍下,但是想了想,源码介绍的非常清楚了,而且还是中文注释、中文注释、中文注释 来张图让你了解下

好了,关于excel的使用就介绍到这里了,如有错误,敬请指正

0 人点赞