Skip to content

Commit 8ed9864

Browse files
committed
Added the parseToObject() method present in ExcelSheet which is used to parse a Sheet into a Java object.
1 parent bf4d8d3 commit 8ed9864

File tree

16 files changed

+244
-19
lines changed

16 files changed

+244
-19
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.mbenincasa.javaexcelutils.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* This annotation is needed when the parseToObject() method in ExcelSheet is used
10+
* @author Mirko Benincasa
11+
* @since 0.5.0
12+
*/
13+
@Retention(RetentionPolicy.RUNTIME)
14+
@Target(ElementType.FIELD)
15+
public @interface ExcelCellMapping {
16+
17+
/**
18+
* @return the value to add to the source row
19+
*/
20+
int deltaRow();
21+
22+
/**
23+
* @return the value to add to the source column
24+
*/
25+
int deltaCol();
26+
}

src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelSheet.java

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

3+
import io.github.mbenincasa.javaexcelutils.annotations.ExcelCellMapping;
34
import io.github.mbenincasa.javaexcelutils.exceptions.CellNotFoundException;
5+
import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException;
46
import io.github.mbenincasa.javaexcelutils.exceptions.RowNotFoundException;
7+
import io.github.mbenincasa.javaexcelutils.model.parser.ExcelCellParser;
58
import io.github.mbenincasa.javaexcelutils.tools.ExcelUtility;
69
import lombok.*;
710
import org.apache.poi.ss.usermodel.Cell;
811
import org.apache.poi.ss.usermodel.Row;
912
import org.apache.poi.ss.usermodel.Sheet;
1013
import org.apache.poi.ss.util.CellRangeAddress;
1114

15+
import java.lang.reflect.Field;
16+
import java.lang.reflect.InvocationTargetException;
1217
import java.util.LinkedList;
1318
import java.util.List;
1419
import java.util.concurrent.atomic.AtomicInteger;
@@ -165,18 +170,6 @@ public void appendCells(String startingCell, Stream<Object[]> data) {
165170
writeOrAppendCells(data, rowIndex, colIndex);
166171
}
167172

