读写csv可以用exce4j框架

读写csv可以用exce4j框架

mvn依赖包

 <dependency>
 <groupId>com.github.crab2died</groupId>
 <artifactId>Excel4J</artifactId>
 <version>2.1.4-Final2</version>
 </dependency>

读写操作

List<Student2> listStudent2s= ExcelUtils.getInstance().readCSV2Objects("csvtest_test2.csv", Student2.class);

ExcelUtils.getInstance().exportObjects2CSV(list, Student2.class, "csvtest_test2.csv");

详细代码如下


import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.github.crab2died.ExcelUtils;
import com.github.crab2died.annotation.ExcelField;
import com.github.crab2died.exceptions.Excel4JException;
import com.testfan.csv.convetor.DateConvertible;


public class Student2 {

    @ExcelField(title = "学号", order = 1)
    private Long id;

    @ExcelField(title = "姓名", order = 2)
    private String name;

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

    @ExcelField(title = "班级", order = 4)
    private Integer classes;

    @ExcelField(title = "是否开除", order = 5)
    private boolean expel;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Integer getClasses() {
        return classes;
    }

    public void setClasses(Integer classes) {
        this.classes = classes;
    }

    public boolean isExpel() {
        return expel;
    }

    public void setExpel(boolean expel) {
        this.expel = expel;
    }

    public Student2(Long id, String name, Date date, Integer classes, boolean expel) {
        this.id = id;
        this.name = name;
        this.date = date;
        this.classes = classes;
        this.expel = expel;
    }

    public Student2() {
    }

    @Override
    public String toString() {
        return "Student2{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", date=" + date +
                ", classes=" + classes +
                ", expel='" + expel + '\'' +
                '}';
    }
    
    //excel4j 最新版本支持 csv 注解
    public static void main(String[] args) throws Exception {
    	 List<Student2> list = new ArrayList<Student2>();
         list.add(new Student2(1000001L, "张三", new Date(), 1, true));
         list.add(new Student2(1010002L, "古尔丹", new Date(), 2, false));
         list.add(new Student2(1010003L, "蒙多(被开除了)", new Date(), 6, true));
         list.add(new Student2(1010004L, "萝卜特", new Date(), 3, false));
         list.add(new Student2(1010005L, "奥拉基", new Date(), 4, false));
         list.add(new Student2(1010006L, "得嘞", new Date(), 4, false));
         list.add(new Student2(1010007L, "瓜娃子", new Date(), 5, true));
         list.add(new Student2(1010008L, "战三", new Date(), 4, false));
         list.add(new Student2(1010009L, "李四", new Date(), 2, false));
         FileUtils.forceDelete(new File("csvtest_test2.csv")); 
         ExcelUtils.getInstance().exportObjects2CSV(list, Student2.class, "csvtest_test2.csv");
         List<Student2> listStudent2s= ExcelUtils.getInstance().readCSV2Objects("csvtest_test2.csv", Student2.class);
         System.out.println(listStudent2s);
	}
}


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.github.crab2died.converter.ReadConvertible;
import com.github.crab2died.converter.WriteConvertible;

public class DateConvertible implements WriteConvertible,ReadConvertible{

	SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd_HH_mm:ss");
	
	@Override
	public Object execWrite(Object object) {
		if(object instanceof Date) {
			Date date =(Date)object;
			return format.format(date);	
		}
		return object;
	}

	@Override
	public Object execRead(String object) {
		try {
			return format.parse(object);
		} catch (ParseException e) {
			//e.printStackTrace();
		}
		return object;
	}

}

posted @ 2020-12-07 10:13  测开工程师成长之路  阅读(124)  评论(0编辑  收藏  举报