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

使用java在excel数据表上执行操作,java,excel,Java,Excel,excel工作表中有四列 我需要在三列上执行操作,并在第四列上显示结果 如果我执行B9-D9,那么结果等于C9 发生这种情况时,输出应为“匹配” 我需要知道如何访问每一行和每一列,并对其执行必要的操作。 看看你是否能帮助我,如果需要任何额外的细节,请告诉我 package com.infy; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import jav

excel工作表中有四列

  • 我需要在三列上执行操作,并在第四列上显示结果
  • 如果我执行B9-D9,那么结果等于C9
  • 发生这种情况时,输出应为“匹配”
  • 我需要知道如何访问每一行和每一列,并对其执行必要的操作。 看看你是否能帮助我,如果需要任何额外的细节,请告诉我

    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();
    
            }
    
        }
    
    package.info;
    导入java.io.File;
    导入java.io.FileInputStream;
    导入java.io.FileNotFoundException;
    导入java.io.IOException;
    导入java.util.Iterator;
    导入org.apache.poi.ss.usermodel.Cell;
    导入org.apache.poi.ss.usermodel.Row;
    导入org.apache.poi.ss.usermodel.Sheet;
    导入org.apache.poi.ss.usermodel.工作簿;
    导入org.apache.poi.xssf.usermodel.xssf工作簿;
    公共类重新匹配{
    公共静态void main(字符串[]args)引发IOException,
    FileNotFoundException{
    //TODO自动生成的方法存根
    字符串excelFilePath=“C:/Users/akshay.kuchankar/Documents/demo.xlsx”;
    FileInputStream inputStream=新建FileInputStream(新建
    文件(excelFilePath));
    工作簿=新XSSF工作簿(inputStream);
    Sheet firstSheet=工作簿。getSheetAt(0);
    迭代器迭代器=firstSheet.Iterator();
    while(iterator.hasNext()){
    行nextRow=iterator.next();
    迭代器cellIterator=nextRow.cellIterator();
    while(cellIterator.hasNext()){
    Cell=cellIterator.next();
    //执行操作的基本方法或语法应该是什么??
    }
    System.out.println();
    }
    workbook.close();
    inputStream.close();
    }
    }
    


    for(inti=0;i查看库的API:

    您可以通过多种方式与单元格交互,例如:
    cell.setCellValue(“我的新值”);

    至于如何从单元格中获取值并对照它们进行检查,您可以做一些类似于此示例的事情,我们做一些数学运算并比较值:

    //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();
            }
        }
    }
    
    Iterator Iterator=firstSheet.Iterator();
    while(iterator.hasNext()){
    行nextRow=iterator.next();
    //检查以确保跳过第一行,因为该行包含所有列名:
    如果(nextRow.getRowNum()!=0){
    //获取当前行的单元格值
    String columnB=nextRow.getCell(1.getStringCellValue();
    String columnC=nextRow.getCell(2.getStringCellValue();
    String columnD=nextRow.getCell(3.getStringCellValue();
    试一试{
    //做数学B列-D列
    double value=double.parseDouble(columnB)-double.parseDouble(columnD);
    //检查C列是否与(B列-D列)的结果匹配
    if(value==Double.parseDouble(columnC)){
    //如果匹配,则将E列中的文本设置为“匹配”
    nextRow.getCell(4.setCellValue(“匹配”);
    //打印到控制台,显示行是否匹配
    System.out.println(“行”+(nextRow.getRowNum()+1)+“匹配”);
    }
    }
    捕获(NumberFormatException nfe){
    //此处不执行任何操作,如果单元格包含文本而不是数字,则会发生这种情况
    }
    捕获(NullPointerException npe){
    //如果发生了其他情况,您可能也可以忽略这一点,但抛出堆栈跟踪是值得的,以防此代码出现问题
    npe.printStackTrace();
    }
    }
    }
    
    我理解了逻辑部分。谢谢!但我仍然不知道如何使用for循环访问所有行和列。既然有四列,我应该使用四个for循环吗?请让我知道@sorifiendI建议使用一个循环来更改行号,因为每次的冒号都是相同的。在每个循环中,只有g设置所有4列的值,如我上面的示例所示。你现在能告诉我怎么做吗?看看我做了什么,我知道代码离概念不远。但这正是我想要理解的@sorifiend@AkshayKumar见我上面的编辑,展示了如何对工作表中的每一行执行相同的操作。谢谢你!!你帮了我很大的忙!!我我试试这个。如果有什么事情发生,我会回来的!!请避免使用大写字母(就像在图书馆里喊叫一样)好的…我会确保下次不会发生@agerom
    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();
            }
        }
    }