当前位置: 首页 > news >正文

婚纱影楼网站源码外贸网站搭建推广

婚纱影楼网站源码,外贸网站搭建推广,网站 所有权,h5网站设计报价上接《Springboot下导入导出excel》,本篇详细介绍 EasyExcel 注解使用。 1. ExcelProperty value:指定写入的列头,如果不指定则使用成员变量的名字作为列头;如果要设置复杂的头,可以为value指定多个值order&#xff…

上接《Springboot下导入导出excel》,本篇详细介绍 EasyExcel 注解使用。

1. @ExcelProperty

  • value:指定写入的列头,如果不指定则使用成员变量的名字作为列头;如果要设置复杂的头,可以为value指定多个值
  • order:优先级高于 value,会根据 order 的顺序来匹配实体和excel中数据的顺序
  • index:优先级高于 valueorder,指定写到第几列,如果不指定则根据成员变量位置排序;默认第一个字段就是 index=0
  • converter:指定当前字段用什么转换器,默认会自动选择。可以用来设置类型转换器,需要实现 Converter 接口

1.1 value

默认情况下,使用类的属性名作为Excel的列表,当然也可以使用 @ExcelProperty 注解来重新指定属性名称。

public class Student {@ExcelProperty(value = "姓名")String name;@ExcelProperty(value = "年龄")Integer age;@ExcelProperty(value = "出生日期")String birthday;@ExcelProperty(value = "分数")Double score;
}

在这里插入图片描述

1.1.1 表头合并

public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"})String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;
}

在这里插入图片描述

1.2 index

如果不指定则按照属性在类中的排列顺序来。index 是指定该属性在Excel中列的下标,下标从 0 开始。

public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"}, index = 2)String name;@ExcelProperty(value = {"用户基本信息", "年龄"}, index = 1)Integer age;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数", index = 10)Double score;
}

在这里插入图片描述

1.3 order

order 的默认值为 Integer.MAX_VALUE,通过效果我们可以得出结论:order值越小,越排在前面

public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"}, order = 5)Integer age;@ExcelProperty(value = {"用户基本信息", "出生日期"}, order = 6)String birthday;@ExcelProperty(value = "分数")Double score;
}

在这里插入图片描述

✨⚠️注意:

  • 优先级:index > order > 默认配置
  • index 相当于绝对位置,下标从0开始
  • order 相当于相对位置,值越小的排在越前面

1.4 convert

在读写EXCEL时,有时候需要我们进行数据类型转换,例如我们这里的创建时间,在实体对象中是 Long 类型,但是这样直接导出到Excel中不太直观。我们需要转换成 yyyy-MM-dd HH:mm:ss 格式,此时我们就可以用到转换器。

📝DateTimeConverter:

public class DateTimeConverter implements Converter<Long> {private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// 支持导入的Java类型@Overridepublic Class<?> supportJavaTypeKey() {return Long.class;}// 支持导出的Excel类型@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}// 转换为Java@Overridepublic Long convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {return null;}// 转换为Excel@Overridepublic WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {if (value == null) {return new WriteCellData(CellDataTypeEnum.STRING, null);}LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneId.systemDefault());String dateStr = localDateTime.format(dateTimeFormatter);return new WriteCellData(dateStr);}
}
  • supportJavaTypeKey:导入的Java类型
  • supportExcelTypeKey:导出的Excel类型,返回 CellDataTypeEnum 类型。
  • convertToJavaData:导入转换逻辑。
  • convertToExcelData:导出转换逻辑,返回 WriteCellData 类型。

📝Student:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;@ExcelProperty(value = "创建时间", converter = DateTimeConverter.class)private Long createTime;
}

在这里插入图片描述

1.4.1 枚举转换

📝GenderEnum:

