2017-05-29 61 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