Skip to content

Commit 7d500bd

Browse files
committed
WorkbookUtility has been deprecated and has been replaced by ExcelWorkbook
1 parent d9f8c67 commit 7d500bd

File tree

9 files changed

+224
-45
lines changed

9 files changed

+224
-45
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ Java 17 or above.
4545
<dependency>
4646
<groupId>org.junit.jupiter</groupId>
4747
<artifactId>junit-jupiter</artifactId>
48-
<version>RELEASE</version>
48+
<version>5.9.2</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.junit.platform</groupId>
53+
<artifactId>junit-platform-suite-engine</artifactId>
54+
<version>1.9.2</version>
4955
<scope>test</scope>
5056
</dependency>
5157
</dependencies>

src/main/java/model/ExcelWorkbook.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package model;
22

3+
import com.opencsv.CSVReader;
4+
import com.opencsv.CSVWriter;
35
import enums.Extension;
6+
import exceptions.ExtensionNotValidException;
47
import exceptions.OpenWorkbookException;
58
import lombok.AllArgsConstructor;
69
import lombok.Getter;
@@ -9,9 +12,9 @@
912
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
1013
import org.apache.poi.ss.usermodel.Workbook;
1114
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
15+
import tools.ExcelUtility;
1216

13-
import java.io.IOException;
14-
import java.io.InputStream;
17+
import java.io.*;
1518

1619
@AllArgsConstructor
1720
@Getter
@@ -38,7 +41,71 @@ public ExcelWorkbook(InputStream inputStream) throws OpenWorkbookException {
3841
}
3942
}
4043

44+
public static ExcelWorkbook open(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException {
45+
/* Check extension */
46+
String extension = ExcelUtility.checkExcelExtension(file.getName());
47+
48+
/* Open file input stream */
49+
FileInputStream fileInputStream = new FileInputStream(file);
50+
ExcelWorkbook excelWorkbook = open(fileInputStream, extension);
51+
52+
/* Close the stream before return */
53+
fileInputStream.close();
54+
return excelWorkbook;
55+
}
56+
57+
public static ExcelWorkbook open(InputStream inputStream, String extension) throws ExtensionNotValidException, IOException, OpenWorkbookException {
58+
/* Check the extension */
59+
if (!ExcelUtility.isValidExcelExtension(extension)) {
60+
throw new ExtensionNotValidException("Pass a file with the XLS or XLSX extension");
61+
}
62+
63+
return new ExcelWorkbook(inputStream);
64+
}
65+
66+
public static ExcelWorkbook create() {
67+
return create(Extension.XLSX);
68+
}
69+
70+
public static ExcelWorkbook create(String extension) throws ExtensionNotValidException {
71+
if (!ExcelUtility.isValidExcelExtension(extension)) {
72+
throw new ExtensionNotValidException("Pass a file with the XLS or XLSX extension");
73+
}
74+
return create(Extension.getExcelExtension(extension));
75+
}
76+
77+
public static ExcelWorkbook create(Extension extension) {
78+
return new ExcelWorkbook(extension);
79+
}
80+
4181
public void close() throws IOException {
4282
this.workbook.close();
4383
}
84+
85+
public void close(InputStream inputStream) throws IOException {
86+
this.workbook.close();
87+
inputStream.close();
88+
}
89+
90+
public void close(OutputStream outputStream) throws IOException {
91+
this.workbook.close();
92+
outputStream.close();
93+
}
94+
95+
public void close(OutputStream outputStream, InputStream inputStream) throws IOException {
96+
this.workbook.close();
97+
inputStream.close();
98+
outputStream.close();
99+
}
100+
101+
public void close(CSVWriter writer) throws IOException {
102+
this.workbook.close();
103+
writer.close();
104+
}
105+
106+
public void close(OutputStream outputStream, CSVReader reader) throws IOException {
107+
this.workbook.close();
108+
outputStream.close();
109+
reader.close();
110+
}
44111
}

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

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

3+
import model.ExcelWorkbook;
34
import org.apache.poi.ss.usermodel.Workbook;
45
import tools.SheetUtility;
5-
import tools.WorkbookUtility;
66

