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