⾃定义表头ExcelProperty注解中属性index和order区别
使⽤字段作为表头显然不是我们想要的,EasyExcel提供了ExcelProperty注解,可以定义表头的名称。这个注解还提供了index、order两个属性,可以定义列的位置和顺序。
@Data
public class IndexItem {
@ExcelProperty(value = "字符串标题", index = 1)
private String string;
@ExcelProperty(value = "⽇期标题", index = 3)
private Date date;
@ExcelProperty(value = "数字标题", index = 5)
java中index是什么意思private Double doubleData;
}
使⽤起来也很简单:
private static void writeWithIndex() {
final String fileName = defaultFileName("writeWithIndex");
EasyExcelFactory.write(fileName)
.head(IndexItem.class)
.sheet("模板")
.doWrite(WriteSample::sampleItems);
}
结果为:
这⾥需要注意⼀下,在使⽤ExcelProperty注解时,index表⽰字段放置第⼏列,order表⽰顺序。
根据index和order的不同语义,对两者的控制不同。如果index相同,直接会抛出异常,因为程序⽆法判断这个列放那个字段。如果index 值中间有空的数字,就会出现空列。如果order和index同时使⽤,index优先占据位置,order做排序。index=-1的话,使⽤java默认排序,order值越⼩,列越靠前。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。