Skip to content

Commit 7a55651

Browse files
committed
Removed static methods that create an ExcelSheet
1 parent 1a3dad9 commit 7a55651

File tree

7 files changed

+50
-102
lines changed

7 files changed

+50
-102
lines changed

src/main/java/model/ExcelSheet.java

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

3-
import exceptions.SheetAlreadyExistsException;
43
import lombok.AllArgsConstructor;
54
import lombok.EqualsAndHashCode;
65
import lombok.Getter;
76
import org.apache.poi.ss.usermodel.Cell;
87
import org.apache.poi.ss.usermodel.Row;
98
import org.apache.poi.ss.usermodel.Sheet;
10-
import org.apache.poi.ss.usermodel.Workbook;
119

1210
import java.util.LinkedList;
1311
import java.util.List;
@@ -37,36 +35,6 @@ public class ExcelSheet {
3735
*/
3836
private String name;
3937

40-
/**
41-
* Create a new Sheet
42-
* @param excelWorkbook The Workbook where to create the Sheet
43-
* @return A ExcelSheet
44-
* @throws SheetAlreadyExistsException If a Sheet with that name already exists
45-
*/
46-
public static ExcelSheet create(ExcelWorkbook excelWorkbook) throws SheetAlreadyExistsException {
47-
return create(excelWorkbook, null);
48-
}
49-
50-
/**
51-
* Create a new Sheet
52-
* @param excelWorkbook The Workbook where to create the Sheet
53-
* @param sheetName The name of the sheet
54-
* @return A ExcelSheet
55-
* @throws SheetAlreadyExistsException If a Sheet with that name already exists
56-
*/
57-
public static ExcelSheet create(ExcelWorkbook excelWorkbook, String sheetName) throws SheetAlreadyExistsException {
58-
Workbook workbook = excelWorkbook.getWorkbook();
59-
Sheet sheet;
60-
try {
61-
sheet = (sheetName == null || sheetName.isEmpty())
62-
? workbook.createSheet()
63-
: workbook.createSheet(sheetName);
64-
} catch (IllegalArgumentException e) {
65-
throw new SheetAlreadyExistsException(e.getMessage(), e.getCause());
66-
}
67-
return new ExcelSheet(sheet, workbook.getSheetIndex(sheet), sheet.getSheetName());
68-
}
69-
7038
/**
7139
* Returns the Workbook to which it belongs
7240
* @return A ExcelWorkbook

src/main/java/model/ExcelWorkbook.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ public List<ExcelSheet> getSheets() {
218218
return excelSheets;
219219
}
220220

221+
/**
222+
* Create a new Sheet inside the Workbook
223+
* @return The newly created Sheet
224+
*/
225+
public ExcelSheet createSheet() {
226+
Sheet sheet = this.workbook.createSheet();
227+
return new ExcelSheet(sheet, this.workbook.getSheetIndex(sheet), sheet.getSheetName());
228+
}
229+
221230
/**
222231
* Create a new Sheet inside the Workbook
223232
* @param sheetName The name of the sheet to create
@@ -279,7 +288,7 @@ public ExcelSheet getSheetOrCreate(String sheetName) {
279288
try {
280289
return this.getSheet(sheetName);
281290
} catch (SheetNotFoundException e) {
282-
return ExcelSheet.create(this, sheetName);
291+
return this.createSheet(sheetName);
283292
}
284293
}
285294

src/main/java/samples/writeExcelSample/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void main(String[] args) {
1919

2020
try {
2121
ExcelWorkbook excelWorkbook = ExcelWorkbook.create(FilenameUtils.getExtension(testFile.getName()));
22-
ExcelSheet excelSheet = ExcelSheet.create(excelWorkbook);
22+
ExcelSheet excelSheet = excelWorkbook.createSheet("TEST");
2323
ExcelRow excelRow = excelSheet.createRow(0);
2424
ExcelCell excelCell = excelRow.createCell(0);
2525
excelCell.writeValue("Rossi");

src/main/java/tools/Converter.java

Lines changed: 26 additions & 49 deletions
Large diffs are not rendered by default.

src/main/java/tools/SheetUtility.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ public static Sheet create(File file, String sheetName) throws ExtensionNotValid
141141
/* Open file excel */
142142
FileInputStream fileInputStream = new FileInputStream(file);
143143
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(fileInputStream, extension);
144-
ExcelSheet excelSheet = ExcelSheet.create(excelWorkbook, sheetName);
144+
ExcelSheet excelSheet = (sheetName == null || sheetName.isEmpty())
145+
? excelWorkbook.createSheet()
146+
: excelWorkbook.createSheet(sheetName);
145147

146148
return excelSheet.getSheet();
147149
}
@@ -165,7 +167,9 @@ public static Sheet create(Workbook workbook) throws SheetAlreadyExistsException
165167
*/
166168
public static Sheet create(Workbook workbook, String sheetName) throws SheetAlreadyExistsException {
167169
ExcelWorkbook excelWorkbook = new ExcelWorkbook(workbook);
168-
ExcelSheet excelSheet = ExcelSheet.create(excelWorkbook, sheetName);
170+
ExcelSheet excelSheet = (sheetName == null || sheetName.isEmpty())
171+
? excelWorkbook.createSheet()
172+
: excelWorkbook.createSheet(sheetName);
169173
return excelSheet.getSheet();
170174
}
171175

src/test/java/model/ExcelSheetTest.java

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

33
import exceptions.ExtensionNotValidException;
44
import exceptions.OpenWorkbookException;
5-
import exceptions.SheetAlreadyExistsException;
65
import exceptions.SheetNotFoundException;
76
import org.junit.jupiter.api.Assertions;
87
import org.junit.jupiter.api.Test;
@@ -15,22 +14,6 @@ public class ExcelSheetTest {
1514

1615
private final File excelFile = new File("./src/test/resources/employee.xlsx");
1716

18-
@Test
19-
void create() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetAlreadyExistsException {
20-
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
21-
ExcelSheet excelSheet = ExcelSheet.create(excelWorkbook);
22-
Assertions.assertNotNull(excelSheet.getSheet());
23-
}
24-
25-
@Test
26-
void testCreate() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetAlreadyExistsException {
27-
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
28-
String sheetName = "Admin";
29-
ExcelSheet excelSheet = ExcelSheet.create(excelWorkbook, sheetName);
30-
Assertions.assertNotNull(excelSheet.getSheet());
31-
Assertions.assertEquals(true, excelWorkbook.isSheetPresent(sheetName));
32-
}
33-
3417
@Test
3518
void getWorkbook() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
3619
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);

src/test/java/model/ExcelWorkbookTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ void createSheet() throws OpenWorkbookException, ExtensionNotValidException, IOE
115115
Assertions.assertEquals("Test", excelSheet.getName());
116116
}
117117

118+
@Test
119+
void testCreateSheet() throws OpenWorkbookException, ExtensionNotValidException, IOException {
120+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
121+
ExcelSheet excelSheet = excelWorkbook.createSheet();
122+
Assertions.assertNotNull(excelSheet.getSheet());
123+
}
124+
118125
@Test
119126
void getSheets() throws OpenWorkbookException, ExtensionNotValidException, IOException {
120127
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);

0 commit comments

Comments
 (0)