文章18 | 阅读 21274 | 点赞0
示例
Test
@Data
class Student{
private Integer id;
private String name;
private Integer age;
}
public class Test {
private static final String template_path="e:"+ File.separator+"java"+File.separator+"easyexcel"+File.separator+"template2.xlsx";
private static final String fill_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill3.xlsx";
private static final String fill2_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill4.xlsx";
private static final String fill3_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill5.xlsx";
private static final String fill4_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill6.xlsx";
private static final String fill5_path="e:"+ File.separator+"java"+File.separator+"easyexcel2"+File.separator+"fill7.xlsx";
public static void fill(){
EasyExcel.write(fill_path).withTemplate(template_path).sheet().doFill(data());
}
public static void fill2(){
ExcelWriter excelWriter=null;
try{
excelWriter=EasyExcel.write(fill2_path).withTemplate(template_path).build();
WriteSheet writeSheet=EasyExcel.writerSheet("Sheet2").build();
Map<String,Object> map=new HashMap<>();
map.put("date",new Date());
map.put("total",5);
FillConfig fillConfig= FillConfig.builder().forceNewRow(true).build();
excelWriter.fill(data(),fillConfig,writeSheet);
excelWriter.fill(map,writeSheet);
}finally {
if (excelWriter!=null){
excelWriter.finish();
}
}
}
public static void fill3(){
ExcelWriter excelWriter=null;
try {
excelWriter=EasyExcel.write(fill3_path).withTemplate(template_path).registerConverter(new CustomConverter()).build();
WriteSheet writeSheet=EasyExcel.writerSheet("Sheet3").build();
Map<String,Object> map=new HashMap<>();
map.put("date", LocalDateTime.now());
excelWriter.fill(data(),writeSheet);
excelWriter.fill(map,writeSheet);
List<List<String>> lists=new ArrayList<>();
List<String> list=new ArrayList<>();
list.add(null);
list.add(null);
list.add("total:"+5);
lists.add(list);
excelWriter.write(lists,writeSheet);
}finally {
if (excelWriter!=null){
excelWriter.finish();
}
}
}
public static void fill4(){
FillConfig fillConfig= FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
EasyExcel.write(fill4_path).withTemplate(template_path).sheet("Sheet4").doFill(data(),fillConfig);
}
public static void fill5(){
ExcelWriter excelWriter=null;
try{
excelWriter=EasyExcel.write(fill5_path).withTemplate(template_path).build();
WriteSheet writeSheet=EasyExcel.writerSheet("Sheet5").build();
FillConfig fillConfig= FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
excelWriter.fill(new FillWrapper("data1",data()),writeSheet);
excelWriter.fill(new FillWrapper("data2",data()),fillConfig,writeSheet);
}finally {
if (excelWriter!=null){
excelWriter.finish();
}
}
}
private static List<Student> data(){
List<Student> list=new ArrayList<>();
for (int i=0;i<5;i++){
Student student=new Student();
student.setId(i);
student.setName("瓜田李下 "+i);
student.setAge(20+i);
list.add(student);
}
return list;
}
public static void main(String[] args){
fill();
fill2();
fill3();
fill4();
fill5();
}
}
使用测试
竖向填充
竖向复杂填充
竖向复杂大数据量填充
横向填充
带前缀填充
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43931625/article/details/107621809
内容来源于网络,如有侵权,请联系作者删除!