一直在看博客,却不知道怎么写,但是总是想写点什么,犹豫了两三天,决定还是写点东西吧,来和大家分享下。
今年上半年时候接到了一个需求,具体什么需求就不说了,要求导入excel数据,并提示每一行错误信息。接到手后,查看之前的导入excel报表的代码,发现只有初始化workBook和根据cell获取cell 字符串值得通用方法。觉得可利用的东西太少了,于是决定就构建一个通用的实现导入excel报表的工具包。
实现思路大致是这样的:提供模板,模板包含读起始行、属性模板集合、读批次、校验出错中端。
添加属性模板,属性模板包含标题、属性名、字段索引、属性校验对象。
提供读引擎类,初始化读文件输入流、根据模板读取校验字段、校验错误信息管理、对象分配属性,匹配添加对象。
具体实现方式不做详细讲述,只讲述如何使用,我会把源码提供给大家,源码在com.easyReport.read下,有兴趣的可以读下,欢迎交流。
测试使用DEMO。
首先构建基础测试类BasicTest
package test;
import java.io.FileInputStream;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.easyReport.read.ConvertReader;
import com.easyReport.read.temp.ReadTemp;
public abstract class BasicTest {
//测试执行方法
@Test
public abstract void test();
//构建读引擎
protected abstract ConvertReader buildReader();
//构建读模板
protected abstract ReadTemp bulidReadTemp();
//输出对象信息
protected abstract void writeBeanList(List modelLists);
//输出Map信息
protected abstract void writeMapList(List> mapLists);
@SuppressWarnings("unchecked")
public void testReadBean(FileInputStream fis) throws Exception {
ConvertReader reader = buildReader();
writeBeanList((List) reader.readToBeanList(fis, getModelClass()));
}
public void testReadMap(FileInputStream fis) throws Exception {
ConvertReader reader = buildReader();
writeMapList(reader.readToMapList(fis));
}
protected Class> getModelClass() {
return (Class>) ((ParameterizedType) (getClass()
.getGenericSuperclass())).getActualTypeArguments()[0];
}
}
构建excel基础类实现读引擎
package test;
import com.easyReport.read.ConvertReader;
import com.easyReport.read.ExcelReader;
import com.easyReport.read.ReaderContext;
public abstract class AbstractExcelTest extends BasicTest {
@Override
protected ConvertReader buildReader() {
return new ExcelReader(new ReaderContext(bulidReadTemp()));
}
}
构建txt基础类实现读引擎
package test;
import com.easyReport.read.ConvertReader;
import com.easyReport.read.ReaderContext;
import com.easyReport.read.TxtReader;
public abstract class AbstractTxtTest extends BasicTest {
@Override
protected ConvertReader buildReader() {
return new TxtReader(new ReaderContext(bulidReadTemp()));
}
}
test.xls报表数据姓名年龄科目分数
王老五12语文80
Excel导入
package example;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import model.Student;
import test.AbstractExcelTest;
import com.easyReport.read.temp.PropertyTemp;
import com.easyReport.read.temp.ReadTemp;
public class TestExcelSimple extends AbstractExcelTest {
@Override
public void test() {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(new File("testFile/test.xls"));
testReadBean(fileInputStream);
//testReadMap(fileInputStream);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected ReadTemp bulidReadTemp() {
ReadTemp readTemp = new ReadTemp(1);
readTemp.addPropertyTemp(new PropertyTemp("姓名", "name"));
readTemp.addPropertyTemp(new PropertyTemp("年龄", "age"));
readTemp.addPropertyTemp(new PropertyTemp("科目", "course.name"));
readTemp.addPropertyTemp(new PropertyTemp("分数", "course.score"));
return readTemp;
}
@Override
protected void writeBeanList(List modelLists) {
System.out.println("--------------bean---------------");
for (Student s : modelLists) {
System.out.println(s.getName() + "|"+s.getAge()+s.getCourse().getName()+"|"+s.getCourse().getScore());
}
}
@Override
protected void writeMapList(List> mapLists) {
System.out.println("--------------map---------------");
for (Map m : mapLists) {
System.out.println(m.get("name") + "|" + m.get("age")+"|" + m.get("course.name")+"|" + m.get("course.score"));
}
}
}
运行结果:
--------------bean---------------
王老五|12语文|80.0
切换至testReadMap(fileInputStream),运行结果:
--------------map---------------
王老五|12.0|语文|80.0
test.txt数据
姓名|年龄|科目|年龄
王老五|12|语文|80
Txt导入
package example;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import model.Student;
import test.AbstractTxtTest;
import com.easyReport.read.temp.PropertyTemp;
import com.easyReport.read.temp.ReadTemp;
import com.easyReport.read.temp.TxtReadTemp;
public class TestTxtSimple extends AbstractTxtTest {
@Override
public void test() {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(new File("testFile/test.txt"));
testReadBean(fileInputStream);
//testReadMap(fileInputStream);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected ReadTemp bulidReadTemp() {
TxtReadTemp readTemp = new TxtReadTemp(1);
readTemp.setSplit("\\|");//默认,或,分割符
readTemp.addPropertyTemp(new PropertyTemp("姓名", "name"));
readTemp.addPropertyTemp(new PropertyTemp("年龄", "age"));
readTemp.addPropertyTemp(new PropertyTemp("科目", "course.name"));
readTemp.addPropertyTemp(new PropertyTemp("分数", "course.score"));
return readTemp;
}
@Override
protected void writeBeanList(List modelLists) {
System.out.println("--------------bean---------------");
for (Student s : modelLists) {
System.out.println(s.getName() + "|"
+ s.getAge()+s.getCourse().getName()+"|"+s.getCourse().getScore());
}
}
@Override
protected void writeMapList(List> mapLists) {
System.out.println("--------------map---------------");
for (Map m : mapLists) {
System.out.println(m.get("name") + "|" + m.get("age")+"|" + m.get("course.name")+"|" + m.get("course.score"));
}
}
}
运行结果:
--------------map---------------
王老五|12|语文|80
easyreport依赖jar包
注:很不好意思上传附件的时候把字节码打包上传了,请到第六章节下载!