Skip to content

Commit 9123ac9

Browse files
committed
Some methods have been added, especially the methods for reading and writing to a cell.
1 parent 45a5c80 commit 9123ac9

File tree

10 files changed

+275
-0
lines changed

10 files changed

+275
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package exceptions;
2+
3+
/**
4+
* This exception signals that there was an error reading a cell value
5+
* @author Mirko Benincasa
6+
* @since 0.3.0
7+
*/
8+
public class ReadValueException extends Exception {
9+
10+
/**
11+
* Constructs an {@code ReadValueException} with {@code null}
12+
* as its error detail message.
13+
*/
14+
public ReadValueException() {
15+
super();
16+
}
17+
18+
/**
19+
* Constructs an {@code ReadValueException} with the specified detail message.
20+
*
21+
* @param message
22+
* The detail message (which is saved for later retrieval
23+
* by the {@link #getMessage()} method)
24+
*/
25+
public ReadValueException(String message) {
26+
super(message);
27+
}
28+
29+
/**
30+
* Constructs an {@code ReadValueException} with the specified detail message
31+
* and cause.
32+
*
33+
* <p> Note that the detail message associated with {@code cause} is
34+
* <i>not</i> automatically incorporated into this exception's detail
35+
* message.
36+
*
37+
* @param message
38+
* The detail message (which is saved for later retrieval
39+
* by the {@link #getMessage()} method)
40+
*
41+
* @param cause
42+
* The cause (which is saved for later retrieval by the
43+
* {@link #getCause()} method). (A null value is permitted,
44+
* and indicates that the cause is nonexistent or unknown.)
45+
*/
46+
public ReadValueException(String message, Throwable cause) {
47+
super(message, cause);
48+
}
49+
}

src/main/java/model/ExcelCell.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package model;
22

3+
import exceptions.ReadValueException;
34
import lombok.AllArgsConstructor;
45
import lombok.EqualsAndHashCode;
56
import lombok.Getter;
67
import org.apache.poi.ss.usermodel.Cell;
8+
import org.apache.poi.ss.usermodel.CellStyle;
9+
import org.apache.poi.ss.usermodel.FormulaEvaluator;
710
import org.apache.poi.ss.usermodel.Row;
811

12+
import java.time.LocalDate;
13+
import java.time.LocalDateTime;
14+
import java.util.Date;
15+
916
@AllArgsConstructor
1017
@Getter
1118
@EqualsAndHashCode
@@ -18,4 +25,72 @@ public ExcelRow getRow() {
1825
Row row = this.cell.getRow();
1926
return new ExcelRow(row, row.getRowNum());
2027
}
28+
29+
public Object readValue(Class<?> type) throws ReadValueException {
30+
Object val;
31+
switch (this.cell.getCellType()) {
32+
case BOOLEAN -> val = this.cell.getBooleanCellValue();
33+
case STRING -> val = this.cell.getStringCellValue();
34+
case NUMERIC -> {
35+
if (Integer.class.equals(type)) {
36+
val = (int) this.cell.getNumericCellValue();
37+
} else if (Double.class.equals(type)) {
38+
val = this.cell.getNumericCellValue();
39+
} else if (Long.class.equals(type)) {
40+
val = (long) this.cell.getNumericCellValue();
41+
} else if (Date.class.equals(type)) {
42+
val = this.cell.getDateCellValue();
43+
} else if (LocalDateTime.class.equals(type)) {
44+
val = this.cell.getLocalDateTimeCellValue();
45+
} else if (LocalDate.class.equals(type)) {
46+
val = this.cell.getLocalDateTimeCellValue().toLocalDate();
47+
} else {
48+
throw new ReadValueException("This numeric type is not supported: " + type);
49+
}
50+
}
51+
case FORMULA -> {
52+
ExcelWorkbook excelWorkbook = this.getRow().getSheet().getWorkbook();
53+
FormulaEvaluator formulaEvaluator = excelWorkbook.getFormulaEvaluator();
54+
if (Boolean.class.equals(type)) {
55+
val = formulaEvaluator.evaluate(this.cell).getBooleanValue();
56+
} else {
57+
val = this.cell.getCellFormula();
58+
}
59+
}
60+
default -> throw new ReadValueException("Cell type not supported");
61+
}
62+
63+
return val;
64+
}
65+
66+
public void writeValue(Object val) {
67+
if (val instanceof Integer || val instanceof Long) {
68+
this.formatStyle((short) 1);
69+
this.cell.setCellValue(Integer.parseInt(String.valueOf(val)));
70+
} else if (val instanceof Double) {
71+
this.formatStyle((short) 4);
72+
this.cell.setCellValue(Double.parseDouble(String.valueOf(val)));
73+
} else if (val instanceof Date) {
74+
this.formatStyle((short) 22);
75+
this.cell.setCellValue((Date) val);
76+
} else if (val instanceof LocalDate) {
77+
this.formatStyle((short) 14);
78+
this.cell.setCellValue((LocalDate) val);
79+
} else if (val instanceof LocalDateTime) {
80+
this.formatStyle((short) 22);
81+
this.cell.setCellValue((LocalDateTime) val);
82+
} else if (val instanceof Boolean) {
83+
cell.setCellValue((Boolean) val);
84+
} else {
85+
cell.setCellValue(String.valueOf(val));
86+
}
87+
}
88+
89+
public void formatStyle(short dataFormat) {
90+
ExcelWorkbook excelWorkbook = this.getRow().getSheet().getWorkbook();
91+
CellStyle newCellStyle = excelWorkbook.getWorkbook().createCellStyle();
92+
newCellStyle.cloneStyleFrom(this.cell.getCellStyle());
93+
newCellStyle.setDataFormat(dataFormat);
94+
this.cell.setCellStyle(newCellStyle);
95+
}
2196
}

src/main/java/model/ExcelRow.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public ExcelSheet getSheet() {
3636
return new ExcelSheet(sheet, excelWorkbook.getSheet(sheetName).getIndex(), sheetName);
3737
}
3838

39+
public ExcelCell createCell(Integer index) {
40+
return new ExcelCell(this.row.createCell(index), index);
41+
}
42+
3943
public Integer getLastColumnIndex() {
4044
return this.row.getLastCellNum() - 1;
4145
}

src/main/java/model/ExcelSheet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public List<ExcelRow> getRows() {
5151
return excelRows;
5252
}
5353

54+
public ExcelRow createRow(Integer index) {
55+
return new ExcelRow(this.sheet.createRow(index), index);
56+
}
57+
5458
public Integer getLastRowIndex() {
5559
return this.sheet.getLastRowNum();
5660
}

src/main/java/model/ExcelWorkbook.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
1414
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
1515
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
16+
import org.apache.poi.ss.usermodel.FormulaEvaluator;
1617
import org.apache.poi.ss.usermodel.Sheet;
1718
import org.apache.poi.ss.usermodel.Workbook;
1819
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -130,6 +131,11 @@ public List<ExcelSheet> getSheets() {
130131
return excelSheets;
131132
}
132133

134+
public ExcelSheet createSheet(String sheetName) {
135+
Sheet sheet = this.workbook.createSheet(sheetName);
136+
return new ExcelSheet(sheet, this.workbook.getSheetIndex(sheet), sheet.getSheetName());
137+
}
138+
133139
public ExcelSheet getSheet() throws SheetNotFoundException {
134140
return this.getSheet(0);
135141
}
@@ -186,4 +192,8 @@ public Boolean isSheetNull(Integer index) {
186192
Optional<ExcelSheet> excelSheet = excelSheets.stream().filter(s -> Objects.equals(s.getIndex(), index)).findAny();
187193
return excelSheet.isEmpty();
188194
}
195+
196+
public FormulaEvaluator getFormulaEvaluator() {
197+
return this.workbook.getCreationHelper().createFormulaEvaluator();
198+
}
189199
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package samples.writeExcelSample;
2+
3+
import model.ExcelCell;
4+
import model.ExcelRow;
5+
import model.ExcelSheet;
6+
import model.ExcelWorkbook;
7+
import org.apache.commons.io.FilenameUtils;
8+
9+
import java.io.File;
10+
import java.io.FileOutputStream;
11+
import java.time.LocalDate;
12+
import java.time.LocalDateTime;
13+
import java.util.Date;
14+
15+
public class Main {
16+
17+
public static void main(String[] args) {
18+
File testFile = new File("./src/main/resources/test.xlsx");
19+
20+
try {
21+
ExcelWorkbook excelWorkbook = ExcelWorkbook.create(FilenameUtils.getExtension(testFile.getName()));
22+
ExcelSheet excelSheet = ExcelSheet.create(excelWorkbook);
23+
ExcelRow excelRow = excelSheet.createRow(0);
24+
ExcelCell excelCell = excelRow.createCell(0);
25+
excelCell.writeValue("Rossi");
26+
ExcelCell excelCell1 = excelRow.createCell(1);
27+
excelCell1.writeValue("Mario");
28+
ExcelCell excelCell2 = excelRow.createCell(2);
29+
excelCell2.writeValue(23);
30+
ExcelCell excelCell3 = excelRow.createCell(3);
31+
excelCell3.writeValue(LocalDateTime.now());
32+
ExcelCell excelCell4 = excelRow.createCell(4);
33+
excelCell4.writeValue(LocalDate.now());
34+
ExcelCell excelCell5 = excelRow.createCell(5);
35+
excelCell5.writeValue(new Date());
36+
ExcelCell excelCell6 = excelRow.createCell(6);
37+
excelCell6.writeValue(21.12);
38+
ExcelCell excelCell7 = excelRow.createCell(7);
39+
excelCell7.writeValue(true);
40+
41+
FileOutputStream fileOutputStream = new FileOutputStream(testFile);
42+
excelWorkbook.getWorkbook().write(fileOutputStream);
43+
excelWorkbook.close(fileOutputStream);
44+
} catch (Exception e) {
45+
throw new RuntimeException(e.getMessage());
46+
}
47+
48+
}
49+
}

