0

我需要根据找到的行数据总结 Excel Column(1) 值。

我的excel文件如下:

                 column(0)                         column(1)
Row[0]    ECIN - INPUT VALUE (ADD)       NetTradeAllowanceAmount = -600.00
Row[1]    ECIN - INPUT VALUE (ADD)       CashDownPayment = 300.00
Row[2]    ECIN - INPUT VALUE (ADD)       OtherDownPaymentAmount = PATH DOES NOT EXIST
Row[3]    ECIN - INPUT VALUE (ADD)       CashDownPayment = 400.00
Row[4]    ECIN - INPUT VALUE (SUB)       OtherDownPaymentAmount = PATH DOES NOT EXIST
Row[5]    ECIN - INPUT VALUE (SUB)       ManufacturerRebateAmount = 500.00
Row[6]    ECIN - INPUT VALUE (SUB)       DeferredDownPaymentAmount = -700.00
Row[7]    ECIN - INPUT VALUE (SUB)       DeferredDownPaymentAmount = 900.00

首先,我需要查看 Column(0),所有行:

1.add the column(1) values having rows (ADD) data. (eg: SUM= 300.00 + 400.00 - 600.00  = 700.00 - 600.00 = 100.00)
2.add the column(1) values having rows (SUB) data. (eg: SUM=500.00 - 700.00 + 900.00 = 1400.00 - 700.00 = 700.00)
3.then subtract above two SUMs.                    (eg: 100.00 - 700.00 = 600.00)  

我应该将此结果保存在某个变量中,并将该值记录在其他单元格中。

注意:程序不应考虑 value = PATH DOES NOT EXIST,即使行包含数据(SUB / ADD)。

在某种程度上,我已经编写了代码。如下:

import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
public class Hai
{
public static void main(String[] args)
{
    try 
    {
        FileInputStream file = new FileInputStream(new File("C:/Users/Excel.xls"));
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheetAt(5);
        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()) 
        {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while(cellIterator.hasNext())
            {
                    Cell cell = cellIterator.next();
                    String Tag=cell.getStringCellValue().toString();
                    cell = row.getCell(0+1);
                    if(cell !=null)
                    if(Tag.contains("ADD"))
                        {
                           String Tag1=cell.getStringCellValue().toString();
                           String[] s= Tag1.split("=");
                           //System.out.println(s[1]);
                           if(!s[1].contains("PATH DOES NOT EXIST"))
                           {
                             System.out.println(s[1].trim());
                           }
                        }
                    else if(Tag.contains("SUB"))
                        {
                           String Tag1=cell.getStringCellValue().toString();
                           String[] s= Tag1.split("=");
                           if(!s[1].contains("PATH DOES NOT EXIST"))
                           {
                             System.out.println(s[1].trim());
                           }
                        }
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }   
}
}

我得到的输出如下:

-600.00
300.00
400.00
500.00
-700.00
900.00

上面的值都是字符串格式的,我想总结一下这些值。请帮我!

我已将上述值转换​​为 Flaot,如下所示:

Float foo = Float.parseFloat(s[1].trim());

我得到的输出是:

-600.0
300.0
400.0

我想得到两位小数并总结这些值。我无法总结这些值。

是不是这样

import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
 public class Hai
{
public static double getSubstraction(double summ, String your)
{
    if (your.contains("-"))
    {
        return main + Double.parseDouble(your.replace("-", ""));
    } 
    else if (your.contains("+"))
    {
        return main - Double.parseDouble(your.replace("+", ""));
    } 
    else 
    {
        return main - Double.parseDouble(your);
    }

}
 public static double getSumm(double sub, String your) 
    {
        if (your.contains("-")) 
        {
            return main - Double.parseDouble(your.replace("-", ""));
        } 
        else if (your.contains("+")) 
        {
            return main + Double.parseDouble(your.replace("+", ""));
        } 
        else 
        {
            return main + Double.parseDouble(your);
        }
    }
public static void main(String[] args)
{
    try 
    {
        double summ, sub;
        FileInputStream file = new FileInputStream(new File("C:/Users/Pradeep.HALCYONTEKDC/Desktop/19-04-2013.xls"));
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheetAt(5);
        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()) 
        {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while(cellIterator.hasNext())
            {
                    Cell cell = cellIterator.next();
                    String Tag=cell.getStringCellValue().toString();
                    cell = row.getCell(0+1);
                    if(cell !=null)
                    if(Tag.contains("ADD"))
                        {
                           String Tag1=cell.getStringCellValue().toString();
                           String[] s= Tag1.split("=");
                           //System.out.println(s[1]);
                           if(!s[1].contains("PATH DOES NOT EXIST"))
                           {
                               getSumm() ;
                             Float foo = Float.parseFloat(s[1].trim());
                             System.out.println("1----  "+foo);
                             for(int i=0; i<5;i++)
                             {
                                 foo+=foo;
                                 //System.out.println(foo);
                             }
                           }
                        }
                    else if(Tag.contains("SUB"))
                        {
                           String Tag1=cell.getStringCellValue().toString();
                           String[] s= Tag1.split("=");
                           if(!s[1].contains("PATH DOES NOT EXIST"))
                           {
                               getSubstraction();
                             System.out.println(s[1].trim());
                           }
                        }


            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }   
}
}

帮我解决这个问题。

4

1 回答 1

1

如果你想使用 java 来处理 excel 数据,对我来说最好的选择是 apache POI 官方网站上有很多教程,如果你在代码中需要一些帮助,你可以询问,我们会尽力帮助你。

将这两种方法添加到您的代码中

private static double getSubstraction(double summ, String your) {
    if (your.contains("-")) {
        return summ + Double.parseDouble(your.replace("-", ""));
    } else if (your.contains("+")) {
        return summ - Double.parseDouble(your.replace("+", ""));
    } else {
        return summ  - Double.parseDouble(your);
    }

}

private static double getSumm(double sub, String your) {
    if (your.contains("-")) {
        return sub - Double.parseDouble(your.replace("-", ""));
    } else if (your.contains("+")) {
        return sub  + Double.parseDouble(your.replace("+", ""));
    } else {
        return sub + Double.parseDouble(your);
    }
}

定义两个全局变量double summdouble sub例如

public class MainCreator {

