Skip to content

Commit 45a5c80

Browse files
committed
I added some methods for ExcelRow and ExcelSheet.
1 parent 0ae6fcd commit 45a5c80

File tree

6 files changed

+145
-10
lines changed

6 files changed

+145
-10
lines changed

src/main/java/model/ExcelRow.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,35 @@ public ExcelSheet getSheet() {
3535
String sheetName = sheet.getSheetName();
3636
return new ExcelSheet(sheet, excelWorkbook.getSheet(sheetName).getIndex(), sheetName);
3737
}
38+
39+
public Integer getLastColumnIndex() {
40+
return this.row.getLastCellNum() - 1;
41+
}
42+
43+
public Integer countAllColumns(Boolean alsoEmpty) {
44+
Integer count = this.getLastColumnIndex() + 1;
45+
if (alsoEmpty)
46+
return count;
47+
48+
for (int i = 0; i < this.row.getPhysicalNumberOfCells(); i++) {
49+
Cell cell = this.row.getCell(i);
50+
if (cell == null) {
51+
count--;
52+
continue;
53+
}
54+
55+
Object val;
56+
switch (cell.getCellType()) {
57+
case NUMERIC -> val = cell.getNumericCellValue();
58+
case BOOLEAN -> val = cell.getBooleanCellValue();
59+
default -> val = cell.getStringCellValue();
60+
}
61+
62+
if (val == null) {
63+
count--;
64+
}
65+
}
66+
67+
return count;
68+
}
3869
}

src/main/java/model/ExcelSheet.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.AllArgsConstructor;
55
import lombok.EqualsAndHashCode;
66
import lombok.Getter;
7+
import org.apache.poi.ss.usermodel.Cell;
78
import org.apache.poi.ss.usermodel.Row;
89
import org.apache.poi.ss.usermodel.Sheet;
910
import org.apache.poi.ss.usermodel.Workbook;
@@ -49,4 +50,46 @@ public List<ExcelRow> getRows() {
4950

5051
return excelRows;
5152
}
53+
54+
public Integer getLastRowIndex() {
55+
return this.sheet.getLastRowNum();
56+
}
57+
58+
public Integer countAllRows(Boolean alsoEmpty) {
59+
Integer count = this.getLastRowIndex() + 1;
60+
if (alsoEmpty)
61+
return count;
62+
63+
for (int i = 0; i < this.sheet.getPhysicalNumberOfRows(); i++) {
64+
Row row = this.sheet.getRow(i);
65+
boolean isEmptyRow = true;
66+
67+
if (row == null) {
68+
count--;
69+
continue;
70+
}
71+
72+
for (int j = 0; j < row.getLastCellNum(); j++) {
73+
Cell cell = row.getCell(j);
74+
if (cell != null) {
75+
Object val;
76+
switch (cell.getCellType()) {
77+
case NUMERIC -> val = cell.getNumericCellValue();
78+
case BOOLEAN -> val = cell.getBooleanCellValue();
79+
default -> val = cell.getStringCellValue();
80+
}
81+
if (val != null) {
82+
isEmptyRow = false;
83+
break;
84+
}
85+
}
86+
}
87+
88+
if (isEmptyRow) {
89+
count--;
90+
}
91+
}
92+
93+
return count;
94+
}
5295
}

src/main/java/samples/countAllRowsSample/Main.java

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

3-
import tools.ExcelUtility;
3+
import model.ExcelSheet;
4+
import model.ExcelWorkbook;
45

56
import java.io.File;
7+
import java.util.LinkedList;
68
import java.util.List;
79

