Skip to content

Commit 64bdf56

Browse files
authored
Merge pull request #295 from ozlerhakan/4.1.2
Patch release 4.1.2
2 parents 60ce106 + 188bc7f commit 64bdf56

File tree

13 files changed

+243
-101
lines changed

13 files changed

+243
-101
lines changed

pom.xml

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

66
<groupId>com.github.ozlerhakan</groupId>
77
<artifactId>poiji</artifactId>
8-
<version>4.1.1</version>
8+
<version>4.1.2</version>
99
<packaging>jar</packaging>
1010

1111
<name>poiji</name>

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,27 @@ private <T> T tailSetFieldValue(Row currentRow, Class<? super T> type, T instanc
202202
throw new PoijiMultiRowException("Problem(s) occurred while reading data", errors);
203203
}
204204

205-
Map<String, String> excelUnknownCellsMap = StreamSupport
206-
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
207-
.filter(cell -> indexToTitle.size() != 0)
208-
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
209-
.filter(cell -> !cell.toString().isEmpty())
210-
.collect(Collectors.toMap(
211-
cell -> indexToTitle.get(cell.getColumnIndex()),
212-
Object::toString));
205+
if (unknownCells.isEmpty()) {
206+
return instance;
207+
}
213208

214-
unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
209+
if (!indexToTitle.isEmpty()) {
210+
Map<String, String> excelUnknownCellsMap = StreamSupport
211+
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
212+
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
213+
.collect(Collectors.toMap(
214+
cell -> indexToTitle.get(cell.getColumnIndex()),
215+
Object::toString));
216+
unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
217+
} else {
218+
Map<String, String> excelUnknownCellsMap = StreamSupport
219+
.stream(Spliterators.spliteratorUnknownSize(currentRow.cellIterator(), Spliterator.ORDERED), false)
220+
.filter(cell -> !mappedColumnIndices.contains(cell.getColumnIndex()))
221+
.collect(Collectors.toMap(
222+
cell -> valueOf(cell.getColumnIndex()),
223+
Object::toString));
224+
unknownCells.forEach(field -> setFieldData(instance, field, excelUnknownCellsMap));
225+
}
215226

216227
return instance;
217228
}
@@ -271,7 +282,7 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field,
271282
cell.setCellStyle(null);
272283
}
273284
String value;
274-
if (options.isRawData() && cell.getCellType() == CellType.NUMERIC) {
285+
if (options.isRawData() && isCellNumeric(cell)) {
275286
value = NumberToTextConverter.toText(cell.getNumericCellValue());
276287
} else {
277288
value = dataFormatter.formatCellValue(cell, baseFormulaEvaluator);
@@ -285,6 +296,12 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field,
285296
}
286297
}
287298

