我需要比较Web表和下载的excel文件。 Excel数据具有以excel格式设置的数值(示例523010735055.256格式为excel $ 523,011。 我需要用UI值验证$ 523,011。 我该怎么做等于两张桌子。 图片供参考[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
兴趣点更改为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("");
}
答案 0 :(得分:2)
如果使用多个正数,负数,零和/或文本的模式,但没有给出全部四个子模式,则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
答案 1 :(得分:0)
感谢@AxcelRicher的所有反馈。 在这种情况下,Dataformatter不起作用。
我将使用以下代码立即解决问题
switch (cell.getCellType()) {
case NUMERIC:
//cell.getNumericCellValue();
Double value= cell.getNumericCellValue();
Long longValue1 = value.longValue();
String val1 = new String(longValue1.toString());
BigDecimal convertres1 = new BigDecimal(val1);
String aarwavalue1 = (convertres1.divide(new BigDecimal("1000000")).setScale(0, RoundingMode.FLOOR) + "");
System.out.print(aarwavalue1 + "\t ");
break;
case STRING:
System.out.print(cell.getStringCellValue() + " ");
break;
default:
break;