2017-05-25 26 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 –