Skip to content

Commit 351e497

Browse files
authored
Merge pull request #71 from ozlerhakan/1.19.2
version 1.19.2
2 parents 7fb47ad + 7cf9bde commit 351e497

File tree

17 files changed

+247
-86
lines changed

17 files changed

+247
-86
lines changed

README.adoc

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= Poiji
2-
:version: v1.19.1
2+
:version: v1.19.2
33

44
image:https://travis-ci.org/ozlerhakan/poiji.svg?branch=master["Build Status", link="https://travis-ci.org/ozlerhakan/poiji"] image:https://api.codacy.com/project/badge/Grade/6587e90886184da29a1b7c5634695c9d["Codacy code quality", link="https://www.codacy.com/app/ozlerhakan/poiji?utm_source=github.com&utm_medium=referral&utm_content=ozlerhakan/poiji&utm_campaign=Badge_Grade"] image:https://coveralls.io/repos/github/ozlerhakan/poiji/badge.svg?branch=master["Coverage Status", link="https://coveralls.io/github/ozlerhakan/poiji?branch=master"] image:https://img.shields.io/badge/apache.poi-4.0.0-brightgreen.svg[] image:https://img.shields.io/badge/gitter-join%20chat-blue.svg["Gitter", link="https://gitter.im/poiji/Lobby"] image:https://img.shields.io/badge/license-MIT-blue.svg[]
55

@@ -15,15 +15,15 @@ In your Maven/Gradle project, first add the corresponding dependency:
1515
<dependency>
1616
<groupId>com.github.ozlerhakan</groupId>
1717
<artifactId>poiji</artifactId>
18-
<version>1.19.1</version>
18+
<version>1.19.2</version>
1919
</dependency>
2020
----
2121

2222
.gradle
2323
[source,groovy]
2424
----
2525
dependencies {
26-
compile 'com.github.ozlerhakan:poiji:1.19.1'
26+
compile 'com.github.ozlerhakan:poiji:1.19.2'
2727
}
2828
----
2929

@@ -320,89 +320,79 @@ Car car = cars.get(0);
320320
Consider you have a table like below:
321321

322322
|===
323-
5+|Class A 5+| Class B
324-
|Name | Age | City | State | Zip Code | Name | Age | City | State | Zip Code
323+
3+|Group A 3+| Group B
324+
|NameA | AgeA | CityA | NameB | AgeB | CityB
325325

326326
|John Doe
327327
|21
328328
|Vienna
329-
|Virginia
330-
|22349
331329
|Smith Michael
332330
|32
333331
|McLean
334-
|Virginia
335-
|22309
336332

337333
|Jane Doe
338334
|28
339335
|Greenbelt
340-
|Maryland
341-
|20993
342336
|Sean Paul
343337
|25
344338
|Los Angeles
345-
|California
346-
|92384
347339

348340
|Paul Ryan
349341
|19
350342
|Alexandria
351-
|Virginia
352-
|22312
353343
|John Peter
354344
|25
355345
|Vienna
356-
|Virginia
357-
|22347
358346

359347
|Peter Pan
360348
|23
361349
|Alexandria
362-
|Virginia
363-
|22314
364350
|Arnold Regan
365351
|35
366352
|Seattle
367-
|Washington
368-
|90384
369353

370354
|===
371355

372356
The new `ExcelCellRange` annotation (as of 1.19) lets us aggregate a range of information in one object model. In this case, we collect the details of the first person in `classA` and for second person in `classB`:
373357