/*** 性别枚举*/
@Getter
@AllArgsConstructor
public enum GenderEnum {UNKNOWN(0, "未知"),MALE(1, "男性"),FEMALE(2, "女性");private final Integer value;private final String description;public static GenderEnum convert(Integer value) {return Stream.of(values()).filter(bean -> bean.value.equals(value)).findAny().orElse(UNKNOWN);}public static GenderEnum convert(String description) {return Stream.of(values()).filter(bean -> bean.description.equals(description)).findAny().orElse(UNKNOWN);}
}

Stream.of(values()) 是 Java 8 中 Stream API 的一种用法,用于将枚举类型的 values() 方法返回的数组转换为一个流。这样可以方便地对枚举常量进行各种操作,如过滤、映射等。

  • 每个枚举类型都有一个隐式的 values() 方法,该方法返回一个包含所有枚举常量的数组。
  • Stream.of(T... values) 是一个静态方法,它接受一个可变参数列表,并返回一个包含这些元素的流。当你将 values() 方法的结果传递给 Stream.of 时,它会将枚举常量数组转换为一个
  • 使用 findAny() 找到第一个匹配的枚举常量,如果没有找到匹配的枚举常量,则返回 UNKNOWN。

📝GenderConverter:

public class GenderConverter implements Converter<Integer> {// 支持导入的Java类型@Overridepublic Class<?> supportJavaTypeKey() {return Integer.class;}// 支持导出的Excel类型@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}// 转换为Java@Overridepublic Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {return GenderEnum.convert(cellData.getStringValue()).getValue();}// 转换为Excel@Overridepublic WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {return new WriteCellData(GenderEnum.convert(value).getDescription());}
}

在这里插入图片描述

2. @ExcelIgnore

默认所有字段都会和excel去匹配,加了这个注解会忽略该字段

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;@ExcelProperty(value = {"用户基本信息", "性别"}, converter = GenderConverter.class)@ExcelIgnoreInteger gender;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;@ExcelProperty(value = "创建时间", converter = DateTimeConverter.class)private Long createTime;
}

在这里插入图片描述

3. @ExcelIgnoreUnannotated

修饰类,如果类不标注该注解时,默认类中所有成员变量都会参与读写,无论是否在成员变量上加了 @ExcelProperty 的注解。标注该注解后,类中的成员变量如果没有标注 @ExcelProperty 注解将不会参与读写。

@Data
@NoArgsConstructor
@AllArgsConstructor
@ExcelIgnoreUnannotated
public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;Integer gender;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;@ExcelProperty(value = "创建时间", converter = DateTimeConverter.class)private Long createTime;
}

在这里插入图片描述

4. @HeadRowHeight

修饰类,指定列头行高

@Data
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(80)
public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;@ExcelProperty(value = {"用户基本信息", "性别"}, converter = GenderConverter.class)@ExcelIgnoreInteger gender;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;@ExcelProperty(value = "创建时间", converter = DateTimeConverter.class)private Long createTime;
}

在这里插入图片描述

5. @HeadStyle

设置标题样式

属性描述
dataFormat日期格式
hidden设置单元格使用此样式隐藏
locked设置单元格使用此样式锁定
quotePrefix在单元格前面增加 ' 单引号,数字或公式将以字符串形式展示
horizontalAlignment设置是否水平居中
wrapped设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见
verticalAlignment设置是否垂直居中
rotation设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180°
indent设置单元格中缩进文本的空格数
borderLeft设置左边框的样式
borderRight设置右边框样式
borderTop设置上边框样式
borderBottom设置下边框样式
leftBorderColor设置左边框颜色
rightBorderColor设置右边框颜色
topBorderColor设置上边框颜色
bottomBorderColor设置下边框颜色
fillPatternType设置填充类型
fillBackgroundColor设置背景色
fillForegroundColor设置前景色
shrinkToFit设置自动单元格自动大小
@Data
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(80)
@HeadStyle(fillForegroundColor = 10, fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, wrapped = BooleanEnum.TRUE)
public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;@ExcelProperty(value = {"用户基本信息", "性别"}, converter = GenderConverter.class)@ExcelIgnoreInteger gender;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;@ExcelProperty(value = "创建时间", converter = DateTimeConverter.class)private Long createTime;
}

