如何在不使用apache poi中的公式单元格的情况下获取行数据

时间:2021-10-30 20:23:06

I am working on a program where the row data from excel sheet should be displayed as output. An id is been taken as input which will map to the particular row for displaying output. I am using apache poi on java to write the code. I want to display the output without using cells which I currently used in my prog. Is there any function that directly prints row data for row object?

我正在开发一个程序,其中来自excel表的行数据应显示为输出。将id作为输入,将映射到用于显示输出的特定行。我在java上使用apache poi编写代码。我想显示输出而不使用我目前在我的编程中使用的单元格。是否有任何函数直接打印行对象的行数据?

 package exceldata;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;

 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.*;

 public class exceldata {

 private static final String FILE_NAME = "C:\\Users\\agandiko\\Desktop\\data.xlsx";

    public static void main(String[] args) {

        try {System.out.println("Enter the id:");
            Scanner s=new Scanner(System.in);
           String field=s.next();
           int on=0;
           int n=0;
           int cc=1;
            FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
            Workbook wb = new XSSFWorkbook(excelFile);
            DataFormatter formatter = new DataFormatter();
             for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                 System.out.println();
                 System.out.println();
                 System.out.println("Data for Sheet"+" "+(i+1)); 
                 System.out.println("-----------------------------------");
                 System.out.println();
                 System.out.println();
            Sheet datatypeSheet = wb.getSheetAt(i);
            Iterator<Row> iterator = datatypeSheet.iterator();
            int noOfColumns = datatypeSheet.getRow(0).getPhysicalNumberOfCells();
            while (iterator.hasNext()) {

                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();

                while (cellIterator.hasNext()) {

                    Cell currentCell = cellIterator.next();
                    Cell k=currentCell;
                    //getCellTypeEnum shown as deprecated for version 3.15
                    //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
                    String val=formatter.formatCellValue(currentCell).toString();

                     if((val.equals(field)))
                     {
                         while(cellIterator.hasNext()){
                    if(cc==1){System.out.print(formatter.formatCellValue(k).toString()+"  ");}
                    else{currentCell = cellIterator.next();System.out.print(formatter.formatCellValue(currentCell).toString()+"  ");}

                         cc++;
                         }

                       // System.out.print(currentRow.toString()+"  ");
                       // currentRow = iterator.next();
                       // cellIterator = currentRow.iterator();
                       // on=1;n++;}

                          //System.out.println(cc);
                }

               // if(n==noOfColumns){System.out.println();n=0;}

               //on=0;


            }

        cc=1;   }
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

}

1 个解决方案

#1


0  

Here's how I understand the use case: look for some id (the code assumes it to be in the first column of each sheet) and print the whole row if it matches the id that was entered; here's how this could be solved (depending on the data in your sheet, some null checks might need to be added, but in general it works "as is"):

以下是我理解用例的方法:查找一些id(代码假定它位于每个工作表的第一列)并打印整行,如果它与输入的id匹配;这是如何解决的(取决于工作表中的数据,可能需要添加一些空检查,但一般情况下它“按原样”工作):

public static void main(String[] args) throws IOException {

        System.out.println("Enter the id:");
        Scanner s = new Scanner(System.in);
        String id = s.next();

        FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
        Workbook wb = new XSSFWorkbook(excelFile);
        DataFormatter formatter = new DataFormatter();
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            System.out.println();
            System.out.println();
            System.out.println("Data for Sheet" + " " + (i + 1));
            System.out.println("-----------------------------------");
            System.out.println();
            System.out.println();
            Sheet datatypeSheet = wb.getSheetAt(i);
            Iterator<Row> iterator = datatypeSheet.iterator();

            while (iterator.hasNext()) {
                Row currentRow = iterator.next();
                if(id.equals(formatter.formatCellValue(currentRow.getCell(0)))) {
                    currentRow.cellIterator().forEachRemaining(c -> System.out.print(formatter.formatCellValue(c) + " "));
                }
            }
        }
        wb.close();
        s.close();
}

#1


0  

Here's how I understand the use case: look for some id (the code assumes it to be in the first column of each sheet) and print the whole row if it matches the id that was entered; here's how this could be solved (depending on the data in your sheet, some null checks might need to be added, but in general it works "as is"):

以下是我理解用例的方法:查找一些id(代码假定它位于每个工作表的第一列)并打印整行,如果它与输入的id匹配;这是如何解决的(取决于工作表中的数据,可能需要添加一些空检查,但一般情况下它“按原样”工作):

public static void main(String[] args) throws IOException {

        System.out.println("Enter the id:");
        Scanner s = new Scanner(System.in);
        String id = s.next();

        FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
        Workbook wb = new XSSFWorkbook(excelFile);
        DataFormatter formatter = new DataFormatter();
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            System.out.println();
            System.out.println();
            System.out.println("Data for Sheet" + " " + (i + 1));
            System.out.println("-----------------------------------");
            System.out.println();
            System.out.println();
            Sheet datatypeSheet = wb.getSheetAt(i);
            Iterator<Row> iterator = datatypeSheet.iterator();

            while (iterator.hasNext()) {
                Row currentRow = iterator.next();
                if(id.equals(formatter.formatCellValue(currentRow.getCell(0)))) {
                    currentRow.cellIterator().forEachRemaining(c -> System.out.print(formatter.formatCellValue(c) + " "));
                }
            }
        }
        wb.close();
        s.close();
}