从Excel获取价格并需要使用POI计算值

时间:2017-08-21 10:30:37

标签: java selenium apache-poi

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

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");
            }

1 个答案:

答案 0 :(得分:0)

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

尝试使用以下代码 - 只需复制并粘贴

即可
UnboundLocalError: local variable 'conn' referenced before assignment