java使用EasyExcel导入导出excel

作者:迷途者寻影而行 时间:2022-10-31 03:48:30 

目录
  • 一、准备工作

    • 1、导包

  • 二、了解注解

    • 1、常用注解

    • 2、@ExcelProperty注解

    • 3、@ColumnWith注解

    • 4、@ContentFontStyle注解

    • 5、@ContentStyle注解

    • 6、@HeadFontStyle注解

    • 7、ExcelIgnore注解

  • 三、编码

    • 1、映射实体类----例子

    • 2、生成excel

  • 四、结果

    一、准备工作

    1、导包


    <!-- poi 相关-->
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.17</version>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
    </dependency>
    <!-- esayexcel 2.1.7  -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.7</version>
    </dependency>

    二、了解注解

    1、常用注解

    字段注解类注解
    @ColumnWith(列宽)@ColumnWidth(全局列宽)
    @ExcelProperty(字段配置)@HeadFontStyle(头样式)

    @HeadRowHeight(标题高度)

    @ContentFontStyle(内容字体样式)

    @ContentRowHeight(内容高度)

    2、@ExcelProperty注解

    必要的一个注解,注解中有三个参数value,index分别代表列明,列序号
    value和index只能二选一,通常不用设置converter
    1.value 通过标题文本对应
    2.index 通过文本行号对应


    @ExcelProperty(value = "编号", index = 0)
    private Long id;

    3、@ColumnWith注解

    设置列宽度,只有一个参数value,value的单位是字符长度,最大可以设置255个字符,因为一个excel单元格最大可以写入的字符个数就是255个字符


    public class ImeiEncrypt {
       @ColumnWidth(value = 255) //excel单个单元格最大长度255
       private String message;
    }

    4、@ContentFontStyle注解

    用于设置单元格内容字体格式的注解

    参数含义
    fontName字体名称
    fontHeightInPoints字体高度
    italic是否斜体
    strikeout是否设置删除水平线
    color字体颜色
    typeOffset偏移量
    underline下划线
    bold是否加粗
    charset编码格式

    5、@ContentStyle注解

    设置内容格式注解

    参数含义
    dataFormat日期格式
    hidden设置单元格使用此样式隐藏
    locked设置单元格使用此样式锁定
    quotePrefix在单元格前面增加`符号,数字或公式将以字符串形式展示
    horizontalAlignment设置是否水平居中
    wrapped设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见
    verticalAlignment设置是否垂直居中
    rotation设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180°
    indent设置单元格中缩进文本的空格数
    borderLeft设置左边框的样式
    borderRight设置右边框样式
    borderTop设置上边框样式
    leftBorderColor设置左边框颜色
    rightBorderColor设置右边框颜色
    topBorderColor设置上边框颜色
    bottomBorderColor设置下边框颜色
    fillPatternType设置填充类型
    fillBackgroundColor设置背景色
    shrinkToFit设置自动单元格自动大小

    6、@HeadFontStyle注解

    用于定制标题字体格式

    参数含义
    fontName设置字体名称
    fontHeightInPoints设置字体高度
    italic设置字体是否斜体
    strikeout是否设置删除线
    color设置字体颜色
    typeOffset设置偏移量
    underline设置下划线
    charset设置字体编码
    bold设置字体是否加粗

    7、ExcelIgnore注解

    不将该字段转换成Excel

    三、编码

    1、映射实体类----例子


    package com.pingou.admin.bean.param;

    import com.alibaba.excel.annotation.ExcelProperty;
    import com.alibaba.excel.annotation.format.DateTimeFormat;
    import com.alibaba.excel.annotation.write.style.ColumnWidth;
    import com.alibaba.excel.annotation.write.style.ContentRowHeight;
    import com.alibaba.excel.annotation.write.style.HeadRowHeight;
    import lombok.Data;

    import java.math.BigDecimal;
    import java.util.Date;

    @Data
    @ContentRowHeight(35) //文本行高度
    @HeadRowHeight(40)    //标题高度
    @ColumnWidth(40)
    public class OrderExcel {
       //设置excel表头名称
       @ExcelProperty(value = "编号", index = 0)
       private Long id;
       @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
       @ExcelProperty(value = "创建时间", index = 1)
       private Date createTime;
    }

    以上是简单的举例,如果有更多属性自己逐个写就好,然后塞进该实体类就好~

    2、生成excel


    public void excel() {
           //欲导出excel的数据结果集
           List<OrderExcel> excel = new ArrayList<>();
           //省略 向结果集里插入数据的操作

    //UUID生成唯一name
           String name = UUID.randomUUID().toString().replaceAll("-", "") + ".xlsx";
           //实现excel写的操作

    //1 设置写入文件夹地址和excel文件名称
           String filename = "/路径" + name;
           JSONObject json = new JSONObject();
           try {
               // 2 调用easyexcel里面的方法实现写操作
               // write方法两个参数:第一个参数文件路径名称,第二个参数实体类class
               EasyExcel.write(filename, OrderExcel.class).sheet("名字").doWrite(excel);
               //上传到fastdfs上 不上传的话只有本机可以找到,在上面路径下生成excel  
               File file = new File(filename);
               String path = fastDFSClient.upload(new FileInputStream(file), name, null);
               path = (this.fastdfsDomain + path);
               json.put("url", path);
           } catch (IOException e) {
               e.printStackTrace();
           } finally {
               new File(filename).delete();
           }
       }

    以上,就生成完毕了

    四、结果

    java使用EasyExcel导入导出excel

    来源:https://www.cnblogs.com/pkkyh/p/14771249.html

    标签:java,EasyExcel,excel
    0
    投稿

    猜你喜欢

  • C#使用后台线程BackgroundWorker处理任务的总结

    2023-12-08 10:28:19
  • Android 百度地图定位实现仿钉钉签到打卡功能的完整代码

    2022-09-05 17:32:46
  • 基于StreamRead和StreamWriter的使用(实例讲解)

    2022-09-11 22:12:36
  • 详解java封装实现Excel建表读写操作

    2023-12-24 11:17:20
  • 基于springmvc之常用注解,操作传入参数

    2023-03-17 20:49:52
  • android中ProgressDialog与ProgressBar的使用详解

    2023-04-23 16:19:58
  • Springboot+AOP实现返回数据提示语国际化的示例代码

    2021-08-18 19:49:12
  • java多线程编程之InheritableThreadLocal

    2022-02-08 21:40:32
  • Android仿QQ列表滑动删除操作

    2023-10-10 18:31:59
  • java实现输入输出流代码分享

    2023-11-18 01:03:45
  • 浅析SpringBoot2.4 静态资源加载问题

    2023-01-29 11:04:33
  • android自动化测试知识点总结

    2022-12-28 03:51:50
  • Android自定义控件实现时间轴

    2021-07-12 04:13:08
  • C# TreeView无限目录树实现方法

    2023-04-20 03:05:46
  • Android中Notification用法实例总结

    2023-03-16 23:05:38
  • c# 用Base64实现文件上传

    2023-12-20 00:52:11
  • mybatis in foreach 双层嵌套问题

    2023-11-24 12:55:47
  • SpringCloud Nacos + Ribbon 调用服务的实现方式(两种)

    2023-11-18 09:30:38
  • 聊聊SpringMVC项目依赖和静态资源导出问题

    2023-03-26 13:32:20
  • android 点击EditText始终不弹出软件键盘实现代码

    2022-05-23 06:37:19
  • asp之家 软件编程 m.aspxhome.com