810
public class Main {
@@ -12,14 +14,16 @@ public static void main(String[] args) {
1214
File file = new File("./src/main/resources/car.xlsx");
1315

1416
try {
15-
int totalRows = ExcelUtility.countAllRows(file, "car");
16-
System.out.println("Total: " + totalRows);
17-
int totalRowsWithoutEmpty = ExcelUtility.countAllRows(file, "car", false);
18-
System.out.println("Total without empty rows: " + totalRowsWithoutEmpty);
19-
List<Integer> totalRowsOfSheets = ExcelUtility.countAllRowsOfAllSheets(file);
20-
System.out.println("Total of all sheets: " + totalRowsOfSheets);
21-
List<Integer> totalRowsOfSheetsWithoutEmpty = ExcelUtility.countAllRowsOfAllSheets(file, false);
22-
System.out.println("Total of all sheets without empty rows : " + totalRowsOfSheetsWithoutEmpty);
17+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
18+
List<ExcelSheet> excelSheets = excelWorkbook.getSheets();
19+
int totalRows = excelWorkbook.getSheet("car").countAllRows(true);
20+
System.out.println("Total rows: " + totalRows);
21+
List<Integer> totalRowsWithoutEmptyOfSheets = new LinkedList<>();
22+
for (ExcelSheet excelSheet : excelSheets) {
23+
totalRowsWithoutEmptyOfSheets.add(excelSheet.countAllRows(false));
24+
}
25+
26+
System.out.println("Total of all sheets without empty rows: " + totalRowsWithoutEmptyOfSheets);
2327
} catch (Exception e) {
2428
System.err.println("There was an error. Check the console");
2529
throw new RuntimeException(e);

src/main/java/tools/ExcelUtility.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,29 @@ public class ExcelUtility {
2626
/**
2727
* Counts all rows in all sheets<p>
2828
* If not specified, empty lines will also be included
29+
* @deprecated since version 0.3.0
2930
* @param file an Excel file
3031
* @return A list with the number of rows present for each sheet
3132
* @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
3233
* @throws IOException If an I/O error occurs
3334
* @throws OpenWorkbookException If an error occurred while opening the workbook
3435
*/
36+
@Deprecated
3537
public static List<Integer> countAllRowsOfAllSheets(File file) throws ExtensionNotValidException, IOException, OpenWorkbookException {
3638
return countAllRowsOfAllSheets(file, true);
3739
}
3840

3941
/**
4042
* * Counts all rows in all sheets
43+
* @deprecated since version 0.3.0
4144
* @param file an Excel file
4245
* @param alsoEmptyRows if {@code true} then it will also count rows with all empty cells
4346
* @return A list with the number of rows present for each sheet
4447
* @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
4548
* @throws IOException If an I/O error occurs
4649
* @throws OpenWorkbookException If an error occurred while opening the workbook
4750
*/
51+
@Deprecated
4852
public static List<Integer> countAllRowsOfAllSheets(File file, Boolean alsoEmptyRows) throws ExtensionNotValidException, IOException, OpenWorkbookException {
4953
/* Open file excel */
5054
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
@@ -69,6 +73,7 @@ public static List<Integer> countAllRowsOfAllSheets(File file, Boolean alsoEmpty
6973
/**
7074
* Counts all rows in a sheet<p>
7175
* If not specified, empty lines will also be included
76+
* @deprecated since version 0.3.0
7277
* @param file An Excel file
7378
* @param sheetName The name of the sheet to open
7479
* @return A number that corresponds to all rows in the sheet
@@ -77,12 +82,14 @@ public static List<Integer> countAllRowsOfAllSheets(File file, Boolean alsoEmpty
7782
* @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
7883
* @throws IOException If an I/O error occurs
7984
*/
85+
@Deprecated
8086
public static Integer countAllRows(File file, String sheetName) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
8187
return countAllRows(file, sheetName, true);
8288
}
8389

8490
/**
8591
* Counts all rows in a sheet
92+
* @deprecated since version 0.3.0
8693
* @param file An Excel file
8794
* @param sheetName The name of the sheet to open
8895
* @param alsoEmptyRows if {@code true} then it will also count rows with all empty cells
@@ -92,6 +99,7 @@ public static Integer countAllRows(File file, String sheetName) throws OpenWorkb
9299
* @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
93100
* @throws IOException If an I/O error occurs
94101
*/
102+
@Deprecated
95103
public static Integer countAllRows(File file, String sheetName, Boolean alsoEmptyRows) throws ExtensionNotValidException, IOException, OpenWorkbookException, SheetNotFoundException {
96104
/* Open file excel */
97105
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
@@ -136,19 +144,22 @@ public static Boolean isValidExcelExtension(String extension) {
136144
/**
137145
* This method is used to recover the position of the last row of the Sheet. Note the count starts at 1<p>
138146
* By default, the first Sheet is chosen
147+
* @deprecated since version 0.3.0
139148
* @param file file An Excel file
140149
* @return The position of the last row of the Sheet
141150
* @throws OpenWorkbookException If an error occurred while opening the workbook
142151
* @throws SheetNotFoundException If the sheet to open is not found
143152
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
144153
* @throws IOException If an I/O error occurs
145154
*/
155+
@Deprecated
146156
public static Integer getIndexLastRow(File file) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
147157
return getIndexLastRow(file, null);
148158
}
149159

150160
/**
151161
* 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
152163
* @param file file An Excel file
153164
* @param sheetName The name of the sheet to open
154165
* @return The position of the last row of the Sheet
@@ -157,6 +168,7 @@ public static Integer getIndexLastRow(File file) throws OpenWorkbookException, S
157168
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
158169
* @throws IOException If an I/O error occurs
159170
*/
171+
@Deprecated
160172
public static Integer getIndexLastRow(File file, String sheetName) throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
161173
/* Open file excel */
162174
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);
@@ -169,20 +181,23 @@ public static Integer getIndexLastRow(File file, String sheetName) throws OpenWo
169181
/**
170182
* This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
171183
* By default, the first sheet and the first row are chosen
184+
* @deprecated since version 0.3.0
172185
* @param file file An Excel file
173186
* @return The position of the last column of the chosen row
174187
* @throws OpenWorkbookException If an error occurred while opening the workbook
175188
* @throws SheetNotFoundException If the sheet to open is not found
176189
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
177190
* @throws IOException If an I/O error occurs
178191
*/
192+
@Deprecated
179193
public static Integer getIndexLastColumn(File file) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
180194
return getIndexLastColumn(file, null, 0);
181195
}
182196

183197
/**
184198
* This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
185199
* By default, the first row is chosen
200+
* @deprecated since version 0.3.0
186201
* @param file file An Excel file
187202
* @param sheetName The name of the sheet to open
188203
* @return The position of the last column of the chosen row
@@ -191,13 +206,15 @@ public static Integer getIndexLastColumn(File file) throws OpenWorkbookException
191206
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
192207
* @throws IOException If an I/O error occurs
193208
*/
209+
@Deprecated
194210
public static Integer getIndexLastColumn(File file, String sheetName) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
195211
return getIndexLastColumn(file, sheetName, 0);
196212
}
197213

198214
/**
199215
* This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
200216
* By default, the first sheet is chosen
217+
* @deprecated since version 0.3.0
201218
* @param file file An Excel file
202219
* @param indexRow the row index
203220
* @return The position of the last column of the chosen row
@@ -206,12 +223,14 @@ public static Integer getIndexLastColumn(File file, String sheetName) throws Ope
206223
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
207224
* @throws IOException If an I/O error occurs
208225
*/
226+
@Deprecated
209227
public static Integer getIndexLastColumn(File file, Integer indexRow) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
210228
return getIndexLastColumn(file, null, indexRow);
211229
}
212230

213231
/**
214232
* 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
215234
* @param file file An Excel file
216235
* @param sheetName The name of the sheet to open
217236
* @param indexRow the row index
@@ -221,6 +240,7 @@ public static Integer getIndexLastColumn(File file, Integer indexRow) throws Ope
221240
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
222241
* @throws IOException If an I/O error occurs
223242
*/
243+
@Deprecated
224244
public static Integer getIndexLastColumn(File file, String sheetName, Integer indexRow) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
225245
/* Open file excel */
226246
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(file);

src/test/java/model/ExcelRowTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,22 @@ void getSheet() throws OpenWorkbookException, ExtensionNotValidException, IOExce
3434
ExcelSheet excelSheet1 = excelRow.getSheet();
3535
Assertions.assertEquals(excelSheet, excelSheet1);
3636
}
37+
38+
@Test
39+
void getLastColumnIndex() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
40+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
41+
ExcelSheet excelSheet = excelWorkbook.getSheet(1);
42+
List<ExcelRow> excelRows = excelSheet.getRows();
43+
Assertions.assertEquals(4, excelRows.get(0).getLastColumnIndex());
44+
Assertions.assertEquals(2, excelRows.get(1).getLastColumnIndex());
45+
}
46+
47+
@Test
48+
void countAllColumns() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
49+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
50+
ExcelSheet excelSheet = excelWorkbook.getSheet(1);
51+
List<ExcelRow> excelRows = excelSheet.getRows();
52+
Assertions.assertEquals(4, excelRows.get(0).countAllColumns(false));
53+
Assertions.assertEquals(3, excelRows.get(1).countAllColumns(true));
54+
}
3755
}

src/test/java/model/ExcelSheetTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.File;
1111
import java.io.IOException;
12+
import java.util.List;
1213

1314
public class ExcelSheetTest {
1415

@@ -39,6 +40,24 @@ void getWorkbook() throws OpenWorkbookException, ExtensionNotValidException, IOE
3940
}
4041

4142
@Test
42-
void getRows() {
43+
void getRows() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
44+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
45+
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
46+
List<ExcelRow> excelRows = excelSheet.getRows();
47+
Assertions.assertEquals(3, excelRows.size());
48+
}
49+
50+
@Test
51+
void getLastRowIndex() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
52+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
53+
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
54+
Assertions.assertEquals(2, excelSheet.getLastRowIndex());
55+
}
56+
57+
@Test
58+
void countAllRows() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
59+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
60+
ExcelSheet excelSheet = excelWorkbook.getSheet(1);
61+
Assertions.assertEquals(4, excelSheet.countAllRows(false));
4362
}
4463
}

0 commit comments

Comments
 (0)