299+
private boolean isCellNumeric(Cell cell) {
300+
return (cell.getCellType() == CellType.NUMERIC ||
301+
(cell.getCellType() == CellType.FORMULA &&
302+
cell.getCachedFormulaResultType() == CellType.NUMERIC));
303+
}
304+
288305
private <T> void setFieldData(T instance, Field field, Object data) {
289306
try {
290307
field.setAccessible(true);

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,15 @@ private boolean setValue(String content, Class<? super T> type, int column) {
127127
} else {
128128
excelUnknownCellsMap = (Map<String, String>) field.get(instance);
129129
}
130-
excelUnknownCellsMap.put(indexToTitle.get(column), content);
130+
String index = indexToTitle.get(column);
131+
if (index == null) {
132+
excelUnknownCellsMap.put(valueOf(column), content);
133+
} else {
134+
excelUnknownCellsMap.put(indexToTitle.get(column), content);
135+
}
131136
} catch (IllegalAccessException e) {
132-
throw new IllegalCastException("Could not read content of field " + field.getName() + " on Object {" + instance + "}");
137+
throw new IllegalCastException("Could not read content of field " + field.getName()
138+
+ " on Object {" + instance + "}");
133139
}
134140
}
135141
});
@@ -167,7 +173,7 @@ private boolean setValue(Field field, int column, String content, Object ins) {
167173
excelCellNameAnnotations.add(excelCellName);
168174
final String titleName = formatting.transform(options, excelCellName.value());
169175
final Integer titleColumn = titleToIndex.get(titleName);
170-
//Fix both columns mapped to name passing this condition below
176+
// Fix both columns mapped to name passing this condition below
171177
if (titleColumn != null && titleColumn == column) {
172178
Object o = casting.castValue(field, content, internalRow, column, options);
173179
ReflectUtil.setFieldData(field, o, ins);
@@ -234,7 +240,7 @@ private String getTitleNameForMap(String cellContent, int columnIndex) {
234240

235241
@Override
236242
public void headerFooter(String text, boolean isHeader, String tagName) {
237-
//no-op
243+
// no-op
238244
}
239245

240246
@Override

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void shouldMapXLSSheet() throws IOException {
3737
assertThat(result, notNullValue());
3838
CellFormatModel cellFormatModel = result.get(1);
3939
assertThat(cellFormatModel.getAge(), is(40));
40+
workbook.close();
4041
}
4142

4243
@Test
@@ -51,21 +52,23 @@ public void shouldMapXLSXSheet() throws IOException {
5152
assertThat(result, notNullValue());
5253
CellFormatModel cellFormatModel = result.get(1);
5354
assertThat(cellFormatModel.getAge(), is(40));
55+
workbook.close();
5456
}
5557

56-
5758
@Test
5859
public void shouldMapXLSXSheetWithOptions() throws IOException {
5960

6061
File file = new File("src/test/resources/employees_format.xlsx");
6162
FileInputStream fileInputStream = new FileInputStream(file);
6263
Workbook workbook = new XSSFWorkbook(fileInputStream);
6364
Sheet sheet = workbook.getSheetAt(0);
64-
List<CellFormatModel> result = Poiji.fromExcel(sheet, CellFormatModel.class, PoijiOptions.PoijiOptionsBuilder.settings().build());
65+
List<CellFormatModel> result = Poiji.fromExcel(sheet, CellFormatModel.class,
66+
PoijiOptions.PoijiOptionsBuilder.settings().build());
6567

6668
assertThat(result, notNullValue());
6769
CellFormatModel cellFormatModel = result.get(1);
6870
assertThat(cellFormatModel.getAge(), is(40));
71+
workbook.close();
6972
}
7073

7174
@Test
@@ -76,7 +79,9 @@ public void shouldMapXLSXSheetWithOptionsConsumer() throws IOException {
7679
Workbook workbook = new XSSFWorkbook(fileInputStream);
7780
Sheet sheet = workbook.getSheetAt(0);
7881

79-
Poiji.fromExcel(sheet, CellFormatModel.class, PoijiOptions.PoijiOptionsBuilder.settings().build(), this::printout);
82+
Poiji.fromExcel(sheet, CellFormatModel.class, PoijiOptions.PoijiOptionsBuilder.settings().build(),
83+
this::printout);
84+
workbook.close();
8085
}
8186

8287
private void printout(CellFormatModel instance) {
@@ -91,5 +96,6 @@ public void shouldMapXLSXSheetError() throws IOException {
9196
Workbook workbook = new SXSSFWorkbook(xssfWorkbook);
9297
Sheet sheet = workbook.getSheetAt(0);
9398
Poiji.fromExcel(sheet, CellFormatModel.class);
99+
workbook.close();
94100
}
95101
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public void shouldPrintDateTimeFormatter() {
4444
assertThat(options.dateFormatter().toString(), equalTo(formatter.toString()));
4545
}
4646

47+
@Test
48+
public void shouldPrintDateFormatter() {
49+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/M/yyyy HH:mm:ss");
50+
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().dateTimeFormatter(formatter).build();
51+
assertThat(options.dateTimeFormatter().toString(), equalTo(formatter.toString()));
52+
}
53+
4754
@Test
4855
public void shouldUseUsLocaleWhenNotSpecified() {
4956
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().build();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.poiji.deserialize;
2+
3+
import static org.hamcrest.core.Is.is;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com.poiji.bind.Poiji;
7+
import com.poiji.deserialize.model.RowModelFormula;
8+
import com.poiji.option.PoijiOptions;
9+
import java.io.File;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
import org.junit.runners.Parameterized;
15+
16+
/**
17+
* Created by jmorgan on 12.09.2023
18+
*/
19+
@RunWith(Parameterized.class)
20+
public class RawValueFormulaTest {
21+
22+
private String path;
23+
24+
public RawValueFormulaTest(String path) {
25+
this.path = path;
26+
}
27+
28+
@Parameterized.Parameters(name = "{index}: ({0})={1}")
29+
public static Iterable<Object[]> queries() {
30+
return Arrays.asList(new Object[][] {
31+
{ "src/test/resources/raw_value_formula.xls" },
32+
{ "src/test/resources/raw_value_formula.xlsx" },
33+
});
34+
}
35+
36+
@Test
37+
public void shouldMapCalculations() {
38+
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().headerCount(0).rawData(true).build();
39+
List<RowModelFormula> models = Poiji.fromExcel(new File(path), RowModelFormula.class, options);
40+
41+
for (RowModelFormula model : models) {
42+
assertThat(model.getCurrencyValue(), is(123.45D));
43+
assertThat(model.getFormulaValue(), is(246.90D));
44+
}
45+
}
46+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.List;
1313

1414
import static org.hamcrest.core.Is.is;
15-
import static org.junit.Assert.assertThat;
15+
import static org.hamcrest.MatcherAssert.assertThat;
1616

1717
/**
1818
* Created by hakan on 02/08/2018
@@ -28,9 +28,9 @@ public RawValueTest(String path) {
2828

2929
@Parameterized.Parameters(name = "{index}: ({0})={1}")
3030
public static Iterable<Object[]> queries() {
31-
return Arrays.asList(new Object[][]{
32-
{"src/test/resources/raw_value.xls"},
33-
{"src/test/resources/raw_value.xlsx"},
31+
return Arrays.asList(new Object[][] {
32+
{ "src/test/resources/raw_value.xls" },
33+
{ "src/test/resources/raw_value.xlsx" },
3434
});
3535
}
3636

0 commit comments

Comments
 (0)