我需要将 Web 表格与下载的 Excel 文件进行比较。 Excel 数据具有在 Excel 中格式化的数值(例如 523010735055.256 在 Excel 中格式化为 $ 523,011。 我需要验证 523,011 美元的 UI 值。 我怎样才能做到两个表都相等。 图片供引用[1]:https://i.imgur.com/a/OWXS7pJ “图像”
我已经使用 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("");
}
最佳答案
看来DataFormatter
如果使用正数、负数、零和/或文本的多个模式并且未给出所有四个子模式,则在最后一个子模式后需要一个分号。
示例:Format for positive numbers;Format for negative numbers;Format for zeros;Format for text
有效,但是 Format for positive numbers;Format for negative numbers
不起作用,需要 Format for positive numbers;Format for negative numbers;
。请注意负数模式后面的分号。
这与 Excel
不同因为不需要最后一个分号。
如果我们知道这一点,我们可以执行以下操作:
工作表示例:
Excel
中的数字格式模式是 "$ "#,##0,,;"($ "#,##0,,\)
.
代码:
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
class ReadExcelSpecialNumberFormat {
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelSpecialNumberFormat.xlsx"));
DataFormatter dataFormatter = new DataFormatter();
CreationHelper creationHelper = workbook.getCreationHelper();
FormulaEvaluator formulaEvaluator = creationHelper.createFormulaEvaluator();
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
CellStyle cellStyle = cell.getCellStyle();
String dataFormatString = cellStyle.getDataFormatString();
if (dataFormatString != null && dataFormatString.contains(";"))
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(dataFormatString+";"));
String cellContent = dataFormatter.formatCellValue(cell, formulaEvaluator);
System.out.print(cellContent + " ");
}
System.out.println();
}
workbook.close();
}
}
结果:
axel@arichter:~/Dokumente/JAVA/poi/poi-4.1.0$ javac -Xlint:deprecation -Xlint:unchecked -cp .:./*:./lib/*:./ooxml-lib/* ReadExcelSpecialNumberFormat.java
axel@arichter:~/Dokumente/JAVA/poi/poi-4.1.0$ java -cp .:./*:./lib/*:./ooxml-lib/* ReadExcelSpecialNumberFormat
Data Grouping Amount 1 ($ M) Amount 2 ($ M) Amount 3 ($ M)
Credit Risk $ 772.314 $ 913.546 $ 812.453
Available For Sale ($ 685.104) $ 800.603 $ 830.131
Contingent $ 509.480 $ 524.524 ($ 629.797)
Total $ 596.689 $ 2.238.674 $ 1.012.786
关于java - Selenium 将 Web 表格与 Excel 数据进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55959105/