Skip to content

Commit ba66777

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

File tree

5 files changed

+122
-1
lines changed

5 files changed

+122
-1
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import io.github.mbenincasa.javaexcelutils.exceptions.CellNotFoundException;
55
import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException;
66
import io.github.mbenincasa.javaexcelutils.exceptions.RowNotFoundException;
7+
import io.github.mbenincasa.javaexcelutils.model.parser.Direction;
78
import io.github.mbenincasa.javaexcelutils.model.parser.ExcelCellParser;
9+
import io.github.mbenincasa.javaexcelutils.model.parser.ExcelListParserMapping;
810
import io.github.mbenincasa.javaexcelutils.tools.ExcelUtility;
911
import lombok.*;
1012
import org.apache.poi.ss.usermodel.Cell;
@@ -294,6 +296,44 @@ public <T> T parseToObject(Class<T> clazz, String startingCell) throws NoSuchMet
294296
return obj;
295297
}
296298

299+
/**
300+
* @param clazz The class of the object to return
301+
* @param mapping The rules to retrieve the list of objects
302+
* @param <T> The class parameter of the object
303+
* @return The object list parsed
304+
* @throws ReadValueException If an error occurs while reading a cell
305+
* @throws InvocationTargetException If an error occurs while instantiating a new object or setting a field
306+
* @throws NoSuchMethodException If the setting method or empty constructor of the object is not found
307+
* @throws InstantiationException If an error occurs while instantiating a new object
308+
* @throws IllegalAccessException If a field or fields of the {@code clazz} could not be accessed
309+
*/
310+
public <T> List<T> parseToList(Class<T> clazz, ExcelListParserMapping mapping) throws ReadValueException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
311+
List<T> objectList = new LinkedList<>();
312+
int[] cellIndexes = ExcelUtility.getCellIndexes(mapping.getStartingCell());
313+
int startingRow = cellIndexes[0];
314+
int startingCol = cellIndexes[1];
315+
316+
if (mapping.getDirection() == Direction.HORIZONTAL) {
317+
int maxCol = getOrCreateRow(startingRow).getLastColumnIndex();
318+
while (startingCol <= maxCol) {
319+
String currentCell = ExcelUtility.getCellName(startingRow, startingCol);
320+
T obj = parseToObject(clazz, currentCell);
321+
objectList.add(obj);
322+
startingCol += mapping.getJumpCells();
323+
}
324+
} else if (mapping.getDirection() == Direction.VERTICAL) {
325+
int maxRow = getLastRowIndex();
326+
while (startingRow <= maxRow) {
327+
String currentCell = ExcelUtility.getCellName(startingRow, startingCol);
328+
T obj = parseToObject(clazz, currentCell);
329+
objectList.add(obj);
330+
startingRow += mapping.getJumpCells();
331+
}
332+
}
333+
334+
return objectList;
335+
}
336+
297337
private void writeOrAppendCells(Stream<Object[]> data, AtomicInteger rowIndex, int colIndex) {
298338
data.forEach(rowData -> {
299339
ExcelRow excelRow = getOrCreateRow(rowIndex.get());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.mbenincasa.javaexcelutils.model.parser;
2+
3+
/**
4+
* This enum defines the direction of the list in an Excel sheet which is used in the parseToList() method in the ExcelSheet
5+
* @author Mirko Benincasa
6+
* @since 0.5.0
7+
*/
8+
public enum Direction {
9+
10+
/**
11+
* Horizontal direction
12+
*/
13+
HORIZONTAL,
14+
15+
/**
16+
* Vertical direction
17+
*/
18+
VERTICAL
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.mbenincasa.javaexcelutils.model.parser;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
/**
7+
* Class used to define the rules to be applied for a Sheet in a list of objects
8+
* @author Mirko Benincasa
9+
* @since 0.5.0
10+
*/
11+
@AllArgsConstructor
12+
@Getter
13+
public class ExcelListParserMapping {
14+
15+
/**
16+
* Source cell name
17+
*/
18+
private String startingCell;
19+
20+
/**
21+
* List direction
22+
*/
23+
private Direction direction;
24+
25+
/**
26+
* The distance, according to the line of direction, between two adjacent sources
27+
*/
28+
private Integer jumpCells;
29+
}

src/main/java/io/github/mbenincasa/javaexcelutils/samples/parseSheetToExcel/Main.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import io.github.mbenincasa.javaexcelutils.model.excel.ExcelSheet;
44
import io.github.mbenincasa.javaexcelutils.model.excel.ExcelWorkbook;
5+
import io.github.mbenincasa.javaexcelutils.model.parser.Direction;
6+
import io.github.mbenincasa.javaexcelutils.model.parser.ExcelListParserMapping;
57

68
import java.io.File;
9+
import java.util.List;
710

811
public class Main {
912

@@ -16,7 +19,11 @@ public static void main(String[] args) {
1619
System.out.println("Start the parsing...");
1720
Employee employee = excelSheet.parseToObject(Employee.class, "A1");
1821
System.out.println("...completed");
19-
System.out.println("Data: " + employee.toString());
22+
ExcelSheet excelSheet1 = excelWorkbook.getSheet("DATA_2");
23+
ExcelListParserMapping mapping = new ExcelListParserMapping("A1", Direction.VERTICAL, 8);
24+
List<Employee> employees = excelSheet1.parseToList(Employee.class, mapping);
25+
System.out.println("Data single object: " + employee.toString());
26+
System.out.println("Data multi objects: " + employees.toString());
2027
} catch (Exception e) {
2128
System.err.println("There was an error. Check the console");
2229
throw new RuntimeException(e);

src/test/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelSheetTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

33
import io.github.mbenincasa.javaexcelutils.exceptions.*;
4+
import io.github.mbenincasa.javaexcelutils.model.parser.Direction;
5+
import io.github.mbenincasa.javaexcelutils.model.parser.ExcelListParserMapping;
46
import io.github.mbenincasa.javaexcelutils.tools.utils.ParsableEmployee;
57
import org.apache.poi.ss.usermodel.Sheet;
68
import org.apache.poi.ss.usermodel.Workbook;
@@ -179,4 +181,28 @@ void parseToObject() throws OpenWorkbookException, ExtensionNotValidException, I
179181
Assertions.assertEquals("Nocera Inferiore", employee.getAddress().getCity());
180182
Assertions.assertEquals("84014", employee.getAddress().getCap());
181183
}
184+
185+
@Test
186+
void parseToList() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, ReadValueException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
187+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFileToParse);
188+
ExcelSheet excelSheet = excelWorkbook.getSheet("DATA_2");
189+
ExcelListParserMapping mapping = new ExcelListParserMapping("A1", Direction.VERTICAL, 8);
190+
List<ParsableEmployee> employees = excelSheet.parseToList(ParsableEmployee.class, mapping);
191+
ParsableEmployee parsableEmployee = employees.get(0);
192+
Assertions.assertEquals("Mario", parsableEmployee.getName());
193+
Assertions.assertEquals("Rossi", parsableEmployee.getLastName());
194+
Assertions.assertEquals(25, parsableEmployee.getAge());
195+
Assertions.assertEquals(LocalDate.of(2022, 1 , 12), parsableEmployee.getHireDate());
196+
Assertions.assertEquals(LocalDate.of(2022, 2 , 12), parsableEmployee.getTerminationDate());
197+
Assertions.assertEquals("Nocera Inferiore", parsableEmployee.getAddress().getCity());
198+
Assertions.assertEquals("84014", parsableEmployee.getAddress().getCap());
199+
parsableEmployee = employees.get(1);
200+
Assertions.assertEquals("Antonio", parsableEmployee.getName());
201+
Assertions.assertEquals("Bianchi", parsableEmployee.getLastName());
202+
Assertions.assertEquals(30, parsableEmployee.getAge());
203+
Assertions.assertEquals(LocalDate.of(2022, 1 , 12), parsableEmployee.getHireDate());
204+
Assertions.assertEquals(LocalDate.of(2023, 1 , 12), parsableEmployee.getTerminationDate());
205+
Assertions.assertEquals("Pero", parsableEmployee.getAddress().getCity());
206+
Assertions.assertEquals("20016", parsableEmployee.getAddress().getCap());
207+
}
182208
}

0 commit comments

Comments
 (0)