Skip to content

Commit d248ff1

Browse files
committed
I added methods that return the index of the last row of a sheet and the index of the last column of a row
1 parent 6521280 commit d248ff1

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

src/main/java/tools/ExcelUtility.java

+89
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,95 @@ public static Boolean isValidExcelExtension(String extension) {
140140
return extension.equalsIgnoreCase(Extension.XLS.getExt()) || extension.equalsIgnoreCase(Extension.XLSX.getExt());
141141
}
142142

143+
/**
144+
* This method is used to recover the position of the last row of the Sheet. Note the count starts at 1<p>
145+
* By default, the first Sheet is chosen
146+
* @param file file An Excel file
147+
* @return The position of the last row of the Sheet
148+
* @throws OpenWorkbookException If an error occurred while opening the workbook
149+
* @throws SheetNotFoundException If the sheet to open is not found
150+
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
151+
* @throws IOException If an I/O error occurs
152+
*/
153+
public static Integer getIndexLastRow(File file) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
154+
return getIndexLastRow(file, null);
155+
}
156+
157+
/**
158+
* This method is used to recover the position of the last row of the Sheet. Note the count starts at 1
159+
* @param file file An Excel file
160+
* @param sheetName The name of the sheet to open
161+
* @return The position of the last row of the Sheet
162+
* @throws OpenWorkbookException If an error occurred while opening the workbook
163+
* @throws SheetNotFoundException If the sheet to open is not found
164+
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
165+
* @throws IOException If an I/O error occurs
166+
*/
167+
public static Integer getIndexLastRow(File file, String sheetName) throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
168+
Sheet sheet = (sheetName == null || sheetName.isEmpty()) ? SheetUtility.open(file) : SheetUtility.open(file, sheetName);
169+
return sheet.getLastRowNum() + 1;
170+
}
171+
172+
/**
173+
* This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
174+
* By default, the first sheet and the first row are chosen
175+
* @param file file An Excel file
176+
* @return The position of the last column of the chosen row
177+
* @throws OpenWorkbookException If an error occurred while opening the workbook
178+
* @throws SheetNotFoundException If the sheet to open is not found
179+
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
180+
* @throws IOException If an I/O error occurs
181+
*/
182+
public static Integer getIndexLastColumn(File file) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
183+
return getIndexLastColumn(file, null, 0);
184+
}
185+
186+
/**
187+
* This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
188+
* By default, the first row is chosen
189+
* @param file file An Excel file
190+
* @param sheetName The name of the sheet to open
191+
* @return The position of the last column of the chosen row
192+
* @throws OpenWorkbookException If an error occurred while opening the workbook
193+
* @throws SheetNotFoundException If the sheet to open is not found
194+
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
195+
* @throws IOException If an I/O error occurs
196+
*/
197+
public static Integer getIndexLastColumn(File file, String sheetName) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
198+
return getIndexLastColumn(file, sheetName, 0);
199+
}
200+
201+
/**
202+
* This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1<p>
203+
* By default, the first sheet is chosen
204+
* @param file file An Excel file
205+
* @param indexRow the row index
206+
* @return The position of the last column of the chosen row
207+
* @throws OpenWorkbookException If an error occurred while opening the workbook
208+
* @throws SheetNotFoundException If the sheet to open is not found
209+
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
210+
* @throws IOException If an I/O error occurs
211+
*/
212+
public static Integer getIndexLastColumn(File file, Integer indexRow) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
213+
return getIndexLastColumn(file, null, indexRow);
214+
}
215+
216+
/**
217+
* This method is used to recover the position of the last column of the chosen row. Note that the count starts at 1
218+
* @param file file An Excel file
219+
* @param sheetName The name of the sheet to open
220+
* @param indexRow the row index
221+
* @return The position of the last column of the chosen row
222+
* @throws OpenWorkbookException If an error occurred while opening the workbook
223+
* @throws SheetNotFoundException If the sheet to open is not found
224+
* @throws ExtensionNotValidException If the filename extension does not belong to an Excel file
225+
* @throws IOException If an I/O error occurs
226+
*/
227+
public static Integer getIndexLastColumn(File file, String sheetName, Integer indexRow) throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
228+
Sheet sheet = (sheetName == null || sheetName.isEmpty()) ? SheetUtility.open(file) : SheetUtility.open(file, sheetName);
229+
return (int) sheet.getRow(indexRow).getLastCellNum();
230+
}
231+
143232
private static int countOnlyRowsNotEmpty(Sheet sheet) {
144233
int numRows = sheet.getLastRowNum() + 1;
145234
for (int i = 0; i < sheet.getLastRowNum(); i++) {

0 commit comments

Comments
 (0)