2017-05-25 110 views
0

上的Excel數據表執行操作有在excel表四列用java

  1. 我需要在三列執行操作和顯示在第四個結果。
  2. Image with data in excel
  3. 如果我執行B9-D9,那麼結果等於C9。
  4. 發生這種情況時,輸出應爲「匹配」。
  5. 我需要知道如何訪問每個行和列並對其執行必要的操作。 看看你是否可以幫助我,讓我知道是否需要任何額外的細節。

    package com.infy; 
    
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.util.Iterator; 
    
    
    
    import org.apache.poi.ss.usermodel.Cell; 
    import org.apache.poi.ss.usermodel.Row; 
    import org.apache.poi.ss.usermodel.Sheet; 
    import org.apache.poi.ss.usermodel.Workbook; 
    import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
    
    
    public class ReconMatch { 
        public static void main(String[] args) throws IOException, 
    FileNotFoundException{ 
         // TODO Auto-generated method stub 
         String excelFilePath ="C:/Users/akshay.kuchankar/Documents/demo.xlsx"; 
         FileInputStream inputStream = new FileInputStream(new 
    File(excelFilePath)); 
    
         Workbook workbook = new XSSFWorkbook(inputStream); 
         Sheet firstSheet = workbook.getSheetAt(0); 
         Iterator<Row> iterator = firstSheet.iterator(); 
    
         while (iterator.hasNext()) { 
          Row nextRow = iterator.next(); 
          Iterator<Cell> cellIterator = nextRow.cellIterator(); 
    
          while (cellIterator.hasNext()) { 
           Cell cell = cellIterator.next(); 
    
           //what should be the basic approach or the syntax to perform the operaiton?? 
          } 
          System.out.println(); 
          } 
          workbook.close(); 
          inputStream.close(); 
    
         } 
    
        } 
    

    for(int i= 0; i<firstSheet.getRow(0).getCell(0).getNumericCellValue(); i++) 
          { 
    
           FGAmount = firstSheet.getRow(1).getCell(1).getNumericCellValue(); 
           // System.out.println(FGAmount); 
           difference = firstSheet.getRow(1).getCell(3).getNumericCellValue(); 
           value = FGAmount + difference; 
    
          } 
            alconAmount = firstSheet.getRow(1).getCell(2).getNumericCellValue(); 
    
         //   result = firstSheet.getRow(1).getCell(4).getStringCellValue(); 
    
         }    
        } 
        try { 
         if(value== alconAmount){ 
          firstSheet.getRow(1).getCell(4).setCellValue("Manual Matched"); 
          System.out.println("matched"); 
         }      
    
        } catch (Exception e) { 
         e.printStackTrace(); 
         System.out.println(e); 
        } 
        // System.out.println(result); 
    
        workbook.close(); 
        inputStream.close(); 
    
+0

請避免使用大寫字母(這就像在圖書館裏大喊大叫) – amonk

+0

好吧...下次我不會再發生@agerom –

回答

0

看看該API爲您的圖書館:

https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html

您可以在許多方面的細胞,例如互動: cell.setCellValue("My new value");

至於如何獲取值形成細胞和核對他們,你可以做一些事情,有點像這個例子中,我們做了一下數學和比較值:

//Value of row 9, column 1 (column B) 
String B9 = firstSheet.getRow(9).getCell(1).getStringCellValue(); 

//Value of row 9, column 2 (column C) 
String D9 = firstSheet.getRow(9).getCell(2).getStringCellValue(); 

//Value of row 9, column 3 (column D) 
String D9 = firstSheet.getRow(9).getCell(3).getStringCellValue(); 

//do math B9 - D9 
double value = Double.parseDouble(B9) - Double.parseDouble(D9); 

//check if C9 matches the result of B9 - D9 
if(value == Double.parseDouble(C9)) 
{ 
    //if it matches set cell E9 to display "Matched" and print out a message 
    firstSheet.getRow(9).getCell(4).setCellValue("matched"); 
    System.out.println("matched"); 
} 

我沒有測試此代碼,但它應該指向正確的方向。

顯然有一些事情是你應該做的像把這個在一個循環中,而不是硬編碼值,也應檢查列名的確保你獲得你的價值觀前右列,然後你應該檢查該單元格是一個數字,而不是文字等。


編輯回覆您的評論。這裏是一些代碼,將在表格中除第1行以外的每一行都執行,並且將執行與上述示例中完全相同的操作,如果列B - 列D等於列C,則在E列中寫入「匹配」:

Iterator<Row> iterator = firstSheet.iterator(); 

while (iterator.hasNext()) { 
    Row nextRow = iterator.next(); 

    //Check to make sure we skip the first row because that has all the column names: 
    if (nextRow.getRowNum() != 0){ 
     //Get cell values of the current row 
     String columnB = nextRow.getCell(1).getStringCellValue(); 
     String columnC = nextRow.getCell(2).getStringCellValue(); 
     String columnD = nextRow.getCell(3).getStringCellValue(); 

     try{ 
      //do math column B - column D 
      double value = Double.parseDouble(columnB) - Double.parseDouble(columnD); 

      //check if column C matches the result of (column B - column D) 
      if(value == Double.parseDouble(columnC)){ 
       //if it matches set text in column E to "matched" 
       nextRow.getCell(4).setCellValue("matched"); 
       //print to console showing if a row matched 
       System.out.println("Row " + (nextRow.getRowNum()+1) + " matched"); 
      } 
     } 
     catch(NumberFormatException nfe){ 
      //do nothing here, this will happen if a cell contains text instead of numbers 
     } 
     catch(NullPointerException npe){ 
      //Something else happened, you can probably ignore this as well but it will pay to throw a stack trace just in case something is wrong with this code 
      npe.printStackTrace(); 
     } 
    } 
} 
+0

我理解了邏輯部分。謝謝!但是我仍然不知道如何訪問所有具有for循環的行和列。由於有四列我應該使用四個循環?請讓我知道@sorifiend –

+0

我推薦一個單一的循環來改變行號,因爲每次冒號都是一樣的。在每個循環中,只需獲取上述示例中所有4列的值。 – sorifiend

+0

現在你能告訴我我該怎麼做嗎?看看我做了什麼,我知道代碼是沒有在這個概念附近。但這正是我想了解的@sorifiend –