|
1 | 1 | package io.github.mbenincasa.javaexcelutils.model.excel; |
2 | 2 |
|
| 3 | +import io.github.mbenincasa.javaexcelutils.annotations.ExcelCellMapping; |
3 | 4 | import io.github.mbenincasa.javaexcelutils.exceptions.CellNotFoundException; |
| 5 | +import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException; |
4 | 6 | import io.github.mbenincasa.javaexcelutils.exceptions.RowNotFoundException; |
| 7 | +import io.github.mbenincasa.javaexcelutils.model.parser.ExcelCellParser; |
5 | 8 | import io.github.mbenincasa.javaexcelutils.tools.ExcelUtility; |
6 | 9 | import lombok.*; |
7 | 10 | import org.apache.poi.ss.usermodel.Cell; |
8 | 11 | import org.apache.poi.ss.usermodel.Row; |
9 | 12 | import org.apache.poi.ss.usermodel.Sheet; |
10 | 13 | import org.apache.poi.ss.util.CellRangeAddress; |
11 | 14 |
|
| 15 | +import java.lang.reflect.Field; |
| 16 | +import java.lang.reflect.InvocationTargetException; |
12 | 17 | import java.util.LinkedList; |
13 | 18 | import java.util.List; |
14 | 19 | import java.util.concurrent.atomic.AtomicInteger; |
@@ -165,18 +170,6 @@ public void appendCells(String startingCell, Stream<Object[]> data) { |
165 | 170 | writeOrAppendCells(data, rowIndex, colIndex); |
166 | 171 | } |
167 | 172 |
|
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 | | - |
180 | 173 | /** |
181 | 174 | * Method used to delete cells based on the selected range, for example: 'A1:B2' |
182 | 175 | * @param cellRange The range of cells to be deleted |
@@ -260,4 +253,56 @@ public Integer countAllRows(Boolean alsoEmpty) { |
260 | 253 |
|
261 | 254 | return count; |
262 | 255 | } |
| 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 | + } |
263 | 308 | } |
0 commit comments