文章目录
1.准备Read.xlsx(具有两个sheet) 2.读取第一个sheet中的数据
3.读取所有sheet中的数据
EasyExcel填充 1.简单填充 1.准备 Fill01.xlsx 2.无模版 3.方法 4.结果
2.列表填充 1.准备 Fill02.xlsx 2.模板 3.方法 4.结果
3.组合填充 1.准备 Fill03.xlsx 2.模板 3.方法 4.结果
4.水平填充 1.准备 Fill04.xlsx 2.模板 3.方法 4.结果
1.准备Read.xlsx(具有两个sheet)
2.读取第一个sheet中的数据
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class DemoData {
@ExcelProperty ( value = "字符串标题" )
private String name;
@ExcelProperty ( value = "日期标题" )
private Date hireDate;
@ExcelProperty ( value = "数字标题" )
private Double salary;
}
2.方法
@Test
public void testRead01 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Read.xlsx" ;
List < DemoData > demoDataArrayList = new ArrayList < > ( ) ;
ExcelReader excelReader = EasyExcel . read ( fileName)
. build ( ) ;
ReadSheet sheetOne = EasyExcel . readSheet ( 0 )
. head ( DemoData . class )
. registerReadListener ( new AnalysisEventListener < DemoData > ( ) {
@Override
public void invoke ( DemoData demoData, AnalysisContext analysisContext) {
demoDataArrayList. add ( demoData) ;
}
@Override
public void doAfterAllAnalysed ( AnalysisContext analysisContext) {
System . out. println ( "解析完成一个sheet..." ) ;
}
} )
. build ( ) ;
excelReader. read ( sheetOne) ;
excelReader. finish ( ) ;
System . out. println ( "所有解析的数据为:" ) ;
demoDataArrayList. forEach ( System . out:: println ) ;
}
3.结果
3.读取所有sheet中的数据
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class DemoData {
@ExcelProperty ( value = "字符串标题" )
private String name;
@ExcelProperty ( value = "日期标题" )
private Date hireDate;
@ExcelProperty ( value = "数字标题" )
private Double salary;
}
2.方法
@Test
public void testRead02 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Read.xlsx" ;
List < DemoData > demoDataArrayList = new ArrayList < > ( ) ;
ExcelReader excelReader = EasyExcel . read ( fileName, DemoData . class , new AnalysisEventListener < DemoData > ( ) {
@Override
public void invoke ( DemoData demoData, AnalysisContext analysisContext) {
demoDataArrayList. add ( demoData) ;
}
@Override
public void doAfterAllAnalysed ( AnalysisContext analysisContext) {
System . out. println ( "解析完成一个sheet..." ) ;
}
} ) . build ( ) ;
excelReader. readAll ( ) ;
excelReader. finish ( ) ;
System . out. println ( "所有解析的数据为:" ) ;
demoDataArrayList. forEach ( System . out:: println ) ;
}
3.结果
EasyExcel填充
1.简单填充
1.准备 Fill01.xlsx
2.无模版
3.方法
@Test
public void testFill01 ( ) {
String templateFileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Fill01.xlsx" ;
String filledFileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Fill01_end.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( filledFileName)
. withTemplate ( templateFileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetNo ( 0 )
. build ( ) ;
Map < String , Object > data = new HashMap < > ( ) ;
data. put ( "name" , "张三" ) ;
data. put ( "age" , 20 ) ;
data. put ( "birthday" , new Date ( ) ) ;
excelWriter. fill ( data, writeSheet) ;
excelWriter. finish ( ) ;
}
4.结果
2.列表填充
1.准备 Fill02.xlsx
2.模板
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Fill02 {
private String name;
private Double number;
}
3.方法
@Test
public void testFill02 ( ) {
String templateFileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Fill02.xlsx" ;
String filledFileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Fill02_end.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( filledFileName)
. withTemplate ( templateFileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetNo ( 0 )
. build ( ) ;
List < Fill02 > data = new ArrayList < > ( ) ;
data. add ( Fill02 . builder ( )
. name ( "张三" )
. number ( 100.0 )
. build ( ) ) ;
data. add ( Fill02 . builder ( )
. name ( "李四" )
. number ( 200.0 )
. build ( ) ) ;
data. add ( Fill02 . builder ( )
. name ( "王五" )
. number ( 300.0 )
. build ( ) ) ;
excelWriter. fill ( data, writeSheet) ;
excelWriter. finish ( ) ;
}
4.结果
3.组合填充
1.准备 Fill03.xlsx
2.模板
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Fill03 {
private String name;
private Integer age;
}
3.方法
@Test
public void testFill03 ( ) {
String templateFileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Fill03.xlsx" ;
String filledFileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Fill03_end.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( filledFileName)
. withTemplate ( templateFileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetNo ( 0 )
. build ( ) ;
Map < String , Object > data1 = new HashMap < > ( ) ;
data1. put ( "time" , new Date ( ) ) ;
data1. put ( "num" , 2 ) ;
List < Fill03 > data2 = new ArrayList < > ( ) ;
data2. add ( Fill03 . builder ( ) . name ( "张三" ) . age ( 20 ) . build ( ) ) ;
data2. add ( Fill03 . builder ( ) . name ( "李四" ) . age ( 21 ) . build ( ) ) ;
FillConfig fillConfig = FillConfig . builder ( ) . forceNewRow ( Boolean . TRUE ) . build ( ) ;
excelWriter. fill ( data1, fillConfig, writeSheet) ;
excelWriter. fill ( data2, fillConfig, writeSheet) ;
excelWriter. finish ( ) ;
}
4.结果
4.水平填充
1.准备 Fill04.xlsx
2.模板
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Fill04 {
private String name;
private Integer age;
}
3.方法
@Test
public void testFill04 ( ) {
String templateFileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Fill04.xlsx" ;
String filledFileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Fill04_end.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( filledFileName)
. withTemplate ( templateFileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetNo ( 0 )
. build ( ) ;
List < Fill04 > data = new ArrayList < > ( ) ;
data. add ( Fill04 . builder ( ) . name ( "张三" ) . age ( 20 ) . build ( ) ) ;
data. add ( Fill04 . builder ( ) . name ( "李四" ) . age ( 21 ) . build ( ) ) ;
FillConfig fillConfig = FillConfig . builder ( ) . direction ( WriteDirectionEnum . HORIZONTAL ) . build ( ) ;
excelWriter. fill ( data, fillConfig, writeSheet) ;
excelWriter. finish ( ) ;
}
4.结果