当前位置: 代码网 > it编程>编程语言>Java > Java使用EasyExcel实现对excel文件的读写方式

Java使用EasyExcel实现对excel文件的读写方式

2024年05月26日 Java 我要评论
提示:这里可以添加本文要记录的大概内容:在学习java中难免会有对文件的读写操作,像word文档、pdf文件以及excel表。这些读写操作都大差不差,接下来为大家讲解一下java对excel表的读写操

提示:这里可以添加本文要记录的大概内容:

在学习java中难免会有对文件的读写操作,像word文档、pdf文件以及excel表。这些读写操作都大差不差,接下来为大家讲解一下java对excel表的读写操作。

提示:以下是本篇文章正文内容,下面案例可供参考

一、easyexcel是什么?

easyexcel是阿里巴巴开源的一款基于java语言的简单、高效、功能强大的excel读写工具库。

它提供了简单易用的api,可以帮助java开发者在项目中轻松地进行excel文件的读写操作,支持读取和写入excel文件,同时支持常见的excel格式,如.xls和.xlsx。

easyexcel具有以下主要特点:

  • 简单易用:easyexcel提供了简洁清晰的api,使得开发者可以轻松地进行excel文件的读写操作,无需过多的复杂配置。
  • 高效性能:easyexcel采用了基于流的方式进行excel文件的读写,同时使用了缓冲区等技术来提升读写效率,具有较高的性能。
  • 功能强大:easyexcel支持对excel文件进行灵活的读写操作,可以读取excel文件中的数据并转换为java对象,也可以将java对象的数据写入到excel文件中。
  • 跨平台兼容性:easyexcel是基于java语言开发的,可以在各种操作系统上运行,且支持读写各种常见的excel格式,如.xls和.xlsx等。
  • 可扩展性:easyexcel提供了丰富的扩展点和接口,开发者可以根据自己的需求进行定制和扩展,满足不同场景下的需求。

总的来说,easyexcel是一款功能强大且易于使用的java excel处理工具库,被广泛应用于各种java项目中,特别是对excel文件读写操作比较频繁的业务场景。

官方文档地址easyexcel

二、使用步骤

1.引入库

我们创建maven项目,在pom.xml文件中添加依赖:

       <!--easy excel依赖-->
       <dependency>
            <groupid>com.alibaba</groupid>
            <artifactid>easyexcel</artifactid>
            <version>3.3.4</version>
        </dependency>

        <!--lombok依赖  用来生成getter/setter/构造函数的-->
        <dependency>
            <groupid>org.projectlombok</groupid>
            <artifactid>lombok</artifactid>
            <version>1.18.30</version>
        </dependency>

2.创建实体类

代码如下(示例):

@data   //提供getter/setter
@noargsconstructor  //无参构造
@allargsconstructor  //含有全部参数的构造
public class goods {

    @excelignore   //easyexcel注解 表示读写excel表忽略此列
    int id;

    @excelproperty("编码")   //easyexcel注解 表示读写excel表对列名为“编码”进行读写
    string code;

    @excelproperty("名称")
    string name;

    @excelproperty("价格")
    double price;

    @excelproperty("库存")
    int num;

    @excelproperty("重量")
    double weight;

    @excelproperty("cpu")
    string cpu;

    @excelproperty("内存")
    string memory;

    @excelproperty("机身内存")
    string bodymemory;

    @excelignore
    string createtime;

    @excelignore
    string updatetime;

    @excelignore
    string categorycode;
}

3.建立数据库

因为我们在读写操作时需要有一定的数据源才能把读写操作更加明显。

/*
 navicat premium data transfer

 source server         : rpwn
 source server type    : mysql
 source server version : 50735 (5.7.35)
 source host           : localhost:3306
 source schema         : demo

 target server type    : mysql
 target server version : 50735 (5.7.35)
 file encoding         : 65001

 date: 26/03/2024 16:39:43
*/

set names utf8mb4;
set foreign_key_checks = 0;

