使用java在excel数据表上执行操作

时间:2017-05-25 06:54:49

标签: java excel

excel表中有四列

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

1 个答案:

答案 0 :(得分: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行之外的每一行,并且与上面的示例完全相同,并编写&#34;匹配&#34;在列E中,如果列B - 列D等于列C:

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