在程序项目开发中,无论是Android还是Java,在很多地方都会使用到Excle表格作为数据的存储方式,这就避免不了程序对Excle文件的操作。
问题:目前,无论是JXL还是POI,对Excel的操作都是比较原始的,用户无法使用这些框架代码直接读取Excle并相应相应的数据实体(Entity),也无法直接将数据实体写入到Excle文件中去。
本文章要介绍的是一个对JXL框架进行封装,实现读取Excle文件数据并生成相应的Entity对象,实现直接将Entity数据写入到Excellent文件中。
JXLUtil框架功能
1、读取Excle数据,生成相应的Entity
2、将Entity数据直接写入到Excle文件中
注意:Entity支持日期属性、支持继承、支持包含其他Entity
下面是框架的一个使用Demo
作者实体AuthorEntity
package com.winway.jxl.test;
import com.winway.jxl.anomotion.CellColum;
public class AuthorEntity {
@CellColum(headerName = "作者", index = 0)
private String name;
@CellColum(headerName = "国籍", index = 1)
private String country;
@CellColum(headerName = "性别", index = 2)
private String sex;
@CellColum(headerName = "出生年月", index = 3)
private String birth;
@CellColum(headerName = "年龄", index = 4)
private int age;
@CellColum(headerName = "学历", index = 5)
private String edu;
@CellColum(headerName = "简介", index = 6)
private String product;
public AuthorEntity() {
super();
}
public AuthorEntity(String country, String name, String sex, String birth, int age, String edu,
String product) {
super();
this.country = country;
this.name = name;
this.sex = sex;
this.birth = birth;
this.age = age;
this.edu = edu;
this.product = product;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEdu() {
return edu;
}
public void setEdu(String edu) {
this.edu = edu;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name + "," + sex + "," + birth + "," + country;
}
}
书本BookEntity
package com.winway.jxl.test;
import com.winway.jxl.anomotion.CellColum;
import com.winway.jxl.anomotion.CellEntity;
public class BookEntity {
@CellColum(headerName = "书名", index = 0)
private String name;
@CellEntity(offset = 8)
private AuthorEntity author;
@CellColum(headerName = "出版社", index = 1)
private String publicCompony;
@CellColum(headerName = "语种", index = 2)
private String laguage;
@CellColum(headerName = "价格", index = 3)
private float price;
@CellColum(headerName = "类别", index = 4)
private String type;
@CellColum(headerName = "简介", index = 5)
private String product;
@CellColum(headerName = "存货", index = 6)
private boolean has;
public BookEntity() {
super();
}
public BookEntity(String name, AuthorEntity author, String publicCompony, String laguage,
float price, String type, String product, boolean has) {
super();
this.name = name;
this.author = author;
this.publicCompony = publicCompony;
this.laguage = laguage;
this.price = price;
this.type = type;
this.product = product;
this.has = has;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AuthorEntity getAuthor() {
return author;
}
public void setAuthor(AuthorEntity author) {
this.author = author;
}
public String getPublicCompony() {
return publicCompony;
}
public void setPublicCompony(String publicCompony) {
this.publicCompony = publicCompony;
}
public String getLaguage() {
return laguage;
}
public void setLaguage(String laguage) {
this.laguage = laguage;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public boolean isHas() {
return has;
}
public void setHas(boolean has) {
this.has = has;
}
@Override
public String toString() {
return name + "," + price + "," + has + "," + author.toString();
}
}
主类
package com.winway.jxl.test;
import java.util.ArrayList;
import java.util.List;
import com.winway.jxl.core.ExcelReader;
import com.winway.jxl.core.ExcelWriter;
public class TestXN2 {
static void write() throws Exception {
ExcelWriter<BookEntity> writer = new ExcelWriter<>("D:/test.xls");
ArrayList<BookEntity> list = new ArrayList<>();
AuthorEntity author = new AuthorEntity("中华人民共和国", "无名团体", "男女", "1960-12", 56, "本科",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFF");
BookEntity book = new BookEntity("十万个为什么", author, "国家教育出版社", "中文", 12.80f, "儿童教育读物",
"《十万个为什么》启发儿童积极思考、大胆想象,充分发挥自己的智慧和创造力。为什么是每个人童年的主旋律,跳动着无数天真好奇的音符,不要小看了孩子们不断提出 的一个个为什么,因为这是激发他们求知举的重要推动力,让孩子们获得最准确的科学知识,增长智慧,激发孩子热爱科学,探索科学的热情,瞪大好奇的双眼,刨根部底找答案!小小的头脑在解答中渐渐成长。",
true);
int max = 10;
for (int i = 0; i < max; i++) {
list.add(book);
}
writer.writeExcel(list);
}
static void read() throws Exception {
ExcelReader<BookEntity> reader = new ExcelReader<>(BookEntity.class, "D:/test.xls");
List<BookEntity> readExcel = reader.readExcel(0, 0);
for (BookEntity bookEntity : readExcel) {
System.out.println(bookEntity.toString());
}
}
public static void main(String[] args) throws Exception {
write();
read();
}
}
以下是运行结果
以下是项目工程的下载,包含JXLUtil.jar包和源码
源码下载