easyexcel使⽤教程-导出篇easyExcel使⽤教程–导出篇
开始准备⼯作
1、导⼊Maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
<version>4.12</version>
</dependency>
2、新建Student.java类
import l.annotation.ExcelIgnoreUnannotated; import l.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.math.BigDecimal;
import java.util.Date;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor// ⼀定要有⽆参构造⽅法
public class Student {
@ExcelProperty(value ="姓名")
private String name;
@ExcelProperty(value ="性别")
private Integer sex;
@ExcelProperty(value ="⽣⽇")
private Date birthday;
@ExcelProperty(value ="体重KG")
private BigDecimal weight;
private String memo;
}
3、generateStudentUtil.java类,随机⽣成Student对象
st.easyexcel.bean.Student;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class generateStudentUtil {
public static List<Student>generateStudent(int size){
List<Student> stues =new ArrayList<>();
for(int i =0; i < size; i++){
stues.add(new Student("姓名"+ i,(int)(Math.random()*2),randomDate(),randomWeight(),"备注"));
}
return stues;
}
public static Date randomDate(){
LocalDateTime localDateTime = LocalDateTime.of(randomInt(1990,2022),randomInt(1,12),randomInt(1,28),randomInt(0,23),randomInt(0,59),r andomInt(0,59),randomInt(0,999));
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
return Date.Instant());
}
public static int randomInt(int min,int max){bigdecimal转换为integer
int de = max - min;
// ⼆进制长度
int bitCount = BinaryString(de).length();
int ans =0;
do{
ans =0;
for(int i =0; i < bitCount; i++){
ans +=random0_1()<< i;
}
}while(ans > de);
return ans + min;
}
public static int random0_1(){
return(int)(Math.random()*2);
}
public static BigDecimal randomWeight(){
return BigDecimal.valueOf((Math.random()*10));
}
}
4、BaseTest.java
import l.EasyExcel;
import l.ExcelWriter;
import l.lumn.LongestMatchColumnWidthStyleStrategy; import java.util.function.Consumer;
// ⽗类
public class BaseTest {
/**
* 导出⽅法
*
* @param fileName ⽂件
* @param writerConsumer consumer
*/
public static void export(String fileName, Consumer<ExcelWriter> writerConsumer){ ExcelWriter writer = EasyExcel.write(fileName)
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.build();
writerConsumer.accept(writer);
writer.finish();
}
}
4、导出Excel报表
st.easyexcel;
import l.EasyExcel;
import l.ExcelWriter;
st.easyexcel.bean.Student;
import org.junit.Test;
import java.util.function.Consumer;
import st.ateStudent;
public class TestEasyExcel extends BaseTest {
@Test
public void export1(){
Consumer<ExcelWriter> consumer = writer ->{
writer.write(generateStudent(10), EasyExcel.writerSheet("学⽣信息")
.head(Student.class)
.build());
};
export("D:/报表.xlsx", consumer);
}
}
运⾏export1结果
5、把姓名格式化:1显⽰男,0显⽰⼥
新建SexConverter.java
verter;
import l.converters.Converter;
import l.enums.CellDataTypeEnum;
import l.metadata.CellData;
import l.metadata.GlobalConfiguration;
import l.metadata.property.ExcelContentProperty;
public class SexConverter implements Converter<Integer>{
@Override
public Class<Integer>supportJavaTypeKey(){
return Integer.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey(){
return CellDataTypeEnum.STRING;
}
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration)throws Exce ption {
return"男".StringValue())?1:0;
}
@Override
public CellData<String>convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration)thro ws Exception {
return new CellData<>(integer.equals(1)?"男":"⼥");
}
}
Student类sex属性注⼊SexConverter转换器
@ExcelProperty(value ="性别", converter = SexConverter.class)
private Integer sex;
再次运⾏export1()单元测试
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论