Skip to content

Commit 0f7d7a7

Browse files
committed
SheetUtility has been deprecated and has been replaced by ExcelSheet.
1 parent 7d500bd commit 0f7d7a7

File tree

11 files changed

+404
-141
lines changed

11 files changed

+404
-141
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 you are trying to insert a sheet into a workbook that already contains that name
5+
* @author Mirko Benincasa
6+
* @since 0.3.0
7+
*/
8+
public class SheetAlreadyExistsException extends Exception {
9+
10+
/**
11+
* Constructs an {@code SheetAlreadyExistsException} with {@code null}
12+
* as its error detail message.
13+
*/
14+
public SheetAlreadyExistsException() {
15+
super();
16+
}
17+
18+
/**
19+
* Constructs an {@code SheetAlreadyExistsException} 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 SheetAlreadyExistsException(String message) {
26+
super(message);
27+
}
28+
29+
/**
30+
* Constructs an {@code SheetAlreadyExistsException} 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 SheetAlreadyExistsException(String message, Throwable cause) {
47+
super(message, cause);
48+
}
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
package model;
22

3+
import exceptions.SheetAlreadyExistsException;
34
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
46
import org.apache.poi.ss.usermodel.Sheet;
7+
import org.apache.poi.ss.usermodel.Workbook;
58

69
@AllArgsConstructor
10+
@Getter
711
public class ExcelSheet {
812

913
private Sheet sheet;
14+
private Integer index;
15+
private String name;
16+
17+
public static ExcelSheet create(ExcelWorkbook excelWorkbook) throws SheetAlreadyExistsException {
18+
return create(excelWorkbook, null);
19+
}
20+
21+
public static ExcelSheet create(ExcelWorkbook excelWorkbook, String sheetName) throws SheetAlreadyExistsException {
22+
Workbook workbook = excelWorkbook.getWorkbook();
23+
Sheet sheet;
24+
try {
25+
sheet = (sheetName == null || sheetName.isEmpty())
26+
? workbook.createSheet()
27+
: workbook.createSheet(sheetName);
28+
} catch (IllegalArgumentException e) {
29+
throw new SheetAlreadyExistsException(e.getMessage(), e.getCause());
30+
}
31+
return new ExcelSheet(sheet, workbook.getSheetIndex(sheet), sheet.getSheetName());
32+
}
33+
34+
public ExcelWorkbook getExcelWorkbook() {
35+
return new ExcelWorkbook(this.getSheet().getWorkbook());
36+
}
1037
}

src/main/java/model/ExcelWorkbook.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55
import enums.Extension;
66
import exceptions.ExtensionNotValidException;
77
import exceptions.OpenWorkbookException;
8+
import exceptions.SheetNotFoundException;
89
import lombok.AllArgsConstructor;
910
import lombok.Getter;
11+
import lombok.SneakyThrows;
1012
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
1113
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
1214
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
15+
import org.apache.poi.ss.usermodel.Sheet;
1316
import org.apache.poi.ss.usermodel.Workbook;
1417
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1518
import tools.ExcelUtility;
1619

1720
import java.io.*;
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
import java.util.Objects;
24+
import java.util.Optional;
1825

1926
@AllArgsConstructor
2027
@Getter
@@ -108,4 +115,69 @@ public void close(OutputStream outputStream, CSVReader reader) throws IOExceptio
108115
outputStream.close();
109116
reader.close();
110117
}
118+
119+
public Integer length() {
120+
return this.workbook.getNumberOfSheets();
121+
}
122+
123+
public List<ExcelSheet> getSheets() {
124+
List<ExcelSheet> excelSheets = new LinkedList<>();
125+
for (Sheet sheet : this.workbook) {
126+
excelSheets.add(new ExcelSheet(sheet, this.workbook.getSheetIndex(sheet), sheet.getSheetName()));
127+
}
128+
return excelSheets;
129+
}
130+
131+
public ExcelSheet getSheet(Integer index) throws SheetNotFoundException {
132+
List<ExcelSheet> excelSheets = this.getSheets();
133+
for (ExcelSheet excelSheet : excelSheets) {
134+
if (Objects.equals(excelSheet.getIndex(), index))
135+
return excelSheet;
136+
}
137+
138+
throw new SheetNotFoundException("No sheet was found in the index: " + index);
139+
}
140+
141+
public ExcelSheet getSheet(String sheetName) throws SheetNotFoundException {
142+
List<ExcelSheet> excelSheets = this.getSheets();
143+
for (ExcelSheet excelSheet : excelSheets) {
144+
if (excelSheet.getName().equals(sheetName))
145+
return excelSheet;
146+
}
147+
148+
throw new SheetNotFoundException("No sheet was found with the name: " + sheetName);
149+
}
150+
151+
@SneakyThrows
152+
public ExcelSheet getSheetOrCreate(String sheetName) {
153+
try {
154+
return this.getSheet(sheetName);
155+
} catch (SheetNotFoundException e) {
156+
return ExcelSheet.create(this, sheetName);
157+
}
158+
}
159+
160+
public Boolean isSheetPresent(String sheetName) {
161+
List<ExcelSheet> excelSheets = this.getSheets();
162+
Optional<ExcelSheet> excelSheet = excelSheets.stream().filter(s -> s.getName().equals(sheetName)).findAny();
163+
return excelSheet.isPresent();
164+
}
165+
166+
public Boolean isSheetPresent(Integer index) {
167+
List<ExcelSheet> excelSheets = this.getSheets();
168+
Optional<ExcelSheet> excelSheet = excelSheets.stream().filter(s -> Objects.equals(s.getIndex(), index)).findAny();
169+
return excelSheet.isPresent();
170+
}
171+
172+
public Boolean isSheetNull(String sheetName) {
173+
List<ExcelSheet> excelSheets = this.getSheets();
174+
Optional<ExcelSheet> excelSheet = excelSheets.stream().filter(s -> s.getName().equals(sheetName)).findAny();
175+
return excelSheet.isEmpty();
176+
}
177+
178+
public Boolean isSheetNull(Integer index) {
179+
List<ExcelSheet> excelSheets = this.getSheets();
180+
Optional<ExcelSheet> excelSheet = excelSheets.stream().filter(s -> Objects.equals(s.getIndex(), index)).findAny();
181+
return excelSheet.isEmpty();
182+
}
111183
}

src/main/java/samples/sheetSample/Main.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package samples.sheetSample;
22

3+
import model.ExcelSheet;
34
import model.ExcelWorkbook;
4-
import org.apache.poi.ss.usermodel.Workbook;
5-
import tools.SheetUtility;
65

76
import java.io.File;
87
import java.util.List;
@@ -13,22 +12,21 @@ public static void main(String[] args) {
1312
File file = new File("./src/main/resources/employee.xlsx");
1413

1514
try {
16-
int totalSheets = SheetUtility.length(file);
15+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
16+
int totalSheets = excelWorkbook.length();
1717
System.out.println("Total: " + totalSheets);
18-
List<String> sheetnames = SheetUtility.getNames(file);
18+
List<String> sheetnames = excelWorkbook.getSheets().stream().map(ExcelSheet::getName).toList();
1919
System.out.println("Sheet names: " + sheetnames);
20-
int sheetIndex = SheetUtility.getIndex(file, "Employee");
20+
int sheetIndex = excelWorkbook.getSheet("Employee").getIndex();
2121
System.out.println("Sheet index: " + sheetIndex);
22-
String sheetName = SheetUtility.getName(file, 0);
22+
String sheetName = excelWorkbook.getSheet(0).getName();
2323
System.out.println("Sheet name: " + sheetName);
2424

25-
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
26-
Workbook workbook = excelWorkbook.getWorkbook();
2725
String sheetNameTest = "test";
2826
int sheetIndexTest = 0;
29-
Boolean isPresentByName = SheetUtility.isPresent(workbook, sheetNameTest);
27+
Boolean isPresentByName = excelWorkbook.isSheetPresent(sheetNameTest);
3028
System.out.println("Sheet is: " + sheetNameTest + ". It is present: " + isPresentByName);
31-
Boolean isPresentByPosition = SheetUtility.isPresent(workbook, 0);
29+
Boolean isPresentByPosition = excelWorkbook.isSheetPresent(0);
3230
System.out.println("Sheet index: " + sheetIndexTest + ". It is present: " + isPresentByPosition);
3331
excelWorkbook.close();
3432
} catch (Exception e) {

0 commit comments

Comments
 (0)