Octopus
如何導入excel
如何導出excel
OctopusOctopus 是一個簡單的java excel導入導出工具.
如何導入excel下面是一個excel文件中sheet的數據,有四個學生信息.
studentId | name | sex | inTime | score |
---|---|---|---|---|
20134123 | John | M | 2013-9-1 | 89 |
20124524 | Joyce | F | 20123-8-31 | 79 |
20156243 | P | 2015-5-15 | 94 | |
20116522 | Nemo | F | 2011-2-26 |
一個學生類,用來保存從excel中讀取的學生信息.
//lombok annotations @Getter @Setter @NoArgsConstructor @ToString public class Student { @ModelLineNumber private int lineNum; @ModelProperty(value = "id",blankable = false) private String studentId; @ModelProperty(value = "name",defaultValue = "anonymous") private String name; @ModelProperty(value = "sex",wrongMsg = "sex must be M or F",pattern = "^M|F$") private String sex; @ModelProperty(value = "admission",wrongMsg = "admission must be a date") private LocalDate inTime; @ModelProperty(value = "score",wrongMsg = "score must be numeric",defaultValue = "100") private Double score; }
用代碼讀取excel,并輸出學生信息:
InputStream is = getClass().getResourceAsStream("/test.xlsx"); Workbook workbook = WorkbookFactory.create(is); Sheet sheet = workbook.getSheetAt(0); //read students with ReusableSheetReader SheetReader> students = new ReusableSheetReader<>(sheet,1,0,Student.class); //print students information for (ModelEntity student:students) { System.out.println(student.toString()); }
輸出的學生信息
SimpleModelEntity(entity=Student(lineNum=2, studentId=20134123, name=John, sex=M, inTime=2013-09-01, score=89.0, gradeAndClazz=null), exceptions=[]) SimpleModelEntity(entity=Student(lineNum=3, studentId=20124524, name=Joyce, sex=F, inTime=null, score=79.0, gradeAndClazz=null), exceptions=[cn.chenhuanming.octopus.exception.DataFormatException: in cell (3,4) ,20123-8-31 can not be formatted to class java.time.LocalDate]) SimpleModelEntity(entity=Student(lineNum=4, studentId=20156243, name=anonymous, sex=null, inTime=2015-05-15, score=94.0, gradeAndClazz=null), exceptions=[cn.chenhuanming.octopus.exception.PatternNotMatchException: P and ^M|F$ don"t match!]) SimpleModelEntity(entity=Student(lineNum=5, studentId=20116522, name=Nemo, sex=F, inTime=2011-02-26, score=100.0, gradeAndClazz=null), exceptions=[])
通過ModelEntity
完整的測試用例:src/test/cn/chenhuanming/octopus/core/SheetReaderTest
如何導出excel為了說明導出的特性,我們給Student類增加一個屬性GradeAndClazz用來表示年級和班級.下面是最終的Student類,可以用來導入導出.
@Getter @Setter @NoArgsConstructor @ToString public class Student { @ModelLineNumber private int lineNum; @ModelProperty(value = "id",blankable = false) private String studentId; @ModelProperty(value = "name",defaultValue = "anonymous") private String name; @ModelProperty(value = "sex",wrongMsg = "sex must be M or F",pattern = "^M|F$") private String sex; //jackson annotation to format output @JsonFormat(pattern = "yyyy-MM-dd") @ModelProperty(value = "admission",wrongMsg = "admission must be a date") private LocalDate inTime; @ModelProperty(value = "score",wrongMsg = "score must be numeric",defaultValue = "100") private Double score; @ModelIgnore private GradeAndClazz gradeAndClazz; public Student(String studentId, String name, String sex, LocalDate inTime, Double score,GradeAndClazz gradeAndClazz) { this.studentId = studentId; this.name = name; this.sex = sex; this.inTime = inTime; this.score = score; this.gradeAndClazz = gradeAndClazz; } }
GradeAndClazz類,只有年級和班級兩個信息.
@Getter @Setter @AllArgsConstructor public class GradeAndClazz{ private String grade; private String clazz; }
需要一個xml來配置導出的屬性和屬性描述作為表頭
用代碼導出學生信息
//prepare workbook and stuednts objects Workbook workbook = new XSSFWorkbook(); String rootPath = this.getClass().getClassLoader().getResource("").getPath(); FileOutputStream os = new FileOutputStream(rootPath+"/export.xlsx"); GradeAndClazz gradeAndClazz = new GradeAndClazz("2014","R6"); Student student1 = new Student("201223","John","M", LocalDate.now(),98.00,gradeAndClazz); Student student2 = new Student("204354","Tony","M", LocalDate.now(),87.00,gradeAndClazz); Student student3 = new Student("202432","Joyce","F", LocalDate.now(),90.00,gradeAndClazz); //write excel with OneSheetExcelWriter ExcelWriterstudentExcelWriter = new OneSheetExcelWriter<>(getClass().getClassLoader().getResourceAsStream("studentExport.xml")); studentExcelWriter.write(workbook,Arrays.asList(student1,student2,student3)); workbook.write(os);
導出結果
| class info | id name M admission score |---------|----------| | grade | class | ---------------------------------------------------------------| 201223 John M 2017-07-06 98.0 | 2014 | R6 | 204354 Tony M 2017-07-06 87.0 | 2014 | R6 | 202432 Joyce F 2017-07-06 90.0 | 2014 | R6 |
可以看到,對于gradeAndClazz屬性,會用一個合并單元格來表示.admission因為被@JsonFormat標記,因此會格式化輸出日期。事實上Octopus會調用jackson來格式化json后再寫入excel.
詳細例子在 src/test/cn/chenhuanming/octopus/core/OneSheetExcelWriterTest
github項目地址文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/67505.html
摘要:我想能不能像配置文件一樣可配置的導入導出,那樣使用起來就方便許多。配置和使用下面是員工信息模型。支持多種映射,使用英文逗號進行分割。導入時它會以分割前面的作為導入時使用的值,后面的作為導出時使用的值后面值進行逆推導出時同理。 1.前言 在工作時,遇到過這樣的需求,需要靈活的對工單進行導入或導出,以前自己也做過,但使用不靈活繁瑣。我想能不能像配置文件一樣可配置的導入導出,那樣使用起來就方...
摘要:的使用及導出報表首先,了解是什么一基本概念是軟件基金會的開放源碼函式庫,提供給程序對格式檔案讀和寫的功能。 POI的使用及導出excel報表 首先,了解poi是什么? 一、基本概念 ? Apache POI是Apache軟件基金會的開放源碼函式庫,POI提供API給Java程序對Microsoft Office格式檔案讀和寫的功能。 二、基本結構 ? HSSF - 提供讀寫...
摘要:閱讀原文如何高效導出百萬級數據在一個具有統計功能的系統中,導出功能幾乎是一定的,如何導出導出的數據有多少如何高效的導出簡介什么是就不用介紹了,這里主要說明不同版本下每個下的行列限制。 閱讀原文:POI如何高效導出百萬級Excel數據? 在一個具有統計功能的系統中,導出excel功能幾乎是一定的,如何導出excel?導出的數據有多少?如何高效的導出? Excel簡介什么是excel就不用...
摘要:拿到值后,創建當前單元格,把數據填充進去。首先判斷當前單元格的數據是不是數字型的,如果是數字型的,在判斷是不是日期類型的,如果是日期類型,再轉為日期類型。 導讀 最近,公司在做導入導出的項目,首先想到的是poi的導入和導出。如果每次導入和導出都要重寫的話,那么,實在是浪費時間和精力。于是,封裝了原生的poi的導入和導出。在封裝的時候,就會出現一系列的問題。 在進行導入和導出的時候,我們...
閱讀 738·2021-10-09 09:44
閱讀 2005·2021-09-22 15:54
閱讀 5043·2021-09-22 10:55
閱讀 1434·2019-08-29 18:41
閱讀 771·2019-08-29 11:24
閱讀 2099·2019-08-28 18:20
閱讀 1024·2019-08-26 11:51
閱讀 3043·2019-08-26 11:00