EasyExcel环境搭建
1.创建模块 easyexcel-demo
2.引入依赖
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 2.4.2</ version>
</ parent>
< groupId> com.sunxiansheng</ groupId>
< artifactId> easyexcel-demo</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< properties>
< maven.compiler.source> 8</ maven.compiler.source>
< maven.compiler.target> 8</ maven.compiler.target>
< project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding>
</ properties>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> easyexcel</ artifactId>
< version> 3.1.2</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
</ dependencies>
</ project>
3.启动类创建
package com. sunxiansheng ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
@SpringBootApplication
public class ExcelApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( ExcelApplication . class , args) ;
}
}
EasyExcel写
1.最简单的写入
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class Write01 {
@ExcelProperty ( value = "用户编号" )
private Integer userId;
@ExcelProperty ( value = "姓名" )
private String userName;
@ExcelProperty ( value = "性别" )
private String gender;
@ExcelProperty ( value = "工资" )
private Double salary;
@ExcelProperty ( value = "入职时间" )
private Date hireDate;
}
2.方法
@Test
public void testWrite01 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write01.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" )
. head ( Write01 . class )
. build ( ) ;
List < Write01 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write01 user = Write01 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? "女" : "男" )
. salary ( i * 1000.00 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
excelWriter. finish ( ) ;
}
3.结果 Write01.xlsx
2.指定字段不写入Excel
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class Write02 {
@ExcelProperty ( value = "用户编号" )
private Integer userId;
@ExcelProperty ( value = "姓名" )
private String userName;
@ExcelProperty ( value = "性别" )
private String gender;
@ExcelProperty ( value = "工资" )
private Double salary;
@ExcelProperty ( value = "入职时间" )
private Date hireDate;
}
2.方法
@Test
public void testWrite02 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write02.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" )
. head ( Write02 . class )
. excludeColumnFieldNames ( Arrays . asList ( "salary" , "hireDate" ) )
. build ( ) ;
List < Write02 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write02 user = Write02 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? "女" : "男" )
. salary ( i * 1000.00 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
excelWriter. finish ( ) ;
}
3.结果 Write02.xlsx
3.指定字段写入excel
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class Write03 {
@ExcelProperty ( value = "用户编号" )
private Integer userId;
@ExcelProperty ( value = "姓名" )
private String userName;
@ExcelProperty ( value = "性别" )
private String gender;
@ExcelProperty ( value = "工资" )
private Double salary;
@ExcelProperty ( value = "入职时间" )
private Date hireDate;
}
2.方法
@Test
public void testWrite03 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write03.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" )
. head ( Write03 . class )
. includeColumnFieldNames ( Arrays . asList ( "salary" , "hireDate" ) )
. build ( ) ;
List < Write03 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write03 user = Write03 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? "女" : "男" )
. salary ( i * 1000.00 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
excelWriter. finish ( ) ;
}
3.结果 Write03.xlsx
4.按照index顺序写入excel
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class Write04 {
@ExcelProperty ( value = "用户编号" , index = 0 )
private Integer userId;
@ExcelProperty ( value = "姓名" , index = 1 )
private String userName;
@ExcelProperty ( value = "性别" , index = 3 )
private String gender;
@ExcelProperty ( value = "工资" , index = 4 )
private Double salary;
@ExcelProperty ( value = "入职时间" , index = 2 )
private Date hireDate;
}
2.方法
@Test
public void testWrite04 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write04.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" )
. head ( Write04 . class )
. build ( ) ;
List < Write04 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write04 user = Write04 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? "女" : "男" )
. salary ( i * 1000.00 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
excelWriter. finish ( ) ;
}
3.结果 Write04.xlsx
5.复杂头数据写入,合并单元格
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class Write05 {
@ExcelProperty ( value = { "group1" , "用户编号" } )
private Integer userId;
@ExcelProperty ( value = { "group1" , "姓名" } )
private String userName;
@ExcelProperty ( value = { "group2" , "性别" } )
private String gender;
@ExcelProperty ( value = { "group2" , "工资" } )
private Double salary;
@ExcelProperty ( value = "入职时间" )
private Date hireDate;
}
2.方法
@Test
public void testWrite05 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write05.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" )
. head ( Write05 . class )
. build ( ) ;
List < Write05 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write05 user = Write05 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? "女" : "男" )
. salary ( i * 1000.00 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
excelWriter. finish ( ) ;
}
3.结果 Write05.xlsx
6.写入数据到不同的sheet中
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class Write06 {
@ExcelProperty ( value = "用户编号" )
private Integer userId;
@ExcelProperty ( value = "姓名" )
private String userName;
@ExcelProperty ( value = "性别" )
private String gender;
@ExcelProperty ( value = "工资" )
private Double salary;
@ExcelProperty ( value = "入职时间" )
private Date hireDate;
}
2.方法
@Test
public void testWrite06 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write06.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
for ( int j = 0 ; j < 10 ; j++ ) {
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" + j)
. head ( Write06 . class )
. build ( ) ;
List < Write06 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write06 user = Write06 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? "女" : "男" )
. salary ( i * 1000.00 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
}
excelWriter. finish ( ) ;
}
3.结果 Write06.xlsx
7.日期数字格式化
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class Write07 {
@ExcelProperty ( value = "用户编号" )
private Integer userId;
@ExcelProperty ( value = "姓名" )
private String userName;
@ExcelProperty ( value = "性别" )
private String gender;
@ExcelProperty ( value = "工资" )
@ContentStyle ( dataFormat = 2 )
private Double salary;
@ExcelProperty ( value = "入职时间" )
@DateTimeFormat ( value = "yyyy年MM月dd日 HH时mm分ss秒" )
private Date hireDate;
}
2.方法
@Test
public void testWrite07 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write07.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" )
. head ( Write07 . class )
. build ( ) ;
List < Write07 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write07 user = Write07 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? "女" : "男" )
. salary ( i * 1000.213214234 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
excelWriter. finish ( ) ;
}
3.结果 Write07.xlsx
8.写入图片到Excel
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@ContentRowHeight ( value = 100 )
@ColumnWidth ( value = 20 )
static class Write08 {
@ExcelProperty ( value = "File类型" )
private File file;
@ExcelProperty ( value = "InputStream类型" )
private InputStream inputStream;
@ExcelProperty ( value = "String类型" , converter = StringImageConverter . class )
private String str;
@ExcelProperty ( value = "二进制数据(字节)" )
private byte [ ] byteArr;
@ExcelProperty ( value = "网络图片" )
private URL url;
}
2.方法
@Test
public void testWrite08 ( ) throws Exception {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write08.xlsx" ;
String imagePath = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/th.jpeg" ;
URL url = new URL ( "https://cn.bing.com/th?id=OHR.TanzaniaBeeEater_ZH-CN3246625733_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp" ) ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. head ( Write08 . class )
. sheetName ( "图片信息" )
. build ( ) ;
byte [ ] bytes = new byte [ ( int ) new File ( imagePath) . length ( ) ] ;
InputStream inputStream = new FileInputStream ( imagePath) ;
inputStream. read ( bytes, 0 , bytes. length) ;
List < Write08 > imageDataList = new ArrayList < > ( ) ;
Write08 imageData = Write08 . builder ( )
. file ( new File ( imagePath) )
. inputStream ( new FileInputStream ( imagePath) )
. str ( imagePath)
. byteArr ( bytes)
. url ( url)
. build ( ) ;
imageDataList. add ( imageData) ;
excelWriter. write ( imageDataList, writeSheet) ;
excelWriter. finish ( ) ;
}
3.结果 Write08.xlsx
9.通过注解形式设置写入Excel样式
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@HeadRowHeight ( value = 30 )
@ContentRowHeight ( value = 25 )
@ColumnWidth ( value = 20 )
@HeadFontStyle ( fontName = "宋体" , fontHeightInPoints = 20 )
@ContentFontStyle ( fontName = "宋体" , fontHeightInPoints = 20 )
public static class Write09 {
@ExcelProperty ( value = "用户编号" )
private Integer userId;
@HeadFontStyle ( fontHeightInPoints = 30 )
@ContentFontStyle ( fontName = "宋体" , fontHeightInPoints = 20 )
@ExcelProperty ( value = "姓名" )
private String userName;
@ExcelProperty ( value = "性别" )
private String gender;
@ExcelProperty ( value = "工资" )
private Double salary;
@ExcelProperty ( value = "入职时间" )
private Date hireDate;
}
2.方法
@Test
public void testWrite09 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write09.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" )
. head ( Write09 . class )
. build ( ) ;
List < Write09 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write09 user = Write09 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? "女" : "男" )
. salary ( i * 1000.00 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
excelWriter. finish ( ) ;
}
3.结果 Write09.xlsx
10.应用自定义转换器
1.模板
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public static class Write10 {
@ExcelProperty ( value = "用户编号" )
private Integer userId;
@ExcelProperty ( value = "姓名" )
private String userName;
@ExcelProperty ( value = "性别" , converter = GenderConverter . class )
private Integer gender;
@ExcelProperty ( value = "工资" )
private Double salary;
@ExcelProperty ( value = "入职时间" )
private Date hireDate;
}
2.自定义转换器
public static class GenderConverter implements Converter < Integer > {
private static final String MALE = "男" ;
private static final String FEMALE = "女" ;
@Override
public Class < ? > supportJavaTypeKey ( ) {
return Integer . class ;
}
@Override
public CellDataTypeEnum supportExcelTypeKey ( ) {
return CellDataTypeEnum . STRING ;
}
@Override
public WriteCellData < ? > convertToExcelData ( Integer value,
ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws Exception {
String gender;
switch ( value) {
case 0 :
gender = MALE ;
break ;
case 1 :
gender = FEMALE ;
break ;
default :
gender = "未知" ;
}
return new WriteCellData < > ( gender) ;
}
@Override
public Integer convertToJavaData ( ReadCellData < ? > cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
String cellDataStringValue = cellData. getStringValue ( ) ;
switch ( cellDataStringValue) {
case MALE :
return 0 ;
case FEMALE :
return 1 ;
default :
return - 1 ;
}
}
}
3.方法
@Test
public void testWrite10 ( ) {
String fileName = "/Users/sunxiansheng/IdeaProjects/demo/easyexcel-demo/src/main/resources/Write10.xlsx" ;
ExcelWriter excelWriter = EasyExcel . write ( fileName)
. build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( )
. sheetName ( "用户信息" )
. head ( Write10 . class )
. build ( ) ;
List < Write10 > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
Write10 user = Write10 . builder ( )
. userId ( i)
. userName ( "admin" + i)
. gender ( i % 2 == 0 ? 1 : 0 )
. salary ( i * 1000.00 )
. hireDate ( new Date ( ) )
. build ( ) ;
list. add ( user) ;
}
excelWriter. write ( list, writeSheet) ;
excelWriter. finish ( ) ;
}
4.结果 Write10.xlsx