2017-08-21 41 views
0

在excel中有一列Value1,其中包含价格..例如$ 2789.98,$ 32321.45。我想获得这些值,并需要添加,例如:2789.98 + 32321.45。从Excel获取价格并需要使用POI计算值

public void testData() throws IOException { 
    Object[] calc = this.readExcelByColName("Calculations"); 
    for (int i = 1; i < calc.length; i++) { 
     String string = (String) calc[i-1]; 
     string = string.replaceAll("\\s", ""); 
     String[] parts = string.split("(\\=)"); 
     String[] parts1 = parts[0].split("(\\+)"); 
     DataFormatter df = new DataFormatter(); 
     if (parts.length == 2 && parts1.length > 1) { 
      String value1 = df.formatCellValue(this.sheet.getRow(i).getCell(2)); 
      String value2 = df.formatCellValue(this.sheet.getRow(i).getCell(3)); 
      String value3 = df.formatCellValue(this.sheet.getRow(i).getCell(4)); 
      float result= (Float.parseFloat(value1.toString().substring(1).replaceAll(",", "")) + Float.parseFloat(value2.toString().substring(1).replaceAll(",", ""))); 
      if (result == Float.parseFloat(value3.toString().substring(1).replaceAll(",", ""))) {     
       this.sheet.getRow(i).getCell(5).setCellValue("Pass"); 
      } else { 
       this.sheet.getRow(i).getCell(5).setCellValue("Fail"); 
      } 
+0

我可以能够得到的价格与$符号和删除使用substring,然后转换为整数,但在if(result == Integer.parseInt(value3)){condition,它不会将这两个值都添加到结果变量中。 –

+0

运行int result = Integer.parseInt(value1.toString()。substring(1))+ Integer.parseInt(value2.toString()。substring(1))测试用例正在失败。 –

+0

它没有返回任何东西,TC出错了。看起来转换有问题。我已检查转换为双倍,但没有工作。 –

回答

0

好像你正在使用括号“()”在错误的地方

尝试使用下面的代码,只需复制并粘贴

float result= Float.parseFloat(value1.toString().substring(1).replaceAll(",", "")) + Float.parseFloat(value2.toString().substring(1).replaceAll(",", "")); 
+0

是的......现在正在工作..谢谢了很多 –

+0

@KiranP - 你能接受答案吗?谢谢 – Kapil

+0

当然..我还有一个问题。 this.sheet.getRow(ⅰ).getCell(5).setCellValue( “通”);我无法将单元格值设置为我的Excel。可能是什么问题。 –