您的位置:首页 > 其它

excel使用poi 导入导出一对多数据

2016-08-11 17:25 633 查看
前段时间简单的看了一下poi 于是便有了上篇博文 今天正好有时间 就把这个工具的升级版拿出来与大家分享

需求:导出一对多数据并合并单元格

开始

首先 需要导入poi依赖 这里以maven为例

<!-- 为POI支持Office Open XML -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>


然后 引入cn.sunxyz.common.excel包下的源码(代码以托管到github

下一步 在需要导出的类上标识注解

这里 简单的介绍一下@ExcelID,@ExcelAttribute,@ExcelElement这三个注解的作用
@ExcelID 用来标识身份
@ExcelAttribute 用来标识 生成excel的数据排版样式
@ExcelElement 用来标识集合与自定义对象(map暂时只支持String作为泛型 需要同时标注@ExcelAttribute,@ExcelElement使用)


使用 这里列举了一个简单的例子

首先在需要导出的对象的字段上标注对应的注解

public class School {

@ExcelID
@ExcelAttribute(name="学校编号",column="A")
private String id;

@ExcelAttribute(name="学校名称",column="B")
private String name;

@ExcelElement
private Set<Clazz> clazzs =  new HashSet<>();

@ExcelElement
@ExcelAttribute(name="学校描述",column="C")
private Map<String,String> map = new HashMap<>();

//此处省略了get/set

}


public class Clazz{

@ExcelID
@ExcelAttribute(name="教室编号",column="D")
private String id;

@ExcelAttribute(name="教室名称",column="E")
private String name;

@ExcelElement
private Set<Student> students = new HashSet<>();

//此处省略了get/set

}


public class Student {

@ExcelID
@ExcelAttribute(name="学生编号",column="F")
private String id;

@ExcelAttribute(name="学生姓名",column="G")
private String name;

@ExcelAttribute(name="学生年龄",column="H")
private Integer age;

@ExcelElement
@ExcelAttribute(name="学生详细信息",column="I")
private Map<String,String> map;

//此处省略了get/set
}


然后调用 导入导出方法 即可

public class EnitiyTest2 {

private Logger logger = LoggerFactory.getLogger(EnitiyTest2.class);

@Test
public void exportExcel(){

Set<Student> students = new HashSet<>();
Student student = new Student();;
student.setId("121");
student.setAge(8);
student.setName("小明");
students.add(student);

Map<String,String> sMap = new HashMap<>();
sMap.put("性别", "男");
sMap.put("地址", "济南");
Student student2 = new Student();;
student2.setId("122");
student2.setAge(9);
student2.setName("小李");
student2.setMap(sMap);
students.add(student2);

Set<Clazz> clazzs = new HashSet<>();

Clazz clazz = new Clazz();
clazz.setId("11");
clazz.setName("一年级");
clazz.setStudents(students);
clazzs.add(clazz);

Clazz clazz2 = new Clazz();
clazz2.setId("12");
clazz2.setName("二年级");
clazz2.setStudents(students);
clazzs.add(clazz2);

Clazz clazz3 = new Clazz();
clazz3.setId("13");
clazz3.setName("三年级");
clazzs.add(clazz3);

Clazz clazz4 = new Clazz();
clazz4.setId("14");
clazz4.setName("四年级");
clazz4.setStudents(students);
clazzs.add(clazz4);

List<School> list = new ArrayList<>();

School school = new School();
school.setId("1");
school.setName("中山");
school.setClazzs(clazzs);
list.add(school);

Map<String,String> map = new HashMap<>();
map.put("1", "红星小学");
map.put("2", "TOP");
School school1 = new School();
school1.setId("2");
school1.setName("红星");
school1.setClazzs(clazzs);
school1.setMap(map);
list.add(school1);

FileOutputStream output = null;
try {
output = new FileOutputStream("d:\\success3.xls");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
IExcelUtil<School> eu = new ExcelUtils<>();
eu.build(School.class).exportExcel(list, "学校信息", output);
}

@Test
public void importExcel(){
FileInputStream fis = null;
try {
fis = new FileInputStream("d:\\success3.xls");
IExcelUtil<School> util = new ExcelUtils<>();//创建excel工具类
List<School> list = util.build(School.class).importExcel("学校信息", fis);// 导入
logger.info(JSON.toJSONString(list));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}


导出效果展示



更详细代码可以访问 github

这里需要稍作声明 并未使用get/set方法 如果需要下一版会添加上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: