Skip to content

Commit 0ae6fcd

Browse files
committed
I added some methods for ExcelRow and ExcelCell. I added equals and hashcode methods
1 parent 0f7d7a7 commit 0ae6fcd

File tree

8 files changed

+135
-4
lines changed

8 files changed

+135
-4
lines changed

src/main/java/model/ExcelCell.java

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

33
import lombok.AllArgsConstructor;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.Getter;
46
import org.apache.poi.ss.usermodel.Cell;
7+
import org.apache.poi.ss.usermodel.Row;
58

69
@AllArgsConstructor
10+
@Getter
11+
@EqualsAndHashCode
712
public class ExcelCell {
813

914
private Cell cell;
15+
private Integer index;
16+
17+
public ExcelRow getRow() {
18+
Row row = this.cell.getRow();
19+
return new ExcelRow(row, row.getRowNum());
20+
}
1021
}

src/main/java/model/ExcelRow.java

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

33
import lombok.AllArgsConstructor;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.Getter;
6+
import lombok.SneakyThrows;
7+
import org.apache.poi.ss.usermodel.Cell;
48
import org.apache.poi.ss.usermodel.Row;
9+
import org.apache.poi.ss.usermodel.Sheet;
10+
11+
import java.util.LinkedList;
12+
import java.util.List;
513

614
@AllArgsConstructor
15+
@Getter
16+
@EqualsAndHashCode
717
public class ExcelRow {
818

919
private Row row;
20+
private Integer index;
21+
22+
public List<ExcelCell> getCells() {
23+
List<ExcelCell> excelCells = new LinkedList<>();
24+
for (Cell cell : this.row) {
25+
excelCells.add(new ExcelCell(cell, cell.getColumnIndex()));
26+
}
27+
28+
return excelCells;
29+
}
30+
31+
@SneakyThrows
32+
public ExcelSheet getSheet() {
33+
Sheet sheet = this.row.getSheet();
34+
ExcelWorkbook excelWorkbook = new ExcelWorkbook(sheet.getWorkbook());
35+
String sheetName = sheet.getSheetName();
36+
return new ExcelSheet(sheet, excelWorkbook.getSheet(sheetName).getIndex(), sheetName);
37+
}
1038
}

src/main/java/model/ExcelSheet.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import exceptions.SheetAlreadyExistsException;
44
import lombok.AllArgsConstructor;
5+
import lombok.EqualsAndHashCode;
56
import lombok.Getter;
7+
import org.apache.poi.ss.usermodel.Row;
68
import org.apache.poi.ss.usermodel.Sheet;
79
import org.apache.poi.ss.usermodel.Workbook;
810

11+
import java.util.LinkedList;
12+
import java.util.List;
13+
914
@AllArgsConstructor
1015
@Getter
16+
@EqualsAndHashCode
1117
public class ExcelSheet {
1218

1319
private Sheet sheet;
@@ -31,7 +37,16 @@ public static ExcelSheet create(ExcelWorkbook excelWorkbook, String sheetName) t
3137
return new ExcelSheet(sheet, workbook.getSheetIndex(sheet), sheet.getSheetName());
3238
}
3339

34-
public ExcelWorkbook getExcelWorkbook() {
40+
public ExcelWorkbook getWorkbook() {
3541
return new ExcelWorkbook(this.getSheet().getWorkbook());
3642
}
43+
44+
public List<ExcelRow> getRows() {
45+
List<ExcelRow> excelRows = new LinkedList<>();
46+
for (Row row : this.sheet) {
47+
excelRows.add(new ExcelRow(row, row.getRowNum()));
48+
}
49+
50+
return excelRows;
51+
}
3752
}

src/main/java/model/ExcelWorkbook.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import exceptions.OpenWorkbookException;
88
import exceptions.SheetNotFoundException;
99
import lombok.AllArgsConstructor;
10+
import lombok.EqualsAndHashCode;
1011
import lombok.Getter;
1112
import lombok.SneakyThrows;
1213
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -25,6 +26,7 @@
2526

2627
@AllArgsConstructor
2728
@Getter
29+
@EqualsAndHashCode
2830
public class ExcelWorkbook {
2931

3032
private Workbook workbook;
@@ -128,6 +130,10 @@ public List<ExcelSheet> getSheets() {
128130
return excelSheets;
129131
}
130132

133+
public ExcelSheet getSheet() throws SheetNotFoundException {
134+
return this.getSheet(0);
135+
}
136+
131137
public ExcelSheet getSheet(Integer index) throws SheetNotFoundException {
132138
List<ExcelSheet> excelSheets = this.getSheets();
133139
for (ExcelSheet excelSheet : excelSheets) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package model;
2+
3+
import exceptions.ExtensionNotValidException;
4+
import exceptions.OpenWorkbookException;
5+
import exceptions.SheetNotFoundException;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
class ExcelCellTest {
13+
14+
private final File excelFile = new File("./src/test/resources/employee.xlsx");
15+
16+
@Test
17+
void getRow() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
18+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
19+
ExcelSheet excelSheet = excelWorkbook.getSheet();
20+
ExcelRow excelRow = excelSheet.getRows().get(0);
21+
ExcelCell excelCell = excelRow.getCells().get(0);
22+
ExcelRow excelRow1 = excelCell.getRow();
23+
Assertions.assertEquals(excelRow, excelRow1);
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package model;
2+
3+
import exceptions.ExtensionNotValidException;
4+
import exceptions.OpenWorkbookException;
5+
import exceptions.SheetNotFoundException;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.util.List;
12+
13+
class ExcelRowTest {
14+
15+
private final File excelFile = new File("./src/test/resources/employee.xlsx");
16+
17+
@Test
18+
void getCells() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
19+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
20+
ExcelSheet excelSheet = excelWorkbook.getSheet("Office");
21+
ExcelRow excelRow = excelSheet.getRows().get(0);
22+
List<ExcelCell> excelCells = excelRow.getCells();
23+
Assertions.assertEquals("CITY", excelCells.get(0).getCell().getStringCellValue());
24+
Assertions.assertEquals("PROVINCE", excelCells.get(1).getCell().getStringCellValue());
25+
Assertions.assertEquals("NUMBER OF STATIONS", excelCells.get(2).getCell().getStringCellValue());
26+
27+
}
28+
29+
@Test
30+
void getSheet() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
31+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
32+
ExcelSheet excelSheet = excelWorkbook.getSheet();
33+
ExcelRow excelRow = excelSheet.getRows().get(0);
34+
ExcelSheet excelSheet1 = excelRow.getSheet();
35+
Assertions.assertEquals(excelSheet, excelSheet1);
36+
}
37+
}

src/test/java/model/ExcelSheetTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ void testCreate() throws OpenWorkbookException, ExtensionNotValidException, IOEx
3131
}
3232

3333
@Test
34-
void getExcelWorkbook() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
34+
void getWorkbook() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
3535
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
3636
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
37-
ExcelWorkbook excelWorkbook1 = excelSheet.getExcelWorkbook();
37+
ExcelWorkbook excelWorkbook1 = excelSheet.getWorkbook();
3838
Assertions.assertNotNull(excelWorkbook1.getWorkbook());
3939
}
40+
41+
@Test
42+
void getRows() {
43+
}
4044
}

src/test/java/model/ExcelWorkbookTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ void testGetSheet() throws OpenWorkbookException, ExtensionNotValidException, IO
127127
Assertions.assertEquals("Employee", excelWorkbook.getSheet("Employee").getName());
128128
}
129129

130+
@Test
131+
void testGetSheet1() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
132+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
133+
Assertions.assertEquals("Employee", excelWorkbook.getSheet().getName());
134+
}
135+
130136
@Test
131137
void getSheetOrCreate() throws OpenWorkbookException, ExtensionNotValidException, IOException {
132138
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
@@ -156,5 +162,4 @@ void testIsSheetNull() throws OpenWorkbookException, ExtensionNotValidException,
156162
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
157163
Assertions.assertEquals(true, excelWorkbook.isSheetNull(3));
158164
}
159-
160165
}

0 commit comments

Comments
 (0)