@@ -29,17 +29,13 @@ public class SheetUtility {
29
29
* @throws OpenWorkbookException If an error occurred while opening the workbook
30
30
*/
31
31
public static Integer length (File file ) throws ExtensionNotValidException , IOException , OpenWorkbookException {
32
- /* Check extension */
33
- String extension = ExcelUtility .checkExcelExtension (file .getName ());
34
-
35
32
/* Open file excel */
36
- FileInputStream fileInputStream = new FileInputStream (file );
37
- Workbook workbook = WorkbookUtility .open (fileInputStream , extension );
33
+ Workbook workbook = WorkbookUtility .open (file );
38
34
39
35
Integer totalSheets = workbook .getNumberOfSheets ();
40
36
41
37
/* Close file */
42
- WorkbookUtility .close (workbook , fileInputStream );
38
+ WorkbookUtility .close (workbook );
43
39
44
40
return totalSheets ;
45
41
}
@@ -53,12 +49,8 @@ public static Integer length(File file) throws ExtensionNotValidException, IOExc
53
49
* @throws OpenWorkbookException If an error occurred while opening the workbook
54
50
*/
55
51
public static List <String > getNames (File file ) throws ExtensionNotValidException , IOException , OpenWorkbookException {
56
- /* Check extension */
57
- String extension = ExcelUtility .checkExcelExtension (file .getName ());
58
-
59
52
/* Open file excel */
60
- FileInputStream fileInputStream = new FileInputStream (file );
61
- Workbook workbook = WorkbookUtility .open (fileInputStream , extension );
53
+ Workbook workbook = WorkbookUtility .open (file );
62
54
63
55
/* Iterate all the sheets */
64
56
Iterator <Sheet > sheetIterator = workbook .iterator ();
@@ -69,7 +61,7 @@ public static List<String> getNames(File file) throws ExtensionNotValidException
69
61
}
70
62
71
63
/* Close file */
72
- WorkbookUtility .close (workbook , fileInputStream );
64
+ WorkbookUtility .close (workbook );
73
65
74
66
return sheetNames ;
75
67
}
@@ -85,17 +77,13 @@ public static List<String> getNames(File file) throws ExtensionNotValidException
85
77
* @throws SheetNotFoundException If the sheet to open is not found
86
78
*/
87
79
public static Integer getIndex (File file , String sheetName ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
88
- /* Check extension */
89
- String extension = ExcelUtility .checkExcelExtension (file .getName ());
90
-
91
80
/* Open file excel */
92
- FileInputStream fileInputStream = new FileInputStream (file );
93
- Workbook workbook = WorkbookUtility .open (fileInputStream , extension );
81
+ Workbook workbook = WorkbookUtility .open (file );
94
82
95
83
int sheetIndex = workbook .getSheetIndex (sheetName );
96
84
97
85
/* Close file */
98
- WorkbookUtility .close (workbook , fileInputStream );
86
+ WorkbookUtility .close (workbook );
99
87
100
88
if (sheetIndex < 0 ) {
101
89
throw new SheetNotFoundException ("No sheet was found" );
@@ -114,12 +102,8 @@ public static Integer getIndex(File file, String sheetName) throws ExtensionNotV
114
102
* @throws SheetNotFoundException If the sheet to open is not found
115
103
*/
116
104
public static String getName (File file , Integer position ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
117
- /* Check extension */
118
- String extension = ExcelUtility .checkExcelExtension (file .getName ());
119
-
120
105
/* Open file excel */
121
- FileInputStream fileInputStream = new FileInputStream (file );
122
- Workbook workbook = WorkbookUtility .open (fileInputStream , extension );
106
+ Workbook workbook = WorkbookUtility .open (file );
123
107
124
108
String sheetName ;
125
109
try {
@@ -129,7 +113,7 @@ public static String getName(File file, Integer position) throws ExtensionNotVal
129
113
}
130
114
131
115
/* Close file */
132
- WorkbookUtility .close (workbook , fileInputStream );
116
+ WorkbookUtility .close (workbook );
133
117
134
118
return sheetName ;
135
119
}
@@ -187,7 +171,7 @@ public static Sheet create(Workbook workbook, String sheetName) {
187
171
}
188
172
189
173
/**
190
- * Opens the sheet of the Excel file<p>
174
+ * Gets the sheet of the Excel file<p>
191
175
* If not specified, the first sheet will be opened
192
176
* @param file Excel file
193
177
* @return The sheet in the workbook
@@ -196,12 +180,12 @@ public static Sheet create(Workbook workbook, String sheetName) {
196
180
* @throws OpenWorkbookException If an error occurred while opening the workbook
197
181
* @throws SheetNotFoundException If the sheet to open is not found
198
182
*/
199
- public static Sheet open (File file ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
200
- return open (file , 0 );
183
+ public static Sheet get (File file ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
184
+ return get (file , 0 );
201
185
}
202
186
203
187
/**
204
- * Opens the sheet of the Excel file
188
+ * Gets the sheet of the Excel file
205
189
* @param file Excel file
206
190
* @param sheetName The sheet name in the workbook
207
191
* @return The sheet in the workbook
@@ -210,27 +194,23 @@ public static Sheet open(File file) throws ExtensionNotValidException, IOExcepti
210
194
* @throws OpenWorkbookException If an error occurred while opening the workbook
211
195
* @throws SheetNotFoundException If the sheet to open is not found
212
196
*/
213
- public static Sheet open (File file , String sheetName ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
214
- /* Check extension */
215
- String extension = ExcelUtility .checkExcelExtension (file .getName ());
216
-
197
+ public static Sheet get (File file , String sheetName ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
217
198
/* Open file excel */
218
- FileInputStream fileInputStream = new FileInputStream (file );
219
- Workbook workbook = WorkbookUtility .open (fileInputStream , extension );
199
+ Workbook workbook = WorkbookUtility .open (file );
220
200
221
201
/* Open sheet */
222
202
Sheet sheet = workbook .getSheet (sheetName );
223
203
if (sheet == null )
224
204
throw new SheetNotFoundException ();
225
205
226
206
/* Close workbook */
227
- WorkbookUtility .close (workbook , fileInputStream );
207
+ WorkbookUtility .close (workbook );
228
208
229
209
return sheet ;
230
210
}
231
211
232
212
/**
233
- * Opens the sheet of the Excel file
213
+ * Gets the sheet of the Excel file
234
214
* @param file Excel file
235
215
* @param position The index in the workbook
236
216
* @return The sheet in the workbook
@@ -239,44 +219,40 @@ public static Sheet open(File file, String sheetName) throws ExtensionNotValidEx
239
219
* @throws OpenWorkbookException If an error occurred while opening the workbook
240
220
* @throws SheetNotFoundException If the sheet to open is not found
241
221
*/
242
- public static Sheet open (File file , Integer position ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
243
- /* Check extension */
244
- String extension = ExcelUtility .checkExcelExtension (file .getName ());
245
-
222
+ public static Sheet get (File file , Integer position ) throws ExtensionNotValidException , IOException , OpenWorkbookException , SheetNotFoundException {
246
223
/* Open file excel */
247
- FileInputStream fileInputStream = new FileInputStream (file );
248
- Workbook workbook = WorkbookUtility .open (fileInputStream , extension );
224
+ Workbook workbook = WorkbookUtility .open (file );
249
225
250
226
/* Open sheet */
251
227
Sheet sheet = workbook .getSheetAt (position );
252
228
if (sheet == null )
253
229
throw new SheetNotFoundException ();
254
230
255
231
/* Close workbook */
256
- WorkbookUtility .close (workbook , fileInputStream );
232
+ WorkbookUtility .close (workbook );
257
233
258
234
return sheet ;
259
235
}
260
236
261
237
/**
262
- * Opens the sheet in the workbook.<p>
238
+ * Gets the sheet in the workbook.<p>
263
239
* If not specified, the first sheet will be opened
264
240
* @param workbook The {@code Workbook} where there is the sheet
265
241
* @return The sheet in the workbook in first position
266
242
* @throws SheetNotFoundException If the sheet to open is not found
267
243
*/
268
- public static Sheet open (Workbook workbook ) throws SheetNotFoundException {
269
- return open (workbook , 0 );
244
+ public static Sheet get (Workbook workbook ) throws SheetNotFoundException {
245
+ return get (workbook , 0 );
270
246
}
271
247
272
248
/**
273
- * Opens the sheet in the workbook.
249
+ * Gets the sheet in the workbook.
274
250
* @param workbook The {@code Workbook} where there is the sheet
275
251
* @param sheetName The sheet name in the workbook
276
252
* @return The sheet in the workbook
277
253
* @throws SheetNotFoundException If the sheet to open is not found
278
254
*/
279
- public static Sheet open (Workbook workbook , String sheetName ) throws SheetNotFoundException {
255
+ public static Sheet get (Workbook workbook , String sheetName ) throws SheetNotFoundException {
280
256
/* Open sheet */
281
257
Sheet sheet = workbook .getSheet (sheetName );
282
258
if (sheet == null )
@@ -285,13 +261,13 @@ public static Sheet open(Workbook workbook, String sheetName) throws SheetNotFou
285
261
}
286
262
287
263
/**
288
- * Opens the sheet in the workbook.
264
+ * Gets the sheet in the workbook.
289
265
* @param workbook The {@code Workbook} where there is the sheet
290
266
* @param position The index in the workbook
291
267
* @return The sheet in the workbook
292
268
* @throws SheetNotFoundException If the sheet to open is not found
293
269
*/
294
- public static Sheet open (Workbook workbook , Integer position ) throws SheetNotFoundException {
270
+ public static Sheet get (Workbook workbook , Integer position ) throws SheetNotFoundException {
295
271
/* Open sheet */
296
272
Sheet sheet = workbook .getSheetAt (position );
297
273
if (sheet == null )
@@ -300,12 +276,12 @@ public static Sheet open(Workbook workbook, Integer position) throws SheetNotFou
300
276
}
301
277
302
278
/**
303
- * Opens the sheet in the workbook. If it doesn't find it, it creates it.
279
+ * Gets the sheet in the workbook. If it doesn't find it, it creates it.
304
280
* @param workbook The {@code Workbook} where there is the sheet
305
281
* @param sheetName The sheet name in the workbook
306
282
* @return The sheet in the workbook or a new one
307
283
*/
308
- public static Sheet openOrCreate (Workbook workbook , String sheetName ) {
284
+ public static Sheet getOrCreate (Workbook workbook , String sheetName ) {
309
285
/* Open sheet */
310
286
Sheet sheet = workbook .getSheet (sheetName );
311
287
return sheet == null ? workbook .createSheet (sheetName ) : sheet ;
0 commit comments