|
11 | 11 | import org.apache.poi.xssf.usermodel.XSSFCellStyle; |
12 | 12 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
13 | 13 |
|
14 | | -import java.io.File; |
15 | | -import java.io.FileInputStream; |
16 | | -import java.io.FileOutputStream; |
17 | | -import java.io.IOException; |
| 14 | +import java.io.*; |
| 15 | +import java.util.Arrays; |
18 | 16 | import java.util.HashMap; |
19 | 17 | import java.util.Hashtable; |
20 | 18 | import java.util.Map; |
@@ -216,14 +214,128 @@ public Object[][] getDataHashTable(String excelPath, String sheetName, int start |
216 | 214 |
|
217 | 215 | } |
218 | 216 |
|
219 | | - public String getTestCaseName(String testCaseName) { |
220 | | - String value = testCaseName; |
221 | | - int position = value.indexOf("@"); |
222 | | - value = value.substring(0, position); |
223 | | - position = value.lastIndexOf("."); |
| 217 | + // Get data from specific rows |
| 218 | + public Object[][] getDataFromSpecificRows(String excelPath, String sheetName, int[] rowNumbers) { |
| 219 | + System.out.println("Excel File: " + excelPath); |
| 220 | + System.out.println("Sheet Name: " + sheetName); |
| 221 | + System.out.println("Reading data from specific rows: " + Arrays.toString(rowNumbers)); |
224 | 222 |
|
225 | | - value = value.substring(position + 1); |
226 | | - return value; |
| 223 | + Object[][] data = null; |
| 224 | + |
| 225 | + try { |
| 226 | + File f = new File(excelPath); |
| 227 | + |
| 228 | + if (!f.exists()) { |
| 229 | + System.out.println("File Excel path not found."); |
| 230 | + throw new FileNotFoundException("File Excel path not found."); |
| 231 | + } |
| 232 | + |
| 233 | + fis = new FileInputStream(excelPath); |
| 234 | + workbook = WorkbookFactory.create(fis); |
| 235 | + sheet = workbook.getSheet(sheetName); |
| 236 | + |
| 237 | + if (sheet == null) { |
| 238 | + System.out.println("Sheet name not found."); |
| 239 | + throw new RuntimeException("Sheet name not found."); |
| 240 | + } |
| 241 | + |
| 242 | + int columns = getColumns(); |
| 243 | + System.out.println("Column count: " + columns); |
| 244 | + |
| 245 | + // Khởi tạo mảng data với kích thước bằng số lượng dòng được chỉ định |
| 246 | + data = new Object[rowNumbers.length][columns]; |
| 247 | + |
| 248 | + // Đọc dữ liệu từ các dòng được chỉ định |
| 249 | + for (int i = 0; i < rowNumbers.length; i++) { |
| 250 | + int rowNum = rowNumbers[i]; |
| 251 | + // Kiểm tra xem dòng có tồn tại không |
| 252 | + if (rowNum > sheet.getLastRowNum()) { |
| 253 | + System.out.println("WARNING: Row " + rowNum + " does not exist in the sheet."); |
| 254 | + // Gán giá trị rỗng cho dòng không tồn tại |
| 255 | + for (int j = 0; j < columns; j++) { |
| 256 | + data[i][j] = ""; |
| 257 | + } |
| 258 | + continue; |
| 259 | + } |
| 260 | + |
| 261 | + for (int j = 0; j < columns; j++) { |
| 262 | + data[i][j] = getCellData(rowNum, j); |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + // Đóng workbook và FileInputStream |
| 267 | + workbook.close(); |
| 268 | + fis.close(); |
| 269 | + |
| 270 | + } catch (Exception e) { |
| 271 | + System.out.println("Exception in getDataFromSpecificRows: " + e.getMessage()); |
| 272 | + e.printStackTrace(); |
| 273 | + } |
| 274 | + |
| 275 | + return data; |
| 276 | + } |
| 277 | + |
| 278 | + // Get data from specific rows with Hashtable |
| 279 | + public Object[][] getDataHashTableFromSpecificRows(String excelPath, String sheetName, int[] rowNumbers) { |
| 280 | + System.out.println("Excel File: " + excelPath); |
| 281 | + System.out.println("Sheet Name: " + sheetName); |
| 282 | + System.out.println("Reading data from specific rows: " + Arrays.toString(rowNumbers)); |
| 283 | + |
| 284 | + Object[][] data = null; |
| 285 | + |
| 286 | + try { |
| 287 | + File f = new File(excelPath); |
| 288 | + |
| 289 | + if (!f.exists()) { |
| 290 | + System.out.println("File Excel path not found."); |
| 291 | + throw new FileNotFoundException("File Excel path not found."); |
| 292 | + } |
| 293 | + |
| 294 | + fis = new FileInputStream(excelPath); |
| 295 | + workbook = WorkbookFactory.create(fis); |
| 296 | + sheet = workbook.getSheet(sheetName); |
| 297 | + |
| 298 | + if (sheet == null) { |
| 299 | + System.out.println("Sheet name not found."); |
| 300 | + throw new RuntimeException("Sheet name not found."); |
| 301 | + } |
| 302 | + |
| 303 | + int columns = getColumns(); |
| 304 | + // Khởi tạo mảng data với kích thước bằng số lượng dòng được chỉ định |
| 305 | + data = new Object[rowNumbers.length][1]; |
| 306 | + |
| 307 | + // Đọc dữ liệu từ các dòng được chỉ định |
| 308 | + for (int i = 0; i < rowNumbers.length; i++) { |
| 309 | + int rowNum = rowNumbers[i]; |
| 310 | + // Kiểm tra xem dòng có tồn tại không |
| 311 | + if (rowNum > sheet.getLastRowNum()) { |
| 312 | + System.out.println("WARNING: Row " + rowNum + " does not exist in the sheet."); |
| 313 | + data[i][0] = new Hashtable<String, String>(); |
| 314 | + continue; |
| 315 | + } |
| 316 | + |
| 317 | + Hashtable<String, String> table = new Hashtable<>(); |
| 318 | + for (int j = 0; j < columns; j++) { |
| 319 | + // Lấy tên cột từ dòng đầu tiên (header) |
| 320 | + String columnName = getCellData(0, j); |
| 321 | + // Lấy giá trị từ dòng hiện tại và cột j |
| 322 | + String cellValue = getCellData(rowNum, j); |
| 323 | + // Thêm vào Hashtable |
| 324 | + table.put(columnName, cellValue); |
| 325 | + } |
| 326 | + data[i][0] = table; |
| 327 | + } |
| 328 | + |
| 329 | + // Đóng workbook và FileInputStream |
| 330 | + workbook.close(); |
| 331 | + fis.close(); |
| 332 | + |
| 333 | + } catch (Exception e) { |
| 334 | + System.out.println("Exception in getDataHashTableFromSpecificRows: " + e.getMessage()); |
| 335 | + e.printStackTrace(); |
| 336 | + } |
| 337 | + |
| 338 | + return data; |
227 | 339 | } |
228 | 340 |
|
229 | 341 | public int getRowContains(String sTestCaseName, int colNum) { |
|
0 commit comments