src/test/java/model/ExcelCellTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import exceptions.ExtensionNotValidException;
44
import exceptions.OpenWorkbookException;
5+
import exceptions.ReadValueException;
56
import exceptions.SheetNotFoundException;
67
import org.junit.jupiter.api.Assertions;
78
import org.junit.jupiter.api.Test;
89

910
import java.io.File;
1011
import java.io.IOException;
12+
import java.time.LocalDate;
13+
import java.time.LocalDateTime;
14+
import java.util.Date;
15+
import java.util.List;
1116

1217
class ExcelCellTest {
1318

@@ -22,4 +27,50 @@ void getRow() throws OpenWorkbookException, ExtensionNotValidException, IOExcept
2227
ExcelRow excelRow1 = excelCell.getRow();
2328
Assertions.assertEquals(excelRow, excelRow1);
2429
}
30+
31+
@Test
32+
void readValue() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, ReadValueException {
33+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
34+
ExcelSheet excelSheet = excelWorkbook.getSheet();
35+
ExcelRow excelRow = excelSheet.getRows().get(1);
36+
List<ExcelCell> excelCells = excelRow.getCells();
37+
Assertions.assertEquals("Rossi", excelCells.get(0).readValue(String.class));
38+
Assertions.assertEquals("Mario", excelCells.get(1).readValue(String.class));
39+
Assertions.assertEquals(25, excelCells.get(2).readValue(Integer.class));
40+
Assertions.assertEquals(LocalDate.of(1987, 5, 22), excelCells.get(3).readValue(LocalDate.class));
41+
Assertions.assertNotNull(excelCells.get(4).readValue(Date.class));
42+
Assertions.assertEquals(28000.00, excelCells.get(5).readValue(Double.class));
43+
Assertions.assertEquals(LocalDateTime.of(2023, 2, 11, 12, 35, 55, 603000000), excelCells.get(6).readValue(LocalDateTime.class));
44+
Assertions.assertEquals(true, excelCells.get(7).readValue(Boolean.class));
45+
}
46+
47+
@Test
48+
void writeValue() throws OpenWorkbookException, ExtensionNotValidException, IOException, ReadValueException {
49+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
50+
ExcelSheet excelSheet = excelWorkbook.getSheetOrCreate("TestWrite");
51+
ExcelRow excelRow = excelSheet.createRow(0);
52+
ExcelCell excelCell = excelRow.createCell(0);
53+
excelCell.writeValue("Text");
54+
ExcelCell excelCell1 = excelRow.createCell(1);
55+
excelCell1.writeValue(21);
56+
ExcelCell excelCell2 = excelRow.createCell(2);
57+
LocalDateTime localDateTime = LocalDateTime.of(2021, 1, 1, 21, 21, 21, 0);
58+
excelCell2.writeValue(localDateTime);
59+
ExcelCell excelCell3 = excelRow.createCell(3);
60+
excelCell3.writeValue(false);
61+
Assertions.assertEquals("Text", excelCell.readValue(String.class));
62+
Assertions.assertEquals(21, excelCell1.readValue(Integer.class));
63+
Assertions.assertEquals(localDateTime, excelCell2.readValue(LocalDateTime.class));
64+
Assertions.assertEquals(false, excelCell3.readValue(Boolean.class));
65+
}
66+
67+
@Test
68+
void formatStyle() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
69+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
70+
ExcelSheet excelSheet = excelWorkbook.getSheet();
71+
ExcelRow excelRow = excelSheet.getRows().get(0);
72+
ExcelCell excelCell = excelRow.getCells().get(0);
73+
excelCell.formatStyle((short) 1);
74+
Assertions.assertEquals((short) 1, excelCell.getCell().getCellStyle().getDataFormat());
75+
}
2576
}

