Excel4j如何使用

一.如何引入?

1.使用maven管理工程,在pom.xml添加相关依赖

<dependency>
    <groupId>com.github.crab2died</groupId>
    <artifactId>Excel4J</artifactId>
    <version>3.0.0-Alpha</version>
</dependency>

二、如何使用?

2.1、处理Excel

2.2.1 读取/写入excel

对象类:

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);
	}


}

 

2.2、处理CSV

三、扩展

/**
 * 功能说明: 用来在对象的属性上加入的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;
}

3.1 @interface ExcelField注解的使用

3.1.1 @ExcelField 修饰Excel对象类的字段,该字段需与Excel的某列对应

@ExcelField(title = "入学日期",writeConverter = Converter.class, order = 3,readConverter=Converter.class)
    private Date date;

属性:

title:title指定的名称要与Excel的某列的头名称对应

writeConverter/readConverter:写入/读取Excel列内容转换

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值