Octopus
如何導(dǎo)入excel
如何導(dǎo)出excel
OctopusOctopus 是一個(gè)簡(jiǎn)單的java excel導(dǎo)入導(dǎo)出工具.
如何導(dǎo)入excel下面是一個(gè)excel文件中sheet的數(shù)據(jù),有四個(gè)學(xué)生信息.
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 |
一個(gè)學(xué)生類(lèi),用來(lái)保存從excel中讀取的學(xué)生信息.
//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,并輸出學(xué)生信息:
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()); }
輸出的學(xué)生信息
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=[])
通過(guò)ModelEntity
完整的測(cè)試用例:src/test/cn/chenhuanming/octopus/core/SheetReaderTest
如何導(dǎo)出excel為了說(shuō)明導(dǎo)出的特性,我們給Student類(lèi)增加一個(gè)屬性GradeAndClazz用來(lái)表示年級(jí)和班級(jí).下面是最終的Student類(lèi),可以用來(lái)導(dǎo)入導(dǎo)出.
@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類(lèi),只有年級(jí)和班級(jí)兩個(gè)信息.
@Getter @Setter @AllArgsConstructor public class GradeAndClazz{ private String grade; private String clazz; }
需要一個(gè)xml來(lái)配置導(dǎo)出的屬性和屬性描述作為表頭
用代碼導(dǎo)出學(xué)生信息
//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);
導(dǎo)出結(jié)果
| 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 |
可以看到,對(duì)于gradeAndClazz屬性,會(huì)用一個(gè)合并單元格來(lái)表示.admission因?yàn)楸?b>@JsonFormat標(biāo)記,因此會(huì)格式化輸出日期。事實(shí)上Octopus會(huì)調(diào)用jackson來(lái)格式化json后再寫(xiě)入excel.
詳細(xì)例子在 src/test/cn/chenhuanming/octopus/core/OneSheetExcelWriterTest
github項(xiàng)目地址文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/67505.html
摘要:我想能不能像配置文件一樣可配置的導(dǎo)入導(dǎo)出,那樣使用起來(lái)就方便許多。配置和使用下面是員工信息模型。支持多種映射,使用英文逗號(hào)進(jìn)行分割。導(dǎo)入時(shí)它會(huì)以分割前面的作為導(dǎo)入時(shí)使用的值,后面的作為導(dǎo)出時(shí)使用的值后面值進(jìn)行逆推導(dǎo)出時(shí)同理。 1.前言 在工作時(shí),遇到過(guò)這樣的需求,需要靈活的對(duì)工單進(jìn)行導(dǎo)入或?qū)С觯郧白约阂沧鲞^(guò),但使用不靈活繁瑣。我想能不能像配置文件一樣可配置的導(dǎo)入導(dǎo)出,那樣使用起來(lái)就方...
摘要:的使用及導(dǎo)出報(bào)表首先,了解是什么一基本概念是軟件基金會(huì)的開(kāi)放源碼函式庫(kù),提供給程序?qū)Ω袷綑n案讀和寫(xiě)的功能。 POI的使用及導(dǎo)出excel報(bào)表 首先,了解poi是什么? 一、基本概念 ? Apache POI是Apache軟件基金會(huì)的開(kāi)放源碼函式庫(kù),POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫(xiě)的功能。 二、基本結(jié)構(gòu) ? HSSF - 提供讀寫(xiě)...
摘要:閱讀原文如何高效導(dǎo)出百萬(wàn)級(jí)數(shù)據(jù)在一個(gè)具有統(tǒng)計(jì)功能的系統(tǒng)中,導(dǎo)出功能幾乎是一定的,如何導(dǎo)出導(dǎo)出的數(shù)據(jù)有多少如何高效的導(dǎo)出簡(jiǎn)介什么是就不用介紹了,這里主要說(shuō)明不同版本下每個(gè)下的行列限制。 閱讀原文:POI如何高效導(dǎo)出百萬(wàn)級(jí)Excel數(shù)據(jù)? 在一個(gè)具有統(tǒng)計(jì)功能的系統(tǒng)中,導(dǎo)出excel功能幾乎是一定的,如何導(dǎo)出excel?導(dǎo)出的數(shù)據(jù)有多少?如何高效的導(dǎo)出? Excel簡(jiǎn)介什么是excel就不用...
摘要:拿到值后,創(chuàng)建當(dāng)前單元格,把數(shù)據(jù)填充進(jìn)去。首先判斷當(dāng)前單元格的數(shù)據(jù)是不是數(shù)字型的,如果是數(shù)字型的,在判斷是不是日期類(lèi)型的,如果是日期類(lèi)型,再轉(zhuǎn)為日期類(lèi)型。 導(dǎo)讀 最近,公司在做導(dǎo)入導(dǎo)出的項(xiàng)目,首先想到的是poi的導(dǎo)入和導(dǎo)出。如果每次導(dǎo)入和導(dǎo)出都要重寫(xiě)的話,那么,實(shí)在是浪費(fèi)時(shí)間和精力。于是,封裝了原生的poi的導(dǎo)入和導(dǎo)出。在封裝的時(shí)候,就會(huì)出現(xiàn)一系列的問(wèn)題。 在進(jìn)行導(dǎo)入和導(dǎo)出的時(shí)候,我們...
閱讀 808·2021-10-09 09:44
閱讀 2105·2021-09-22 15:54
閱讀 5206·2021-09-22 10:55
閱讀 1506·2019-08-29 18:41
閱讀 827·2019-08-29 11:24
閱讀 2167·2019-08-28 18:20
閱讀 1098·2019-08-26 11:51
閱讀 3115·2019-08-26 11:00