在这里插入图片描述

6. @HeadFontStyle

设置标题字体格式

属性描述
fontName设置字体名称
fontHeightInPoints设置字体高度
italic设置字体是否斜体
strikeout是否设置删除线
color设置字体颜色
typeOffset设置偏移量
underline设置下划线
charset设置字体编码
bold设置字体是否加粗
@Data
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(80)
@HeadStyle(fillForegroundColor = 10, fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, wrapped = BooleanEnum.TRUE)
@HeadFontStyle(fontHeightInPoints = 10, color = 5)
public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;@ExcelProperty(value = {"用户基本信息", "性别"}, converter = GenderConverter.class)@ExcelIgnoreInteger gender;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;@ExcelProperty(value = "创建时间", converter = DateTimeConverter.class)private Long createTime;
}

在这里插入图片描述

✨💎:如果需要自定义样式,可以通过继承 AbstractCellStyleStrategy 类(public abstract class AbstractCellStyleStrategy implements CellWriteHandler),实现其setHeadCellStylesetContentCellStyle 方法可以自定义设置表头和单元格内容样式。

参见📖 Easyexcel(7-自定义样式)

7. @ContentRowHeight

修饰类,指定内容行高

@Data
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(80)
@HeadStyle(fillForegroundColor = 10, fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, wrapped = BooleanEnum.TRUE)
@HeadFontStyle(fontHeightInPoints = 10, color = 5)
@ContentRowHeight(value = 30)
public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;@ExcelProperty(value = {"用户基本信息", "性别"}, converter = GenderConverter.class)@ExcelIgnoreInteger gender;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;@ExcelProperty(value = "创建时间", converter = DateTimeConverter.class)private Long createTime;
}

在这里插入图片描述

8. @ColumnWidth

设置表格列的宽度

@Data
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(80)
@HeadStyle(fillForegroundColor = 10, fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, wrapped = BooleanEnum.TRUE)
@HeadFontStyle(fontHeightInPoints = 10, color = 5)
@ContentRowHeight(value = 30)
public class Student {@ExcelProperty(value = {"用户基本信息", "姓名"} )String name;@ExcelProperty(value = {"用户基本信息", "年龄"})Integer age;@ColumnWidth(25)@ExcelProperty(value = {"用户基本信息", "性别"}, converter = GenderConverter.class)Integer gender;@ExcelProperty(value = {"用户基本信息", "出生日期"})String birthday;@ExcelProperty(value = "分数")Double score;@ExcelProperty(value = "创建时间", converter = DateTimeConverter.class)private Long createTime;
}

在这里插入图片描述

9. @ContentStyle

设置内容格式注解,和 @HeadStyle 属性一致。

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

注解 @HeadStyle 的属性 dataFormat 没有说明,在此处说明一下。@ContentStyle 注解的 dataFormat 属性可以接受一个整数,该整数对应于 Excel 的预定义格式代码。