77
import java.io.File;
88
import java.util.List;
@@ -22,13 +22,15 @@ public static void main(String[] args) {
2222
String sheetName = SheetUtility.getName(file, 0);
2323
System.out.println("Sheet name: " + sheetName);
2424

25-
Workbook workbook = WorkbookUtility.open(file);
25+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
26+
Workbook workbook = excelWorkbook.getWorkbook();
2627
String sheetNameTest = "test";
2728
int sheetIndexTest = 0;
2829
Boolean isPresentByName = SheetUtility.isPresent(workbook, sheetNameTest);
2930
System.out.println("Sheet is: " + sheetNameTest + ". It is present: " + isPresentByName);
3031
Boolean isPresentByPosition = SheetUtility.isPresent(workbook, 0);
3132
System.out.println("Sheet index: " + sheetIndexTest + ". It is present: " + isPresentByPosition);
33+
excelWorkbook.close();
3234
} catch (Exception e) {
3335
System.err.println("There was an error. Check the console");
3436
throw new RuntimeException(e);

src/main/java/tools/Converter.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.opencsv.exceptions.CsvValidationException;
99
import enums.Extension;
1010
import exceptions.*;
11+
import model.ExcelWorkbook;
1112
import org.apache.commons.beanutils.PropertyUtils;
1213
import org.apache.commons.io.FilenameUtils;
1314
import org.apache.logging.log4j.LogManager;
@@ -28,7 +29,7 @@
2829
*/
2930
public class Converter {
3031

31-
private static Logger logger = LogManager.getLogger(Converter.class);
32+
private final static Logger logger = LogManager.getLogger(Converter.class);
3233

3334
/**
3435
* Convert a list of objects into an Excel file<p>
@@ -255,15 +256,16 @@ public static File objectsToExcel(List<?> objects, Class<?> clazz, String path,
255256
}
256257

257258
/* Create workbook and sheet */
258-
Workbook workbook = WorkbookUtility.create(extension);
259+
ExcelWorkbook excelWorkbook = ExcelWorkbook.create(extension);
260+
Workbook workbook = excelWorkbook.getWorkbook();
259261
objectsToExistingExcel(workbook, objects, clazz, writeHeader);
260262

261263
/* Write file */
262264
FileOutputStream fileOutputStream = new FileOutputStream(file);
263265
workbook.write(fileOutputStream);
264266

265267
/* Close file */
266-
WorkbookUtility.close(workbook, fileOutputStream);
268+
excelWorkbook.close(fileOutputStream);
267269

268270
return file;
269271
}
@@ -298,15 +300,16 @@ public static void objectsToExistingExcel(File file, List<?> objects, Class<?> c
298300
*/
299301
public static void objectsToExistingExcel(File file, List<?> objects, Class<?> clazz, Boolean writeHeader) throws OpenWorkbookException, ExtensionNotValidException, IOException, IllegalAccessException {
300302
/* Open workbook */
301-
Workbook workbook = WorkbookUtility.open(file);
303+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
304+
Workbook workbook = excelWorkbook.getWorkbook();
302305
objectsToExistingExcel(workbook, objects, clazz, writeHeader);
303306

304307
/* Write file */
305308
FileOutputStream fileOutputStream = new FileOutputStream(file);
306309
workbook.write(fileOutputStream);
307310

308311
/* Close file */
309-
WorkbookUtility.close(workbook, fileOutputStream);
312+
excelWorkbook.close(fileOutputStream);
310313
}
311314

312315
/**
@@ -392,7 +395,8 @@ public static List<?> excelToObjects(File file, Class<?> clazz) throws Extension
392395
*/
393396
public static List<?> excelToObjects(File file, Class<?> clazz, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException, SheetNotFoundException, HeaderNotPresentException {
394397
/* Open file excel */
395-
Workbook workbook = WorkbookUtility.open(file);
398+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
399+
Workbook workbook = excelWorkbook.getWorkbook();
396400
Sheet sheet = (sheetName == null || sheetName.isEmpty())
397401
? SheetUtility.get(workbook)
398402
: SheetUtility.get(workbook, sheetName);
@@ -414,7 +418,7 @@ public static List<?> excelToObjects(File file, Class<?> clazz, String sheetName
414418
}
415419

416420
/* Close file */
417-
WorkbookUtility.close(workbook);
421+
excelWorkbook.close();
418422

419423
return resultList;
420424
}
@@ -482,7 +486,8 @@ public static File excelToCsv(File fileInput, String path, String filename) thro
482486
*/
483487
public static File excelToCsv(File fileInput, String path, String filename, String sheetName) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException, FileAlreadyExistsException {
484488
/* Open file excel */
485-
Workbook workbook = WorkbookUtility.open(fileInput);
489+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(fileInput);
490+
Workbook workbook = excelWorkbook.getWorkbook();
486491
Sheet sheet = (sheetName == null || sheetName.isEmpty())
487492
? SheetUtility.get(workbook)
488493
: SheetUtility.get(workbook, sheetName);
@@ -509,7 +514,7 @@ public static File excelToCsv(File fileInput, String path, String filename, Stri
509514
}
510515

511516
/* Close file */
512-
WorkbookUtility.close(workbook, csvWriter);
517+
excelWorkbook.close(csvWriter);
513518

514519
return csvFile;
515520
}
@@ -589,15 +594,16 @@ public static File csvToExcel(File fileInput, String path, String filename, Exte
589594
}
590595

591596
/* Create workbook and sheet */
592-
Workbook workbook = WorkbookUtility.create(extension);
597+
ExcelWorkbook excelWorkbook = ExcelWorkbook.create(extension);
598+
Workbook workbook = excelWorkbook.getWorkbook();
593599
csvToExistingExcel(workbook, csvReader);
594600

595601
/* Write file */
596602
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
597603
workbook.write(fileOutputStream);
598604

599605
/* Close file */
600-
WorkbookUtility.close(workbook, fileOutputStream, csvReader);
606+
excelWorkbook.close(fileOutputStream, csvReader);
601607

602608
return outputFile;
603609
}
@@ -614,15 +620,16 @@ public static File csvToExcel(File fileInput, String path, String filename, Exte
614620
*/
615621
public static void csvToExistingExcel(File fileOutput, File fileInput) throws OpenWorkbookException, ExtensionNotValidException, IOException, CsvValidationException {
616622
/* Open workbook */
617-
Workbook workbook = WorkbookUtility.open(fileOutput);
623+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(fileOutput);
624+
Workbook workbook = excelWorkbook.getWorkbook();
618625
csvToExistingExcel(workbook, fileInput);
619626

620627
/* Write file */
621628
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
622629
workbook.write(fileOutputStream);
623630

624631
/* Close file */
625-
WorkbookUtility.close(workbook, fileOutputStream);
632+
excelWorkbook.close(fileOutputStream);
626633
}
627634

628635
/**
@@ -637,15 +644,16 @@ public static void csvToExistingExcel(File fileOutput, File fileInput) throws Op
637644
*/
638645
public static void csvToExistingExcel(File fileOutput, CSVReader csvReader) throws OpenWorkbookException, ExtensionNotValidException, IOException, CsvValidationException {
639646
/* Open workbook */
640-
Workbook workbook = WorkbookUtility.open(fileOutput);
647+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(fileOutput);
648+
Workbook workbook = excelWorkbook.getWorkbook();
641649
csvToExistingExcel(workbook, csvReader);
642650

643651
/* Write file */
644652
FileOutputStream fileOutputStream = new FileOutputStream(fileOutput);
645653
workbook.write(fileOutputStream);
646654

647655
/* Close file */
648-
WorkbookUtility.close(workbook, fileOutputStream, csvReader);
656+
excelWorkbook.close(fileOutputStream, csvReader);
649657
}
650658

651659
/**

src/main/java/tools/ExcelUtility.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import exceptions.ExtensionNotValidException;
55
import exceptions.OpenWorkbookException;
66
import exceptions.SheetNotFoundException;
7+
import model.ExcelWorkbook;
78
import org.apache.commons.io.FilenameUtils;
89
import org.apache.poi.ss.usermodel.Cell;
910
import org.apache.poi.ss.usermodel.Row;
@@ -46,7 +47,8 @@ public static List<Integer> countAllRowsOfAllSheets(File file) throws ExtensionN
4647
*/
4748
public static List<Integer> countAllRowsOfAllSheets(File file, Boolean alsoEmptyRows) throws ExtensionNotValidException, IOException, OpenWorkbookException {
4849
/* Open file excel */
49-
Workbook workbook = WorkbookUtility.open(file);
50+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
51+
Workbook workbook = excelWorkbook.getWorkbook();
5052

5153
List<Integer> values = new LinkedList<>();
5254
for (Sheet sheet : workbook) {
@@ -59,7 +61,7 @@ public static List<Integer> countAllRowsOfAllSheets(File file, Boolean alsoEmpty
5961
}
6062

6163
/* Close file */
62-
WorkbookUtility.close(workbook);
64+
excelWorkbook.close();
6365

6466
return values;
6567
}
@@ -92,7 +94,8 @@ public static Integer countAllRows(File file, String sheetName) throws OpenWorkb
9294
*/
9395
public static Integer countAllRows(File file, String sheetName, Boolean alsoEmptyRows) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
9496
/* Open file excel */
95-
Workbook workbook = WorkbookUtility.open(file);
97+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
98+
Workbook workbook = excelWorkbook.getWorkbook();
9699
Sheet sheet = (sheetName == null || sheetName.isEmpty())
97100
? SheetUtility.get(workbook)
98101
: SheetUtility.get(workbook, sheetName);
@@ -103,7 +106,7 @@ public static Integer countAllRows(File file, String sheetName, Boolean alsoEmpt
103106
: countOnlyRowsNotEmpty(sheet);
104107

105108
/* Close file */
106-
WorkbookUtility.close(workbook);
109+
excelWorkbook.close();
107110

108111
return numRows;
109112
}

0 commit comments

Comments
 (0)