当前位置: 代码网 > it编程>编程语言>Java > Java使用FastExcel高效读取和写入Excel

Java使用FastExcel高效读取和写入Excel

2025年11月05日 Java 我要评论
01 引言fastexcel大家可能不熟悉,但是easyexcel应该都使用过吧?fastexcel是easyexcel版本的延续,自从阿里巴巴官方停止维护easyexcel,easyexcel的作者

01 引言

fastexcel大家可能不熟悉,但是easyexcel应该都使用过吧?

fastexceleasyexcel版本的延续,自从阿里巴巴官方停止维护easyexceleasyexcel的作者继续维护起来,并持续改进优化,并更名为fastexcel

02 简介

fastexcel 是一个 java 库,旨在高效地读取和写入 excel 文件。它最初是 easyexcel 的分叉版本,旨在提供增强的性能、持续维护和新功能,同时保持与原始 easyexcel api 的兼容性。这使其成为在 java 应用程序中处理 excel 数据的强大的用户友好的工具。

fastexcel 通过优化内存使用来优先考虑高性能,特别是在处理大型数据集时。它通过 sax 解析机制实现这一点,以流式方式处理基于 xml 的 excel 文件(xlsx),而无需一次性将整个文件加载到内存中。

官网地址:https://readmex.com/fast-excel/fastexcel/page-151366ada-2ea3-4451-aa3d-98ac90ce4f6e

github地址:https://github.com/fast-excel/fastexcel

主要特性:

  • 高性能读写:fastexcel 专注于性能优化,能够高效处理大规模的 excel 数据。相比一些传统的 excel 处理库,它能显著降低内存占用。
  • 简单易用:该库提供了简洁直观的 api,使得开发者可以轻松集成到项目中,无论是简单的 excel 操作还是复杂的数据处理都能快速上手。
  • 流式操作:fastexcel 支持流式读取,将一次性加载大量数据的问题降到最低。这种设计方式在处理数十万甚至上百万行的数据时尤为重要。

03 功能概要

业务项目中,我们经常需要读写excel,如excel的导入,导出。

3.1 maven

<dependency>
    <groupid>cn.idev.excel</groupid>
    <artifactid>fastexcel</artifactid>
    <version>1.2.0</version> <!-- or the latest version -->
</dependency>

3.2 模型类

用来接收excel数据的实体

@data
public class book {

    private string bookname;
    private string author;
    private bigdecimal price;
    private date saledate;
}

3.3 读取excel文件

api

案例:

@test
void test01() throws filenotfoundexception {
    file file = resourceutils.getfile("classpath:file/demo.xls");
    list<book> booklist = new arraylist<>();
    fastexcel.read(file, book.class, new readlistener<book>() {
        @override
        public void invoke(book book, analysiscontext analysiscontext) {
            system.out.println("解析数据:" + json.tojsonstring(book));
            booklist.add(book);
        }

        @override
        public void doafterallanalysed(analysiscontext analysiscontext) {
            system.out.println("解析完所有数据....");
        }
    }).sheet().doread();

    system.out.println("booklist:" + json.tojsonstring(booklist));
}

执行结果:

代码解析:

read(file file, class head, readlistener readlistener)

  • fastexcel拥有静态方法read()
  • file:需要读取的文件
  • head:需要将文件解析的对象,按照字段的顺序和excel的澍勋解析,也可以通过@excelproperty指定索引位置
  • readlistener:读取每一条数据的监听器,处理数据也是这里处理的

fastexcel.read(...).sheet().doread()

  • sheet():默认第一个sheet,也可以传递参数指定
  • doread():开始同步读取数据,也可以异步读取数据doreadsync()

3.4 写入excel文件

写入文件之前,我们需要定义模板类的名称,需要使用到@excelproperty

@data
public class book {

    @excelproperty(value = "书名")
    private string bookname;
    @excelproperty(value = "作者")
    private string author;
    @excelproperty(value = "价格")
    @numberformat("#,###.00")
    private bigdecimal price;
    @excelproperty(value = "上架时间")
    @datetimeformat("yyyy-mm-dd")
    private date saledate;
}

api:

案例:

@test
void test02() {
    list<book> booklist = new arraylist<>();
    for (int i = 0; i < 5; i++) {
        book item = new book();
        item.setbookname("java" + i);
        item.setauthor("张三" + i);
        item.setprice(new bigdecimal("2002.35"));
        item.setsaledate(new date());
        booklist.add(item);
    }
    fastexcel.write("writedemo.xls", book.class).sheet().sheetname("test").dowrite(booklist);
}

执行结果:

代码解析:

write(string pathname, class head)

  • fastexcel拥有静态方法write()
  • pathname:文件要生成的位置
  • head:模板类,这个生成excel的关键

fastexcel.write(...).sheet().sheetname("test").dowrite(booklist)

  • sheet():写入sheet的位置,默认第一个
  • sheetname("test"):自定义sheet的名称
  • dowrite():要写入的数据源

04 小结

fastexcel替我们省去了使用poi的繁琐,使用起来只关系需要处理的数据即可,非常方便。easyexcelfastexcel在使用上基本没有什么区别,因为同宗同源。

easyexcel已经停止维护了,你会去迁移到fastexcel上么?作者给出了几个迁移优势:

以上就是java使用fastexcel高效读取和写入excel的详细内容,更多关于java fastexcel读取和写入excel的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com