    public static void main(String[] args) throws IOException {
        FileInputStream file = new FileInputStream(new File("workbook.xls"));
        Workbook wb = new HSSFWorkbook(file);
        Sheet sh = wb.getSheetAt(0);
        int lastRownum = sh.getLastRowNum();
        double summ = 0;
        double sub = 0;

        for (int i = 0; i < lastRownum + 1; i++) {
            Row row = sh.getRow(i);

            Cell cell1 = row.getCell(1);
            Cell cell2 = row.getCell(2);

            if (cell1 != null && cell2 != null) {
                String cellValue1 = cell1.getStringCellValue();
                String cellValue2 = cell2.getStringCellValue();

                String stringNumber = cellValue2.split("=")[1].trim();
                if (cellValue1.contains("ADD")) {
                    if (cellValue2.split("=")[1].trim().contains("PATH DOES NOT EXIST")) {
                        System.out.println("Path Does Not Exist");
                    } else {
                        System.out.println(cellValue1 + "/" + stringNumber);
                        summ = getSumm(summ, stringNumber);
                    }

                } else if (cellValue1.contains("SUB")) {
                    if (cellValue2.split("=")[1].trim().contains("PATH DOES NOT EXIST")) {
                        System.out.println("Path Does Not Exist");
                    } else {
                        System.out.println(cellValue1 + "/" + stringNumber);
                        sub = getSubstraction(sub, stringNumber);
                    }
                } else {
                    System.out.println("Smt wrong");
                }
            }
        }
        System.out.println("Summ = " + summ);
        System.out.println("Sub = " + sub);
    }

    private static double getSubstraction(double main, String your) {
        if (your.contains("-")) {
            return main + Double.parseDouble(your.replace("-", ""));
        } else if (your.contains("+")) {
            return main - Double.parseDouble(your.replace("+", ""));
        } else {
            return main - Double.parseDouble(your);
        }
    }

    private static double getSumm(double main, String your) {
        if (your.contains("-")) {
            return main - Double.parseDouble(your.replace("-", ""));
        } else if (your.contains("+")) {
            return main + Double.parseDouble(your.replace("+", ""));
        } else {
            return main + Double.parseDouble(your);
        }
    }
}
于 2013-04-18T15:45:35.827 回答