2017-05-29 60 views
0

My input data访问数据

  1. 我有几行附加一个Excel列
  2. 数445.04,此列中重复3次。
  3. 我必须在java中编写一个逻辑,这将帮助我遍历200列以上的列并添加这些类型的值将导致为零。
  4. 这些数字可以不同,在操作时它们的可能性可能导致零的任何值。
  5. Here's another picture with different scenerio
  6. 在上述中,10和-3-7加上时可以做成零,5和-5也可以做成零。 7.如何编写可以访问此类场景的单个列的代码?如果需要额外的东西,留下评论。

这就是我想要达到...输出可以在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(); 
    } 

我想打印这是因为我必须根据它们的符号来计算它们的值并使其为零。

回答

0

您将需要使用某种类型的循环遍历行。这应该对你有所帮助。

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。

enter image description here

+0

嗨,你的代码告诉我如何遍历行,但每次行的结果只是成为与前一个的增加。你能帮我弄清楚为什么?请检查我已经分开正负号码的代码。 @Piyush –

+0

你想分别添加正数和负数吗? – Piyush

+0

请通过更新的代码并检查“预期结果”图像,这就是我想要实现的..谢谢! –

0
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(); 
    } 
} 

}

这就是我想要的目的。这将使行可以在可以操作的值处为零。 @Piyush