src/test/java/model/ExcelRowTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ void getSheet() throws OpenWorkbookException, ExtensionNotValidException, IOExce
3535
Assertions.assertEquals(excelSheet, excelSheet1);
3636
}
3737

38+
@Test
39+
void createCell() throws OpenWorkbookException, ExtensionNotValidException, IOException {
40+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
41+
ExcelSheet excelSheet = excelWorkbook.getSheetOrCreate("TestWrite");
42+
ExcelRow excelRow = excelSheet.createRow(0);
43+
ExcelCell excelCell = excelRow.createCell(0);
44+
Assertions.assertNotNull(excelCell.getCell());
45+
Assertions.assertEquals(0, excelCell.getIndex());
46+
}
47+
3848
@Test
3949
void getLastColumnIndex() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
4050
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);

src/test/java/model/ExcelSheetTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ void getRows() throws OpenWorkbookException, ExtensionNotValidException, IOExcep
4747
Assertions.assertEquals(3, excelRows.size());
4848
}
4949

50+
@Test
51+
void createRow() throws OpenWorkbookException, ExtensionNotValidException, IOException {
52+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
53+
ExcelSheet excelSheet = excelWorkbook.getSheetOrCreate("TestWrite");
54+
ExcelRow excelRow = excelSheet.createRow(0);
55+
Assertions.assertNotNull(excelRow.getRow());
56+
Assertions.assertEquals(0, excelRow.getIndex());
57+
}
58+
5059
@Test
5160
void getLastRowIndex() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
5261
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);

src/test/java/model/ExcelWorkbookTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ void length() throws OpenWorkbookException, ExtensionNotValidException, IOExcept
107107
Assertions.assertEquals(2, excelWorkbook.length());
108108
}
109109

110+
@Test
111+
void createSheet() throws OpenWorkbookException, ExtensionNotValidException, IOException {
112+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
113+
ExcelSheet excelSheet = excelWorkbook.createSheet("Test");
114+
Assertions.assertNotNull(excelSheet.getSheet());
115+
Assertions.assertEquals("Test", excelSheet.getName());
116+
}
117+
110118
@Test
111119
void getSheets() throws OpenWorkbookException, ExtensionNotValidException, IOException {
112120
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
@@ -162,4 +170,10 @@ void testIsSheetNull() throws OpenWorkbookException, ExtensionNotValidException,
162170
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
163171
Assertions.assertEquals(true, excelWorkbook.isSheetNull(3));
164172
}
173+
174+
@Test
175+
void getFormulaEvaluator() throws OpenWorkbookException, ExtensionNotValidException, IOException {
176+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
177+
Assertions.assertNotNull(excelWorkbook.getFormulaEvaluator());
178+
}
165179
}

0 commit comments

Comments
 (0)