374358
[source,java]
375359
----
376-
public class Classes {
360+
public class Groups {
377361
378-
@ExcelCellRange(begin = 0, end = 4)
379-
private Person classA;
362+
@ExcelCellRange
363+
private GroupA groupA;
380364
381-
@ExcelCellRange(begin = 5, end = 9)
382-
private Person classB;
365+
@ExcelCellRange
366+
private GroupB groupB;
383367
384368
}
385369
----
386370

387371
[source, java]
388372
----
389-
public class Person {
373+
public class GroupA {
390374
391-
@ExcelCellName("Name")
375+
@ExcelCellName("NameA")
392376
private String name;
393377
394-
@ExcelCellName("Age")
378+
@ExcelCellName("AgeA")
395379
private Integer age;
396380
397-
@ExcelCellName("City")
381+
@ExcelCellName("CityA")
398382
private String city;
399383
400-
@ExcelCellName("State")
401-
private String state;
384+
}
385+
386+
public class GroupB {
402387
403-
@ExcelCellName("Zip Code")
404-
private Integer zip;
388+
@ExcelCellName("NameB")
389+
private String name;
390+
391+
@ExcelCellName("AgeB")
392+
private Integer age;
405393
394+
@ExcelCellName("CityB")
395+
private String city;
406396
}
407397
----
408398

@@ -411,12 +401,12 @@ Using the conventional way, we can retrieve the data using `Poiji.fromExcel`:
411401
[source,java]
412402
----
413403
PoijiOptions options = PoijiOptionsBuilder.settings().headerStart(1).build(); // header starts at 1 (zero-based).
414-
List<Classes> classes = Poiji.fromExcel(new File(excel), Classes.class, options);
404+
List<Groups> groups = Poiji.fromExcel(new File(excel), Groups.class, options);
415405
416-
Classes firstRowClasses = actualClasses.get(0);
406+
Groups firstRowGroups = actualGroups.get(0);
417407
418-
Person firstRowPerson1 = firstRowClasses.getClassA();
419-
Person secondRowPerson2 = firstRowClasses.getClassB();
408+
GroupA firstRowPerson1 = firstRowGroups.getGroupA();
409+
GroupB secondRowPerson2 = firstRowGroups.getGroupB();
420410
----
421411

422412
=== Example 5

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.ozlerhakan</groupId>
88
<artifactId>poiji</artifactId>
9-
<version>1.19.1</version>
9+
<version>1.19.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>poiji</name>

src/main/java/com/poiji/annotation/ExcelCellRange.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,4 @@
1313
@Target(ElementType.FIELD)
1414
@Documented
1515
public @interface ExcelCellRange {
16-
17-
int begin() default 0;
18-
int end();
19-
2016
}

src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class HSSFUnmarshaller implements Unmarshaller {
3939
this.options = options;
4040
dataFormatter = new DataFormatter();
4141
titles = new HashMap<>();
42-
casting = options.getCasting();
42+
casting = options.getCasting();
4343
}
4444

4545
@Override
@@ -84,7 +84,7 @@ private void loadColumnTitles(Sheet sheet, int maxPhysicalNumberOfRows) {
8484
int row = options.getHeaderStart();
8585
Row firstRow = sheet.getRow(row);
8686
for (Cell cell : firstRow) {
87-
titles.put(cell.getStringCellValue() + cell.getColumnIndex(), cell.getColumnIndex());
87+
titles.put(cell.getStringCellValue(), cell.getColumnIndex());
8888
}
8989
}
9090
}
@@ -101,7 +101,6 @@ private <T> T deserialize0(Row currentRow, Class<T> type) {
101101
}
102102

103103
private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instance) {
104-
int column = 0;
105104
for (Field field : type.getDeclaredFields()) {
106105
ExcelRow excelRow = field.getAnnotation(ExcelRow.class);
107106
if (excelRow != null) {
@@ -111,9 +110,6 @@ private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instanc
111110
}
112111
ExcelCellRange excelCellRange = field.getAnnotation(ExcelCellRange.class);
113112
if (excelCellRange != null) {
114-
if (column < excelCellRange.begin() || column > excelCellRange.end()) {
115-
continue;
116-
}
117113
Class<?> o = field.getType();
118114
Object ins;
119115
try {
@@ -122,29 +118,24 @@ private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instanc
122118
throw new PoijiInstantiationException("Cannot create a new instance of " + o.getName());
123119
}
124120
for (Field f : o.getDeclaredFields()) {
125-
tailSetFieldValue(currentRow, ins, f, column++);
126-
}
127-
try {
128-
field.setAccessible(true);
129-
field.set(instance, ins);
130-
} catch (IllegalArgumentException | IllegalAccessException e) {
131-
throw new PoijiInstantiationException("Cannot access field " + field.getName());
121+
tailSetFieldValue(currentRow, ins, f);
132122
}
123+
setFieldData(instance, field, ins);
133124
} else {
134-
tailSetFieldValue(currentRow, instance, field, column++);
125+
tailSetFieldValue(currentRow, instance, field);
135126
}
136127
}
137128
return instance;
138129
}
139130

