即使不做Excelbean也可以在DTO的属性上使用@ExcelProperty注解来指定需要转换的属性。@ExcelProperty注解有三个参数,分别是value、index和converter。其中,value表示Excel中的列名或列索引;index表示Excel中的列索引;converter表示需要使用的转换器。举个例子,假设有一个StudentDTO类,其中有一个属性是gender,需要将其转换为Excel中的性别列,可以这样写:```
public class StudentDTO {
// 学生姓名
@ExcelProperty(value = "姓名", index = 0)
private String name;
// 学生年龄
@ExcelProperty(value = "年龄", index = 1)
private Integer age;
// 学生性别
@ExcelProperty(value = "性别", index = 2, converter = GenderConverter.class)
private Gender gender;
// ...
}
```
在上面的代码中,@ExcelProperty注解指定了value为“性别”,index为2,converter为GenderConverter.class,表示需要将gender属性转换成Excel中的“性别”列,并且使用GenderConverter类来进行转换。
若excel表格上没有对应属性,会报错吗?
如果Excel表格中多出了一些列,但是DTO中没有对应的属性或注解,那么在转换的过程中不会报错,只是这些列的数据不会被读取。例如,假设有一个StudentDTO类,其中只有name和age两个属性,没有gender属性,那么下面的代码可以正常运行,只会读取Excel表格中的“姓名”和“年龄”列:```
public class StudentDTO {
// 学生姓名
@ExcelProperty(value = "姓名", index = 0)
private String name;
// 学生年龄
@ExcelProperty(value = "年龄", index = 1)
private Integer age;
// ...
}
```如果Excel表格中有一个额外的“性别”列,那么在转换的过程中,这个列的数据会被忽略掉,不会导致异常或错误。如果需要读取这个额外的列,可以在DTO中添加对应的属性或注解。
若Excel多出来列,但是dto没有注解,会报错吗
如果Excel表格中多出了一些列,但是DTO中没有对应的属性或注解,那么在转换的过程中不会报错,只是这些列的数据不会被读取。例如,假设有一个StudentDTO类,其中只有name和age两个属性,没有gender属性,那么下面的代码可以正常运行,只会读取Excel表格中的“姓名”和“年龄”列:```
public class StudentDTO {
// 学生姓名
@ExcelProperty(value = "姓名", index = 0)
private String name;
// 学生年龄
@ExcelProperty(value = "年龄", index = 1)
private Integer age;
// ...
}
```如果Excel表格中有一个额外的“性别”列,那么在转换的过程中,这个列的数据会被忽略掉,不会导致异常或错误。如果需要读取这个额外的列,可以在DTO中添加对应的属性或注解。