excel使用poi 匯入匯出一對多資料
阿新 • • 發佈:2019-01-22
前段時間簡單的看了一下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>
下一步 在需要匯出的類上標識註解
這裡 簡單的介紹一下@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方法 如果需要下一版會新增上