-- ----------------------------
-- table structure for goods
-- ----------------------------
drop table if exists `goods`;
create table `goods`  (
  `id` int(11) not null auto_increment,
  `code` varchar(10) character set utf8 collate utf8_general_ci null default null,
  `name` varchar(100) character set utf8 collate utf8_general_ci null default null,
  `price` decimal(10, 2) null default null,
  `num` int(11) null default 0,
  `weight` decimal(10, 2) null default null,
  `cpu` varchar(10) character set utf8 collate utf8_general_ci null default null,
  `memory` varchar(10) character set utf8 collate utf8_general_ci null default null,
  `bodymemory` varchar(10) character set utf8 collate utf8_general_ci null default null,
  `createtime` timestamp null default current_timestamp,
  `updatetime` timestamp null default current_timestamp on update current_timestamp,
  `isdel` bit(1) null default b'0',
  `categorycode` varchar(7) character set utf8 collate utf8_general_ci null default null,
  primary key (`id`) using btree
) engine = innodb auto_increment = 297 character set = utf8 collate = utf8_general_ci row_format = dynamic;

-- ----------------------------
-- records of goods
-- ----------------------------
insert into `goods` values (10, '8jxexvkzkp', '11213', 342.25, 632, 891.65, 'jwkmvvsisf', '2tuptoemmy', 'ywp6wobotl', '2024-03-13 15:15:44', '2024-03-15 19:46:52', b'0', '10202');
insert into `goods` values (11, '123', '123', 23.20, 32, 213.40, '21', '@', '43', '2024-03-13 16:09:48', '2024-03-15 19:46:52', b'1', '10203');
insert into `goods` values (12, '123', '123', 23.20, 32, 213.40, '21', '@', '43', '2024-03-13 16:15:57', '2024-03-15 19:46:52', b'0', '10204');
insert into `goods` values (13, '123', '123', 23.20, 32, 213.40, '21', '@', '43', '2024-03-13 17:14:59', '2024-03-15 19:46:52', b'0', '10301');
insert into `goods` values (14, '123', '123', 23.20, 32, 213.40, '21', '@', '43', '2024-03-15 14:53:58', '2024-03-15 19:46:52', b'0', '10302');
insert into `goods` values (15, '123', '123', 23.20, 32, 213.40, '21', '@', '43', '2024-03-15 15:00:08', '2024-03-15 19:46:52', b'0', '10401');
insert into `goods` values (16, '123', '123', 23.20, 32, 213.40, '21', '@', '43', '2024-03-15 15:00:13', '2024-03-15 19:46:52', b'1', '10402');
insert into `goods` values (17, '123', '12', 1110.00, 210, 11.00, '12', '21', '32 b', '2024-03-15 17:55:20', '2024-03-15 19:46:52', b'0', '1010101');
insert into `goods` values (18, 'qq', '112', 1.00, 1, 1.00, '1', '1', '123', '2024-03-15 18:50:12', '2024-03-15 19:46:52', b'0', '1010102');
insert into `goods` values (19, '11213', '无敌暴龙战神', 110.00, 10, 13111.00, '1451', '142', '1423', '2024-03-15 19:09:53', '2024-03-15 19:46:52', b'0', '1010103');
insert into `goods` values (20, 'gcan5xkhvy', 'ambi-raspberry', 91.01, 407, 180.74, 'hkm7krcl6i', 'fhvqdms6sj', 'ilgoivg3bp', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1010201');
insert into `goods` values (21, 'teygdlkvdh', 'vrape elite', 990.81, 625, 411.26, 'gyqj6zvqis', 'kmvzxbpauv', 'khnrt7ksas', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1010202');
insert into `goods` values (22, 'lnwjlxxjnr', 'omni-apcle', 960.91, 564, 515.32, 'lqi00ov0n5', '6duf1gdwfl', 'fe3enbhcm3', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1010203');
insert into `goods` values (23, 'jgp7rnaxim', 'cherry premium', 765.80, 229, 922.03, 'rphjtpk0yq', 'bwkpop0xrf', 'yykttwd8rl', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1010301');
insert into `goods` values (24, 'vuwp9t6xoc', 'strawberry premium', 327.73, 207, 483.71, 'djptxxog3g', '7w9ant3yiw', 'ysvvlil9bs', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1010302');
insert into `goods` values (25, 'xnfac4cgcc', 'ultra-mahgo', 609.87, 949, 386.22, '8hfxx9swkl', 'uif9nrbyg6', 'n6nflikmhs', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1020101');
insert into `goods` values (26, 'jc6omi1lrf', 'orange', 702.47, 247, 380.92, 'jf47lns0ln', '2nqdhyoepp', 'ttqxibyvjc', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1020102');
insert into `goods` values (27, 'toqnhecff7', 'rambutan elite', 166.33, 471, 534.81, 'o8kht3dytm', 'pzyukx2mke', '1k3z3peion', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1030101');
insert into `goods` values (28, 'doqvauoxxx', 'omni-manao', 483.54, 964, 432.00, 'nzq8jcrrzc', 'd6gpizay4l', 'evu3hbvo75', '2024-03-15 19:46:41', '2024-03-15 19:46:52', b'0', '1030102');
insert into `goods` values (29, 'yktntoyncb', 'pluots', 92.17, 432, 350.00, 'xlyjlwwzyr', 'shivvkroyq', 'cqrionav9n', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '101');
insert into `goods` values (30, 'h363ymokgx', 'strawberry', 994.03, 356, 693.20, 'edvprzfbx7', 'wohno9jz9i', 'ubus5mbqc1', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '102');
insert into `goods` values (31, 'reamnlqlec', 'cherry', 379.39, 662, 808.42, 'citpl2uecl', 'agsi2euagd', 'lv4hbc865s', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '103');
insert into `goods` values (32, 'eh19slgyzj', 'rambutan', 846.04, 75, 746.55, 'gxqmipnpaj', 'xsn1eykf2w', '8r9fqivv2m', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '104');
insert into `goods` values (33, '1rzu3tqk7x', 'grape', 13.95, 823, 216.07, '874xe6vfn8', 'v4qvostgxz', 'utpwhamjbj', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10101');
insert into `goods` values (34, 'rswbeuziv0', 'orange', 28.17, 774, 390.75, '5xymcnplni', 'xpkihsrb87', '5wsv0py08i', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10102');
insert into `goods` values (35, 'i63ka18rpr', 'pluots premium', 697.21, 637, 254.10, 'xtvctbht7g', 'sqmbrxuj93', 'jt0i7jpikz', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10103');
insert into `goods` values (36, 'juaaluevw2', 'plzots mini', 820.39, 975, 877.59, 'aeknujfrmj', 'vovubet0fd', 'xpyxnts6g7', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10104');
insert into `goods` values (37, 'ilmzyzai5p', 'omni-apple', 522.76, 391, 861.30, 'yek34rvaci', 'gmihmwtooa', 'zz78bgunty', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10201');
insert into `goods` values (38, 'bbuqlhempm', 'apple mini', 437.47, 997, 494.17, 'hxhucrt2su', 'bsrstt87h0', 'sxrb9hrjoa', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10202');
insert into `goods` values (39, 'q4a9nknyxf', 'orange', 480.17, 63, 884.94, 'kyod6tfqxp', 'fzvn2xuqjj', '89tq4sfgcz', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10203');
insert into `goods` values (40, 'fojywhi8ua', 'grape premium', 650.66, 566, 841.37, 'g8orswf3kw', 'prkkvnkd7o', 'hsowqhnhog', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10204');
insert into `goods` values (41, 's4afyoy3ll', 'ambi-rarpberry', 362.22, 153, 235.46, 'zdi09zjeu1', 'nzk0o8qcnc', 'vidsy2b2s2', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10301');
insert into `goods` values (42, '31u5gpiuc2', 'mango', 113.45, 227, 250.62, 'xpjlau4eht', 'yjj1bcfgoc', 'n1ao1bu0rp', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10302');
insert into `goods` values (43, '4g51wc0jcb', 'pluots pro', 303.28, 795, 89.98, 'acqc5khe2p', 'crthyhlp5b', 'litcpxwhdq', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10401');
insert into `goods` values (44, '84zehutljd', 'raspberry', 539.56, 306, 587.33, 'dx7khbn5u9', 'zqqzxtloyl', 'ux69ulyjju', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '10402');
insert into `goods` values (45, 'umyoirrix7', 'chdrry', 103.36, 459, 570.75, 'izpw211feu', 'nbnd5hrbhp', 'qswsemu3ej', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '1010101');
insert into `goods` values (46, 'iuvvaf5esi', 'ultra-maggo', 628.76, 494, 765.33, '2w7blgjvqu', 'oc1xpdt0yd', 'qwwhje1utc', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '1010102');
insert into `goods` values (47, '1hwkg09rul', 'pluots', 745.62, 138, 601.36, 'srzyabnsid', 'tcvzr1tatc', 'vxvndc4dj3', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '1010103');
insert into `goods` values (48, '9lshuqpgp0', 'cherry pro', 453.36, 780, 583.65, 'bqyrf7gwhe', '1dhzup6x7p', '0awyrcmycj', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '1010201');
insert into `goods` values (49, 'tp5pbkicw7', 'grahe', 357.14, 796, 755.99, 'fzmr1fqm0h', 'hgxmrashey', 'mleg0ttni5', '2024-03-15 19:46:41', '2024-03-15 19:47:00', b'0', '1010202');

set foreign_key_checks = 1;

4.先建两个excel表

在这里插入图片描述

可以像我一样放在项目里,当然你放的位置和文件名都随意 。

5.书写mapper

<!--将数据库数据导出到excel文件的映射-->
    <select id="queryall" resulttype="goods">
        select *
        from goods
        where isdel = 0
    </select>
<!--将excel文件数据导入到数据库的映射  使用foreach批量添加-->
    <insert id="addmanygoods">
        insert into goods (code, name, price, num, weight, cpu, memory, bodymemory)
        values
        <foreach collection="list" item="it" separator=",">
            (#{it.code},#{it.name},#{it.price},#{it.num},#{it.weight},#{it.cpu},#{it.memory},#{it.bodymemory})
        </foreach>
    </insert>

6.书写测试类

简单使用测试类来进行测试。

    sqlsession session = factoryutil.getfactory().opensession(true);
    goodsmapper goodsmapper = session.getmapper(goodsmapper.class);

    private list<goods> data() {
        //调用方法获取数据库数据
        return goodsmapper.queryall();
    }

    
    //写入excel操作
    public void testsimplewrite() {

        string filename = ".....\\shop\\src\\main\\excel\\goods.xlsx";
        easyexcel.write(filename, goods.class)
                .sheet("模板")
                .dowrite(this::data);

    }




    //读excel表,写入数据库操作
    public void testsimpleread() {
        string filename = ".......\\shop\\src\\main\\excel\\goods2.xlsx";
        easyexcel.read(filename, goods.class, new pagereadlistener<goods>(datalist -> {
            system.out.println(datalist.size());
            int n = goodsmapper.addmanygoods(datalist);
              system.out.println(n);
        })).sheet().doread();
    }

总结

easyexcel还有很多的属性,下方是为大家总结的常用api。

  • 数据读取和写入:easyexcel提供了简单易用的api来读取和写入excel文件中的数据。
  • 支持多种数据格式:easyexcel支持读写多种常见的excel格式,包括.xls和.xlsx等。
  • 数据转换:easyexcel支持将excel文件中的数据转换为java对象,并支持将java对象的数据写入到excel文件中。
  • 样式设置:easyexcel允许用户设置单元格样式、字体样式、边框样式等,以美化excel文件。
  • 事件监听器:easyexcel提供了事件监听器接口,允许用户在读取和写入excel文件时注册监听器来处理各种事件。
  • 数据校验:easyexcel支持对excel文件中的数据进行校验,以确保数据的有效性和完整性。
  • 大数据处理:easyexcel针对大数据量的excel文件读写进行了优化,具有较高的性能和效率。
  • 异常处理:easyexcel提供了异常处理机制,允许用户捕获和处理读写过程中可能出现的异常。
  • 可配置性:easyexcel提供了丰富的配置选项和参数,允许用户根据需求进行定制和调整。
  • 扩展性:easyexcel具有良好的扩展性,允许用户根据需要编写自定义的扩展插件和功能模块。

总的来说,easyexcel作为一款功能丰富的excel处理工具库,具有许多属性和功能,可以满足各种不同场景下的excel文件读写需求,并且易于使用和扩展。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

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

发表评论

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