1.使用maven管理工程,在pom.xml添加相关依赖
<dependency>
<groupId>com.github.crab2died</groupId>
<artifactId>Excel4J</artifactId>
<version>3.0.0-Alpha</version>
</dependency>
对象类:
package excel4j;
import com.github.crab2died.annotation.ExcelField;
public class StudentExcel {
/**
* id
*/
@ExcelField(title = "header1")
private String id;
/**
* 学号
*/
@ExcelField(title = "header2")
private String no;
/**
* 姓名
*/
@ExcelField(title = "header3")
private String name;
/**
* 学院
*/
@ExcelField(title = "header4")
private String college;
/**
* 成绩
*/
@ExcelField(title = "header5")
private float score;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "StudentExcel [id=" + id + ", no=" + no + ", name=" + name + ", college=" + college + ", score=" + score
+ "]";
}
}
测试类
package excel4j;
import java.io.File;
import java.util.List;
import com.github.crab2died.ExcelUtils;
public class Excel4JUtil {
public static void main(String[] args) throws Exception {
String path_read = System.getProperty("user.dir") + File.separator + "data" + File.separator + "test.xlsx";
String path_writeString = System.getProperty("user.dir") + File.separator + "data" + File.separator
+ "result.xlsx";
// 读
List<StudentExcel> list = Excel4JUtil.excel4j_readExc(path_read);
// 写
Excel4JUtil.excel4j_writeExc(list, path_writeString,StudentExcel.class);
}
/**
* 读excel
*
* @param excelPathRead
* @throws Exception
*/
public static List<StudentExcel> excel4j_readExc(String excelPathRead) throws Exception {
List<StudentExcel> list = ExcelUtils.getInstance().readExcel2Objects(excelPathRead, StudentExcel.class);
System.out.println(list);
return list;
}
/**
* 写excel
* @param <E>
*
* @param excelPathWrite
* @throws Exception
*/
public static <E> void excel4j_writeExc(List<E> list, String excelPathWrite,Class clazz) throws Exception {
ExcelUtils.getInstance().exportObjects2Excel(list, clazz, excelPathWrite);
}
}
/**
* 功能说明: 用来在对象的属性上加入的annotation,通过该annotation说明某个属性所对应的标题
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelField {
/**
* 属性的标题名称
*
* @return 表头名
*/
String title();
/**
* 写数据转换器
*
* @return 写入Excel数据转换器
* @see WriteConvertible
*/
Class<? extends WriteConvertible> writeConverter()
default DefaultConvertible.class;
/**
* 读数据转换器
*
* @return 读取Excel数据转换器
* @see ReadConvertible
*/
Class<? extends ReadConvertible> readConverter()
default DefaultConvertible.class;
/**
* 在excel的顺序
*
* @return 列表顺序
*/
int order() default Integer.MAX_VALUE;
}
@ExcelField(title = "入学日期",writeConverter = Converter.class, order = 3,readConverter=Converter.class)
private Date date;
属性:
title:title指定的名称要与Excel的某列的头名称对应
writeConverter/readConverter:写入/读取Excel列内容转换