140-
private <T> void tailSetFieldValue(Row currentRow, T instance, Field field, int column) {
131+
private <T> void tailSetFieldValue(Row currentRow, T instance, Field field) {
141132
ExcelCell index = field.getAnnotation(ExcelCell.class);
142133
if (index != null) {
143134
constructTypeValue(currentRow, instance, field, index.value());
144135
} else {
145136
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
146137
if (excelCellName != null) {
147-
Integer titleColumn = titles.get(excelCellName.value() + column);
138+
Integer titleColumn = titles.get(excelCellName.value());
148139
if (titleColumn != null) {
149140
constructTypeValue(currentRow, instance, field, titleColumn);
150141
}

src/main/java/com/poiji/bind/mapping/PoijiHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ private boolean setValue(String content, Class<? super T> type, int column) {
129129
}
130130
ExcelCellRange range = field.getAnnotation(ExcelCellRange.class);
131131
if (range != null) {
132-
if (column < range.begin() || column > range.end()) {
133-
continue;
134-
}
135132
Object ins = null;
136133
ins = getInstance(field);
137134
for (Field f : field.getType().getDeclaredFields()) {
@@ -164,7 +161,7 @@ private boolean setValue(Field field, int column, String content, Object ins) {
164161
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
165162
if (excelCellName != null) {
166163
Class<?> fieldType = field.getType();
167-
Integer titleColumn = titles.get(excelCellName.value() + column);
164+
Integer titleColumn = titles.get(excelCellName.value() );
168165
//Fix both columns mapped to name passing this condition below
169166
if (titleColumn != null && titleColumn == column) {
170167
Object o = casting.castValue(fieldType, content, options);
@@ -215,7 +212,7 @@ public void cell(String cellReference, String formattedValue, XSSFComment commen
215212
int headers = options.getHeaderStart();
216213

217214
if (row <= headers) {
218-
titles.put(formattedValue + column, column);
215+
titles.put(formattedValue, column);
219216
}
220217

221218
if (row + 1 <= options.skip()) {

src/test/java/com/poiji/deserialize/DeserializeByCellRangeTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.poiji.bind.Poiji;
44
import com.poiji.deserialize.model.byid.Classes;
5-
import com.poiji.deserialize.model.byid.PersonTest;
5+
import com.poiji.deserialize.model.byid.PersonATest;
6+
import com.poiji.deserialize.model.byid.PersonBTest;
67
import com.poiji.option.PoijiOptions;
78
import com.poiji.option.PoijiOptions.PoijiOptionsBuilder;
89
import org.junit.Test;
@@ -49,10 +50,10 @@ public void shouldMapExcelToJavaMulti() {
4950
Classes actualClasses1 = actualClasses.get(0);
5051
Classes actualClasses2 = actualClasses.get(1);
5152

52-
PersonTest expectedPerson1 = actualClasses1.getClassA();
53-
PersonTest expectedPerson2 = actualClasses1.getClassB();
54-
PersonTest expectedPerson3 = actualClasses2.getClassA();
55-
PersonTest expectedPerson4 = actualClasses2.getClassB();
53+
PersonATest expectedPerson1 = actualClasses1.getClassA();
54+
PersonBTest expectedPerson2 = actualClasses1.getClassB();
55+
PersonATest expectedPerson3 = actualClasses2.getClassA();
56+
PersonBTest expectedPerson4 = actualClasses2.getClassB();
5657

5758
assertThat(expectedPerson1.getAge(), is(28));
5859
assertThat(expectedPerson2.getCity(), is("Los Angeles"));
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.poiji.deserialize;
2+
3+
import com.poiji.bind.Poiji;
4+
import com.poiji.deserialize.model.ProductExcelDTO;
5+
import com.poiji.exception.PoijiExcelType;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.Parameterized;
9+
10+
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
import static org.hamcrest.CoreMatchers.not;
18+
import static org.hamcrest.CoreMatchers.notNullValue;
19+
import static org.junit.Assert.assertThat;
20+
import static org.junit.Assert.fail;
21+
22+
/**
23+
* Created by hakan on 2019-01-10
24+
*/
25+
@RunWith(Parameterized.class)
26+
public class DeserializerCaseTest {
27+
28+
private String path;
29+
private PoijiExcelType poijiExcelType;
30+
31+
public DeserializerCaseTest(String path, PoijiExcelType type) {
32+
this.path = path;
33+
this.poijiExcelType = type;
34+
}
35+
36+
@Parameterized.Parameters(name = "{index}: ({0})={1}")
37+
public static Iterable<Object[]> queries() {
38+
return Arrays.asList(new Object[][]{
39+
{"src/test/resources/test.xlsx", PoijiExcelType.XLSX},
40+
{"src/test/resources/test.xls", PoijiExcelType.XLS},
41+
});
42+
}
43+
44+
@Test
45+
public void shouldMapExcelToJava() {
46+
try (InputStream stream = new FileInputStream(new File(path))) {
47+
List<ProductExcelDTO> products = Poiji.fromExcel(stream, poijiExcelType, ProductExcelDTO.class);
48+
assertThat(products, notNullValue());
49+
assertThat(products.size(), not(0));
50+
} catch (IOException e) {
51+
fail(e.getMessage());
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)