168-
private void writeOrAppendCells(Stream<Object[]> data, AtomicInteger rowIndex, int colIndex) {
169-
data.forEach(rowData -> {
170-
ExcelRow excelRow = getOrCreateRow(rowIndex.get());
171-
for (int i = 0; i < rowData.length; i++) {
172-
Object value = rowData[i];
173-
ExcelCell excelCell = excelRow.getOrCreateCell(colIndex + i);
174-
excelCell.writeValue(value);
175-
}
176-
rowIndex.getAndIncrement();
177-
});
178-
}
179-
180173
/**
181174
* Method used to delete cells based on the selected range, for example: 'A1:B2'
182175
* @param cellRange The range of cells to be deleted
@@ -260,4 +253,56 @@ public Integer countAllRows(Boolean alsoEmpty) {
260253

261254
return count;
262255
}
256+
257+
/**
258+
* @param clazz The class of the object to return
259+
* @param startingCell The name of the source cell
260+
* @param <T> The class parameter of the object
261+
* @return The object parsed
262+
* @throws NoSuchMethodException If the setting method or empty constructor of the object is not found
263+
* @throws InvocationTargetException If an error occurs while instantiating a new object or setting a field
264+
* @throws InstantiationException If an error occurs while instantiating a new object
265+
* @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
266+
* @throws ReadValueException If an error occurs while reading a cell
267+
* @since 0.5.0
268+
*/
269+
public <T> T parseToObject(Class<T> clazz, String startingCell) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ReadValueException {
270+
T obj = clazz.getDeclaredConstructor().newInstance();
271+
int[] cellIndexes = ExcelUtility.getCellIndexes(startingCell);
272+
int startingRow = cellIndexes[0];
273+
int startingCol = cellIndexes[1];
274+
Field[] fields = clazz.getDeclaredFields();
275+
276+
for (Field field : fields) {
277+
if (!field.isAnnotationPresent(ExcelCellMapping.class))
278+
continue;
279+
280+
ExcelCellMapping excelCellMapping = field.getAnnotation(ExcelCellMapping.class);
281+
int deltaRow = excelCellMapping.deltaRow();
282+
int deltaCol = excelCellMapping.deltaCol();
283+
ExcelRow excelRow = getOrCreateRow(startingRow + deltaRow);
284+
ExcelCell excelCell = excelRow.getOrCreateCell(startingCol + deltaCol);
285+
field.setAccessible(true);
286+
287+
Object value = ExcelCellParser.class.isAssignableFrom(field.getType())
288+
? parseToObject(field.getType(), ExcelUtility.getCellName(startingRow + deltaRow, startingCol + deltaCol))
289+
: excelCell.readValue(field.getType());
290+
field.set(obj, value);
291+
292+
}
293+
294+
return obj;
295+
}
296+
297+
private void writeOrAppendCells(Stream<Object[]> data, AtomicInteger rowIndex, int colIndex) {
298+
data.forEach(rowData -> {
299+
ExcelRow excelRow = getOrCreateRow(rowIndex.get());
300+
for (int i = 0; i < rowData.length; i++) {
301+
Object value = rowData[i];
302+
ExcelCell excelCell = excelRow.getOrCreateCell(colIndex + i);
303+
excelCell.writeValue(value);
304+
}
305+
rowIndex.getAndIncrement();
306+
});
307+
}
263308
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.mbenincasa.javaexcelutils.model.parser;
2+
3+
/**
4+
* This interface is needed when the parseToObject() method in the ExcelSheet is used.
5+
* All objects to be parsed must implement this interface to identify that it is not a cell to read.
6+
* @author Mirko Benincasa
7+
* @since 0.5.0
8+
*/
9+
public interface ExcelCellParser {
10+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.mbenincasa.javaexcelutils.samples.convertExcelFileToObjectsSample;
1+
package io.github.mbenincasa.javaexcelutils.samples.convertExcelFileToObjects;
22

33
import io.github.mbenincasa.javaexcelutils.annotations.ExcelBodyStyle;
44
import io.github.mbenincasa.javaexcelutils.annotations.ExcelField;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.mbenincasa.javaexcelutils.samples.convertExcelFileToObjectsSample;
1+
package io.github.mbenincasa.javaexcelutils.samples.convertExcelFileToObjects;
22

33
import io.github.mbenincasa.javaexcelutils.model.converter.ExcelToObject;
44
import io.github.mbenincasa.javaexcelutils.tools.Converter;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.mbenincasa.javaexcelutils.samples.convertExcelFileToObjectsSample;
1+
package io.github.mbenincasa.javaexcelutils.samples.convertExcelFileToObjects;
22

33
import io.github.mbenincasa.javaexcelutils.annotations.ExcelBodyStyle;
44
import io.github.mbenincasa.javaexcelutils.annotations.ExcelField;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.mbenincasa.javaexcelutils.samples.convertObjectsToExcelFileSample;
1+
package io.github.mbenincasa.javaexcelutils.samples.convertObjectsToExcelFile;
22

33
import io.github.mbenincasa.javaexcelutils.annotations.ExcelBodyStyle;
44
import io.github.mbenincasa.javaexcelutils.annotations.ExcelField;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.mbenincasa.javaexcelutils.samples.convertObjectsToExcelFileSample;
1+
package io.github.mbenincasa.javaexcelutils.samples.convertObjectsToExcelFile;
22

33
import io.github.mbenincasa.javaexcelutils.enums.Extension;
44
import io.github.mbenincasa.javaexcelutils.model.converter.ObjectToExcel;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.mbenincasa.javaexcelutils.samples.convertObjectsToExcelFileSample;
1+
package io.github.mbenincasa.javaexcelutils.samples.convertObjectsToExcelFile;
22

33
import io.github.mbenincasa.javaexcelutils.annotations.ExcelBodyStyle;
44
import io.github.mbenincasa.javaexcelutils.annotations.ExcelField;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.mbenincasa.javaexcelutils.samples.parseSheetToExcel;
2+
3+
import io.github.mbenincasa.javaexcelutils.annotations.ExcelCellMapping;
4+
import io.github.mbenincasa.javaexcelutils.model.parser.ExcelCellParser;
5+
import lombok.AllArgsConstructor;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
import lombok.ToString;
9+
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
@Setter
13+
@ToString
14+
public class Address implements ExcelCellParser {
15+
16+
@ExcelCellMapping(deltaRow = 1, deltaCol = 0)
17+
private String city;
18+
@ExcelCellMapping(deltaRow = 2, deltaCol = 0)
19+
private String cap;
20+
}

0 commit comments

Comments
 (0)