常见值:

  • 0 或 General:通用格式
  • 1:数字格式(0.00)
  • 2:货币格式(#,##0.00_); (#,##0.00)
  • 9:百分比格式(0%)
  • 20:日期格式(yyyy-mm-dd)
  • 21:日期格式(mm/dd/yyyy)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {@ExcelProperty("字符串标题")private String string;@ExcelProperty("日期标题 (yyyy-mm-dd)")@ContentStyle(dataFormat = 22) // yyyy-mm-dd 格式private Date date;@ExcelProperty("百分比标题")@ContentStyle(dataFormat = 9) // 百分比格式private Double percentage;@ExcelProperty("货币标题")@ContentStyle(dataFormat = 2) // 货币格式private Double currency;
}
public static void exportExcel(String fileName) {// 写入数据List<Student> data = new ArrayList<>();data.add(new Student("张三", new Date(), 0.2, 100.25));data.add(new Student("李四", new Date(), 0.35, 200.0));data.add(new Student("李丽", new Date(), 0.27, 345.5));data.add(new Student("王二", new Date(), 0.65, 123458.9));// 创建写入对象EasyExcel.write(fileName, Student.class).sheet("学生信息").doWrite(data);
}

在这里插入图片描述

10. @ContentFontStyle

设置单元格内容字体格式,和 @HeadFontStyle 属性一致。

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

✨💎:如果需要自定义样式,可以通过继承 AbstractCellStyleStrategy (public abstract class AbstractCellStyleStrategy implements CellWriteHandler)类,实现其setHeadCellStylesetContentCellStyle 方法可以自定义设置表头和单元格内容样式

11. @ContentLoopMerge

修饰字段,设置合并单元格的注解。

属性描述
eachRow合并列
columnExtend合并行

12. @OnceAbsoluteMerge

修饰类,用于指定位置的单元格合并。

属性描述
firstRowIndex第一行下标
lastRowIndex最后一行下标
firstColumnIndex第一列下标
lastColumnIndex最后一列下标

13. @DateTimeFormat

日期转换,value参照 java.text.SimpleDateFormat

14. @NumberFormat

数字转换,value参照java.text.DecimalFormat

15. 日期、数字导出方式

实际导出Excel会要求导出格式,整理以下4种方式。

15.1 注解@ContentStyle

15.2 注解@DateTimeFormat、@DateTimeFormat

15.3 自定义Converter

自定义Converter,重写convertToExcelData,实现转换;实体类成员变量的注解上增加 @ExcelProterty(value = “工作时间”,converter = DateConverter.class)

15.4 自定义CellWriteHandler

自定义CellWriteHandler,重写afterCellDispose,可以自定义内容样式、值等,自由度更高。构建Excel的时候,注册到处理类
EasyExcel.write(fileName, Student.class)
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(15)) // 设置列宽策略
.registerWriteHandler(new CustomCellWriteHandler()) // 自定义样式
.sheet(“学生信息”)
.doWrite(data);

参考文章:📖Easyexcel(注解使用)
https://blog.csdn.net/q1468051413/article/details/139348375
https://blog.csdn.net/weiwosuoai/article/details/141338421
https://cloud.tencent.com/developer/article/1671316

📖 Easyexcel

http://www.ds6.com.cn/news/49135.html

相关文章:

  • 做爰网站爱情岛适合企业员工培训的课程
  • 如何做好一个网站的推广广州seo关键词优化是什么
  • 用点心做点心官方网站seo搜索引擎优化软件
  • 公司网站怎么做才能吸引人免费发布友链
  • 广州网站开发 英诺科技互联网推广平台有哪些
  • 东营市住房和建设委员会网站软文推广产品
  • 新浪微博可以做网站吗竞价排名点击
  • wordpress 标签模板广州seo推广公司
  • 网站开发java连接数据库后关键词优化哪家好
  • 动漫做那个视频网站谷歌google下载安卓版 app
  • 网站建设项目验收付款百度指数有什么参考意义
  • 浅谈sns网站与流行sns网站对比经典软文案例标题加内容
  • 一个网站的建设要经过哪几个阶段企业网站优化外包
  • 网站的建设方面360建站官网
  • 外管局网站怎么做报告中国新闻今日头条
  • 做外贸做什么英文网站好下载百度app到手机上
  • 网站开发与设计公司搜狗搜索推广
  • 网站设计和网站建设公司网站推广怎么做
  • 天津 网站建设公司企业网站推广技巧
  • 怎么做自己淘宝优惠券网站电商营销策略
  • 海珠营销型网站建设公司今天最新新闻国内大事件
  • 创作服务平台搜索引擎优化作业
  • 重庆求建网站友情链接大全
  • 广西茶叶网站建设百度关键词模拟点击软件
  • 网站后台做完文章不显示seminar怎么读
  • 咸阳机场建设招聘信息网站百度快照入口
  • 有哪些设计软件seo关键字优化技巧
  • wordpress柳城惠州seo外包
  • 建设银行上海分行网站商旅100网页版
  • 毕业设计做购物网站的要求南昌网站开发公司