简体   繁体   English

使用Apache POI从Excel的单列访问数据

[英]Access data from excel in single column using Apache POI

My input data 我的输入数据

  1. I have attached an Excel column with few rows 我已经附上了几行的Excel列
  2. Number 445.04 is repeated thrice in this column. 在此列中,将编号445.04重复三次。
  3. I have to write a logic in java which will help me iterate through this column with 200+ rows and add these type of values will result in zero. 我必须用Java编写逻辑,这将帮助我遍历具有200多个行的该列,并添加这些类型的值将得出零。
  4. The numbers can be different, take up any value where the possibility of them when operated can result in zero. 这些数字可以不同,可以取任何值,否则在操作时它们可能导致零。
  5. Here's another picture with different scenerio 这是另一幅场景不同的照片
  6. In the above, 10 and -3-7 when added can be made zero and also 5 and -5 can also be made zero. 在上面,将10和-3-7相加时可以设为零,也可以将5和-5设为零。 7.How can i write the code which can access a single column for this type of scenario? 7,我该如何编写可访问这种情况下单列的代码? Leave a comment if anything additional is required. 如有其他要求,请发表评论。

This is what I'm trying to achieve...the output can be printed on console of the IDE instead in the excel itself 这就是我要实现的目标...输出可以在IDE的控制台上打印,而不是在Excel本身中打印

Expected result 预期结果


    Iterator<Row> iterator = firstSheet.iterator();
    List<Double> posValue = new ArrayList<Double>();
    List<Double> negValue = new ArrayList<Double>();
    while(iterator.hasNext())//goes down the row, until there is nothing
    {    
        Row nexRow = iterator.next();
        if(nexRow.getRowNum()!=0){
            value = nexRow.getCell(3).getNumericCellValue();

            if(value>0){
                posValue.add(value);
            }
            else if(value <0){
                //System.out.println("ehre");
                negValue.add(value);
            }
        }
        Iterator<Double> pIterator = posValue.iterator();
        Iterator<Double> nIterator = negValue.iterator();

        for(int i = 0; i<posValue.size();i++){
            for(int j=0; j<negValue.size(); j++){
                //System.out.println(negValue.get(j));
                result= posValue.get(i)+negValue.get(j);
            }
        }
        if(result==0){
            System.out.println(result+"    "+nexRow.getRowNum());
        }
        workbook.close();
        inputStream.close();
    }

I thought of this because I have to calculate the values on the basis their signs and make it zero. 我想到这一点是因为我必须根据其符号计算值并将其设为零。

You will need to iterate though the rows using a loop of some kind. 您将需要使用某种循环来遍历行。 This should help you. 这应该对您有帮助。

public static void main(String[] args) throws IOException {
    String excelFilePath = "Book1.xlsx";
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath));

    Workbook workbook = new XSSFWorkbook(inputStream);
    XSSFSheet firstSheet = (XSSFSheet) workbook.getSheetAt(0);
    Iterator<Row> iterator = firstSheet.iterator();
    iterator.next();// skip the header row
    Double result = 0.0;

    while (iterator.hasNext()) {
        Row nextRow = iterator.next();
        result += nextRow.getCell(0).getNumericCellValue();
    }
    System.out.println(result);

    workbook.close();
    inputStream.close();
}

Excel doc. Excel文件

在此处输入图片说明

    Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet firstSheet = workbook.getSheetAt(0);
    double addedValue = 0;
    double value = 0;
    List<Double> value1 = new ArrayList<Double>();
    double result =0;
    Iterator<Row> iterator = firstSheet.iterator();
    List<Double> posValue = new ArrayList<Double>();
    double negValue = 0;
    while(iterator.hasNext())//goes down the row, until there is nothing
    {    
        Row nexRow =iterator.next();
        if(nexRow.getRowNum()!=0)
        {
            value = nexRow.getCell(3).getNumericCellValue();
            value1.add(value);
            for(int i=0; i<value1.size();i++)
            {
                //For storing all the values 
                addedValue=value1.get(i);


                /* Checking if the values are negative or not 
          If it is, then convert it to positive */
                if(addedValue<0)
                {                         
                    negValue=Math.abs(value1.get(i));
                }
            }
            /*checking for the numbers which can be operated to make it zero
               and then displaying the row numbers which have been cancelled out*/

            if(addedValue==negValue)
            {
                result = addedValue-negValue;
                nexRow.getCell(3).setCellValue(0.0);
                System.out.println(result+"  Matched rows--> "+nexRow.getRowNum());

            }
        }
        workbook.close();
        inputStream.close();
    }
}

} }

This is what I was trying to achieve. 这就是我想要实现的目标。 It will make the rows zero where the values can be operated on. 它将使行可以在其中操作值的位置为零。 @Piyush @Piyush

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM