|
| 1 | +package model; |
| 2 | + |
| 3 | +import exceptions.ReadValueException; |
| 4 | +import lombok.AllArgsConstructor; |
| 5 | +import lombok.EqualsAndHashCode; |
| 6 | +import lombok.Getter; |
| 7 | +import org.apache.poi.ss.usermodel.Cell; |
| 8 | +import org.apache.poi.ss.usermodel.CellStyle; |
| 9 | +import org.apache.poi.ss.usermodel.FormulaEvaluator; |
| 10 | +import org.apache.poi.ss.usermodel.Row; |
| 11 | + |
| 12 | +import java.time.LocalDate; |
| 13 | +import java.time.LocalDateTime; |
| 14 | +import java.util.Date; |
| 15 | + |
| 16 | +/** |
| 17 | + * {@code ExcelCell} is the {@code Cell} wrapper class of the Apache POI library |
| 18 | + * @author Mirko Benincasa |
| 19 | + * @since 0.3.0 |
| 20 | + */ |
| 21 | +@AllArgsConstructor |
| 22 | +@Getter |
| 23 | +@EqualsAndHashCode |
| 24 | +public class ExcelCell { |
| 25 | + |
| 26 | + /** |
| 27 | + * This object refers to the Apache POI Library {@code Cell} |
| 28 | + */ |
| 29 | + private Cell cell; |
| 30 | + |
| 31 | + /** |
| 32 | + * The index of the Cell in the Row |
| 33 | + */ |
| 34 | + private Integer index; |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the Row to which it belongs |
| 38 | + * @return A ExcelRow |
| 39 | + */ |
| 40 | + public ExcelRow getRow() { |
| 41 | + Row row = this.cell.getRow(); |
| 42 | + return new ExcelRow(row, row.getRowNum()); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Read the value written inside the Cell |
| 47 | + * @param type The class type of the object written to the Cell |
| 48 | + * @return The value written in the Cell |
| 49 | + * @throws ReadValueException If an error occurs while reading |
| 50 | + */ |
| 51 | + public Object readValue(Class<?> type) throws ReadValueException { |
| 52 | + Object val; |
| 53 | + switch (this.cell.getCellType()) { |
| 54 | + case BOOLEAN -> val = this.cell.getBooleanCellValue(); |
| 55 | + case STRING -> val = this.cell.getStringCellValue(); |
| 56 | + case NUMERIC -> { |
| 57 | + if (Integer.class.equals(type)) { |
| 58 | + val = (int) this.cell.getNumericCellValue(); |
| 59 | + } else if (Double.class.equals(type)) { |
| 60 | + val = this.cell.getNumericCellValue(); |
| 61 | + } else if (Long.class.equals(type)) { |
| 62 | + val = (long) this.cell.getNumericCellValue(); |
| 63 | + } else if (Date.class.equals(type)) { |
| 64 | + val = this.cell.getDateCellValue(); |
| 65 | + } else if (LocalDateTime.class.equals(type)) { |
| 66 | + val = this.cell.getLocalDateTimeCellValue(); |
| 67 | + } else if (LocalDate.class.equals(type)) { |
| 68 | + val = this.cell.getLocalDateTimeCellValue().toLocalDate(); |
| 69 | + } else { |
| 70 | + throw new ReadValueException("This numeric type is not supported: " + type); |
| 71 | + } |
| 72 | + } |
| 73 | + case FORMULA -> { |
| 74 | + ExcelWorkbook excelWorkbook = this.getRow().getSheet().getWorkbook(); |
| 75 | + FormulaEvaluator formulaEvaluator = excelWorkbook.getFormulaEvaluator(); |
| 76 | + if (Boolean.class.equals(type)) { |
| 77 | + val = formulaEvaluator.evaluate(this.cell).getBooleanValue(); |
| 78 | + } else { |
| 79 | + val = this.cell.getCellFormula(); |
| 80 | + } |
| 81 | + } |
| 82 | + default -> throw new ReadValueException("Cell type not supported"); |
| 83 | + } |
| 84 | + |
| 85 | + return val; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Writes inside the cell |
| 90 | + * @param val The value to write in the Cell |
| 91 | + */ |
| 92 | + public void writeValue(Object val) { |
| 93 | + if (val instanceof Integer || val instanceof Long) { |
| 94 | + this.formatStyle((short) 1); |
| 95 | + this.cell.setCellValue(Integer.parseInt(String.valueOf(val))); |
| 96 | + } else if (val instanceof Double) { |
| 97 | + this.formatStyle((short) 4); |
| 98 | + this.cell.setCellValue(Double.parseDouble(String.valueOf(val))); |
| 99 | + } else if (val instanceof Date) { |
| 100 | + this.formatStyle((short) 22); |
| 101 | + this.cell.setCellValue((Date) val); |
| 102 | + } else if (val instanceof LocalDate) { |
| 103 | + this.formatStyle((short) 14); |
| 104 | + this.cell.setCellValue((LocalDate) val); |
| 105 | + } else if (val instanceof LocalDateTime) { |
| 106 | + this.formatStyle((short) 22); |
| 107 | + this.cell.setCellValue((LocalDateTime) val); |
| 108 | + } else if (val instanceof Boolean) { |
| 109 | + cell.setCellValue((Boolean) val); |
| 110 | + } else { |
| 111 | + cell.setCellValue(String.valueOf(val)); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Format text according to the pattern provided |
| 117 | + * @param dataFormat The Apache POI library CellStyle dataFormat |
| 118 | + */ |
| 119 | + public void formatStyle(short dataFormat) { |
| 120 | + ExcelWorkbook excelWorkbook = this.getRow().getSheet().getWorkbook(); |
| 121 | + CellStyle newCellStyle = excelWorkbook.getWorkbook().createCellStyle(); |
| 122 | + newCellStyle.cloneStyleFrom(this.cell.getCellStyle()); |
| 123 | + newCellStyle.setDataFormat(dataFormat); |
| 124 | + this.cell.setCellStyle(newCellStyle); |
| 125 | + } |
| 126 | +} |
0 commit comments