Мне нужно сравнить веб-таблицу с загруженным файлом Excel.Данные Excel имеют числовое значение, которое отформатировано в Excel (например, 523010735055.256 отформатировано в Excel $ 523 011. Мне нужно проверить $ 523 011 со значением пользовательского интерфейса. Как я могу сделать равными обе таблицы. Изображение для справки [1]: https://i.imgur.com/a/OWXS7pJ "image"
Я использовал DataFormatter, как и большинство других постов, предложенных для аналогичного вопроса. Но он не работает.
Я нашел другой способ решить эту проблему без
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
cell.getNumericCellValue();
String value = formatter.formatCellValue(cell);
System.out.print(value + " ");
break;
Как изменить вышеуказанный код следующим образом
String val1 = null;
Double numvalue1 = cell.getNumericCellValue();
Long longValue1 = numvalue1.longValue();
val1 = new String(longValue1.toString());
BigDecimal convertres1 = new BigDecimal(val1);
String aarwavalue1 = (convertres1.divide(new BigDecimal("1000000")).setScale(0, RoundingMode.FLOOR) + "");
Код
waitForElementPresent (драйвер, 30, mstrframe);
// To locate table.
WebElement mytable = driver.findElement(By.xpath("(//div[contains(@id,'ReportGridStyle_forRW')])[2]//tbody"));
// To locate rows of table.
List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
// To calculate no of rows In table.
int rows_count = rows_table.size();
// Loop will execute till the last row of table.
for (int row = 0; row < rows_count; row++) {
// To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
// To calculate no of columns (cells). In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row " + row + " are " + columns_count);
// Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
// To retrieve text from that specific cell.
String celtext = Columns_row.get(column).getText();
System.out
.println("Cell Value of row number " + row + " and column number " + column + " Is " + celtext);
}
}
FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "\\src\\main\\resources\\excelfiles\\Bas Outline Mode Report.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
// Read sheet inside the workbook by its name
XSSFSheet sh1 = wb.getSheetAt(0);
// Data formatter
DataFormatter formatter = new DataFormatter();
// Find number of rows in excel file
// Iterate through each rows one by one
Iterator<Row> rowIterator = sh1.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
cell.getNumericCellValue();
String value = formatter.formatCellValue(cell);
System.out.print(value + " ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + " ");
break;
}
}
System.out.println("");
}
Вывод
Number of cells In Row 0 are 5
Cell Value of row number 0 and column number 0 Is DataGrouping
Cell Value of row number 0 and column number 1 Is RWAExposureType
Cell Value of row number 0 and column number 2 Is AA RWA ex 1.06x
Cell Value of row number 0 and column number 3 Is AA RWA
Cell Value of row number 0 and column number 4 Is SA RWA
Number of cells In Row 1 are 5
Cell Value of row number 1 and column number 0 Is Credit Risk
Cell Value of row number 1 and column number 1 Is Available For Sale
Cell Value of row number 1 and column number 2 Is $ 449,454
Cell Value of row number 1 and column number 3 Is $ 476,421
Cell Value of row number 1 and column number 4 Is $ 264,503
Number of cells In Row 2 are 4
Cell Value of row number 2 and column number 0 Is Contingent
Cell Value of row number 2 and column number 1 Is $ 113,262
Cell Value of row number 2 and column number 2 Is $ 120,057
Cell Value of row number 2 and column number 3 Is $ 258,508
Number of cells In Row 3 are 4
Cell Value of row number 3 and column number 0 Is Total
Cell Value of row number 3 and column number 1 Is $ 562,715
Cell Value of row number 3 and column number 2 Is $ 596,478
Cell Value of row number 3 and column number 3 Is $ 523,011
Number of cells In Row 4 are 5
Cell Value of row number 4 and column number 0 Is Total
Cell Value of row number 4 and column number 1 Is
Cell Value of row number 4 and column number 2 Is $ 562,715
Cell Value of row number 4 and column number 3 Is $ 596,478
Cell Value of row number 4 and column number 4 Is $ 523,011
Data Grouping RWA Exposure Type ( $ M ) AA RWA ex 1.06x ( $ M ) AA RWA ( $ M ) SA RWA
Credit Risk 5.62715E+11 5.96478E+11 5.23011E+11
Available For Sale 4.49454E+11 4.76421E+11 2.64503E+11
Contingent 1.13262E+11 1.20057E+11 2.58508E+11
Total 5.62715E+11 5.96478E+11 5.23011E+11
5/2/2019 1:16:59 PM
POI изменить на 4.1.0
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// Check the cell type and format accordingly
switch (cell.getCellType()) {
case NUMERIC:
cell.getNumericCellValue();
String value =formatter.formatCellValue(cell);
System.out.print(value + " ");
break;
case STRING:
System.out.print(cell.getStringCellValue() + " ");
break;
default:
break;
}
}
System.out.println("");
}