22
33import io .github .mbenincasa .javaexcelutils .enums .Extension ;
44import io .github .mbenincasa .javaexcelutils .exceptions .ExtensionNotValidException ;
5- import io .github .mbenincasa .javaexcelutils .exceptions .OpenWorkbookException ;
6- import io .github .mbenincasa .javaexcelutils .exceptions .SheetNotFoundException ;
7- import io .github .mbenincasa .javaexcelutils .model .excel .ExcelWorkbook ;
85import org .apache .commons .io .FilenameUtils ;
9- import org .apache .poi .ss .usermodel .Cell ;
10- import org .apache .poi .ss .usermodel .Row ;
11- import org .apache .poi .ss .usermodel .Sheet ;
12- import org .apache .poi .ss .usermodel .Workbook ;
13-
14- import java .io .File ;
15- import java .io .IOException ;
16- import java .util .LinkedList ;
17- import java .util .List ;
186
197/**
208 * {@code ExcelUtility} is the static class with the implementations of some utilities on Excel files
2311 */
2412public class ExcelUtility {
2513
26- /**
27- * Counts all rows in all sheets<p>
28- * If not specified, empty lines will also be included
29- * @deprecated since version 0.3.0
30- * @param file an Excel file
31- * @return A list with the number of rows present for each sheet
32- * @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
33- * @throws IOException If an I/O error occurs
34- * @throws OpenWorkbookException If an error occurred while opening the workbook
35- */
36- @ Deprecated
37- public static List <Integer > countAllRowsOfAllSheets (File file ) throws ExtensionNotValidException , IOException , OpenWorkbookException {
38- return countAllRowsOfAllSheets (file , true );
39- }
40-
41- /**
42- * * Counts all rows in all sheets
43- * @deprecated since version 0.3.0
44- * @param file an Excel file
45- * @param alsoEmptyRows if {@code true} then it will also count rows with all empty cells
46- * @return A list with the number of rows present for each sheet
47- * @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
48- * @throws IOException If an I/O error occurs
49- * @throws OpenWorkbookException If an error occurred while opening the workbook
50- */
51- @ Deprecated
52- public static List <Integer > countAllRowsOfAllSheets (File file , Boolean alsoEmptyRows ) throws ExtensionNotValidException , IOException , OpenWorkbookException {
53- /* Open file excel */
54- ExcelWorkbook excelWorkbook = ExcelWorkbook .open (file );
55- Workbook workbook = excelWorkbook .getWorkbook ();
56-
57- List <Integer > values = new LinkedList <>();
58- for (Sheet sheet : workbook ) {
59- if (alsoEmptyRows ) {
60- values .add (sheet .getLastRowNum () + 1 );
61- continue ;
62- }
63-
64- values .add (countOnlyRowsNotEmpty (sheet ));
65- }
66-
67- /* Close file */
68- excelWorkbook .close ();
69-
70- return values ;
71- }
72-
73- /**
74- * Counts all rows in a sheet<p>
75- * If not specified, empty lines will also be included
76- * @deprecated since version 0.3.0
77- * @param file An Excel file
78- * @param sheetName The name of the sheet to open
79- * @return A number that corresponds to all rows in the sheet
80- * @throws OpenWorkbookException If an error occurred while opening the workbook
81- * @throws SheetNotFoundException If the sheet to open is not found
82- * @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
83- * @throws IOException If an I/O error occurs
84- */
85- @ Deprecated
86- public static Integer countAllRows (File file , String sheetName ) throws OpenWorkbookException , SheetNotFoundException , ExtensionNotValidException , IOException {
87- return countAllRows (file , sheetName , true );
88- }
89-
90- /**
91- * Counts all rows in a sheet
92- * @deprecated since version 0.3.0
93- * @param file An Excel file
94- * @param sheetName The name of the sheet to open
95- * @param alsoEmptyRows if {@code true} then it will also count rows with all empty cells
96- * @return A number that corresponds to all rows in the sheet
97- * @throws OpenWorkbookException If an error occurred while opening the workbook
98- * @throws SheetNotFoundException If the sheet to open is not found
99- * @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
100- * @throws IOException If an I/O error occurs
101- */
102- @ Deprecated
103- public static Integer countAllRows (File file , String sheetName , Boolean alsoEmptyRows ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
104- /* Open file excel */
105- ExcelWorkbook excelWorkbook = ExcelWorkbook .open (file );
106- Sheet sheet = (sheetName == null || sheetName .isEmpty ())
107- ? excelWorkbook .getSheet (0 ).getSheet ()
108- : excelWorkbook .getSheet (sheetName ).getSheet ();
109-
110- /* Count all rows */
111- int numRows = alsoEmptyRows
112- ? sheet .getLastRowNum () + 1
113- : countOnlyRowsNotEmpty (sheet );
114-
115- /* Close file */
116- excelWorkbook .close ();
117-
118- return numRows ;
119- }
120-
12114 /**
12215 * Check if the extension is that of an Excel file
12316 * @param filename The name of the file with extension
@@ -140,148 +33,4 @@ public static String checkExcelExtension(String filename) throws ExtensionNotVal
14033 public static Boolean isValidExcelExtension (String extension ) {
14134 return extension .equalsIgnoreCase (Extension .XLS .getExt ()) || extension .equalsIgnoreCase (Extension .XLSX .getExt ());
14235 }
143-
144- /**
145- * This method is used to recover the position of the last row of the Sheet. Note the count starts at 1<p>
146- * By default, the first Sheet is chosen
147- * @deprecated since version 0.3.0
148- * @param file file An Excel file
149- * @return The position of the last row of the Sheet
150- * @throws OpenWorkbookException If an error occurred while opening the workbook
151- * @throws SheetNotFoundException If the sheet to open is not found
152- * @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
153- * @throws IOException If an I/O error occurs
154- */
155- @ Deprecated
156- public static Integer getIndexLastRow (File file ) throws OpenWorkbookException , SheetNotFoundException , ExtensionNotValidException , IOException {
157- return getIndexLastRow (file , null );
158- }
159-
160- /**
161- * This method is used to recover the position of the last row of the Sheet. Note the count starts at 1
162- * @deprecated since version 0.3.0
163- * @param file file An Excel file
164- * @param sheetName The name of the sheet to open
165- * @return The position of the last row of the Sheet
166- * @throws OpenWorkbookException If an error occurred while opening the workbook
167- * @throws SheetNotFoundException If the sheet to open is not found
168- * @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
169- * @throws IOException If an I/O error occurs
170- */
171- @ Deprecated
172- public static Integer getIndexLastRow (File file , String sheetName ) throws OpenWorkbookException , ExtensionNotValidException , IOException , SheetNotFoundException {
173- /* Open file excel */
174- ExcelWorkbook excelWorkbook = ExcelWorkbook .open (file );
175- Sheet sheet = (sheetName == null || sheetName .isEmpty ())
176- ? excelWorkbook .getSheet (0 ).getSheet ()
177- : excelWorkbook .getSheet (sheetName ).getSheet ();
178- return sheet .getLastRowNum () + 1 ;
179- }
180-
181- /**
182- * This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
183- * By default, the first sheet and the first row are chosen
184- * @deprecated since version 0.3.0
185- * @param file file An Excel file
186- * @return The position of the last column of the chosen row
187- * @throws OpenWorkbookException If an error occurred while opening the workbook
188- * @throws SheetNotFoundException If the sheet to open is not found
189- * @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
190- * @throws IOException If an I/O error occurs
191- */
192- @ Deprecated
193- public static Integer getIndexLastColumn (File file ) throws OpenWorkbookException , SheetNotFoundException , ExtensionNotValidException , IOException {
194- return getIndexLastColumn (file , null , 0 );
195- }
196-
197- /**
198- * This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
199- * By default, the first row is chosen
200- * @deprecated since version 0.3.0
201- * @param file file An Excel file
202- * @param sheetName The name of the sheet to open
203- * @return The position of the last column of the chosen row
204- * @throws OpenWorkbookException If an error occurred while opening the workbook
205- * @throws SheetNotFoundException If the sheet to open is not found
206- * @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
207- * @throws IOException If an I/O error occurs
208- */
209- @ Deprecated
210- public static Integer getIndexLastColumn (File file , String sheetName ) throws OpenWorkbookException , SheetNotFoundException , ExtensionNotValidException , IOException {
211- return getIndexLastColumn (file , sheetName , 0 );
212- }
213-
214- /**
215- * This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
216- * By default, the first sheet is chosen
217- * @deprecated since version 0.3.0
218- * @param file file An Excel file
219- * @param indexRow the row index
220- * @return The position of the last column of the chosen row
221- * @throws OpenWorkbookException If an error occurred while opening the workbook
222- * @throws SheetNotFoundException If the sheet to open is not found
223- * @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
224- * @throws IOException If an I/O error occurs
225- */
226- @ Deprecated
227- public static Integer getIndexLastColumn (File file , Integer indexRow ) throws OpenWorkbookException , SheetNotFoundException , ExtensionNotValidException , IOException {
228- return getIndexLastColumn (file , null , indexRow );
229- }
230-
231- /**
232- * This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1
233- * @deprecated since version 0.3.0
234- * @param file file An Excel file
235- * @param sheetName The name of the sheet to open
236- * @param indexRow the row index
237- * @return The position of the last column of the chosen row
238- * @throws OpenWorkbookException If an error occurred while opening the workbook
239- * @throws SheetNotFoundException If the sheet to open is not found
240- * @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
241- * @throws IOException If an I/O error occurs
242- */
243- @ Deprecated
244- public static Integer getIndexLastColumn (File file , String sheetName , Integer indexRow ) throws OpenWorkbookException , SheetNotFoundException , ExtensionNotValidException , IOException {
245- /* Open file excel */
246- ExcelWorkbook excelWorkbook = ExcelWorkbook .open (file );
247- Sheet sheet = (sheetName == null || sheetName .isEmpty ())
248- ? excelWorkbook .getSheet (0 ).getSheet ()
249- : excelWorkbook .getSheet (sheetName ).getSheet ();
250- return (int ) sheet .getRow (indexRow ).getLastCellNum ();
251- }
252-
253- private static int countOnlyRowsNotEmpty (Sheet sheet ) {
254- int numRows = sheet .getLastRowNum () + 1 ;
255- for (int i = 0 ; i < sheet .getLastRowNum (); i ++) {
256- Row row = sheet .getRow (i );
257- boolean isEmptyRow = true ;
258-
259- if (row == null ) {
260- numRows --;
261- continue ;
262- }
263-
264- for (int j = 0 ; j < row .getLastCellNum (); j ++) {
265- Cell cell = row .getCell (j );
266- if (cell != null ) {
267- Object val ;
268- switch (cell .getCellType ()) {
269- case NUMERIC -> val = cell .getNumericCellValue ();
270- case BOOLEAN -> val = cell .getBooleanCellValue ();
271- default -> val = cell .getStringCellValue ();
272- }
273- if (val != null ) {
274- isEmptyRow = false ;
275- break ;
276- }
277- }
278- }
279-
280- if (isEmptyRow ) {
281- numRows --;
282- }
283- }
284-
285- return numRows ;
286- }
28736}
0 commit comments