Java 无法通过poi打印范围内单元格的值

Java 无法通过poi打印范围内单元格的值,java,apache-poi,Java,Apache Poi,我有下面的java程序,它也在尝试通过JavaPOI读取excel表,首先看到下面的excel表格式,如下所示 abcRef 21 ABC 20 ERT 60 FGT 57 abcRef SMS Seal Total 12 45 DRT 3000 23 36 QWE 2000 subtotal

我有下面的java程序,它也在尝试通过JavaPOI读取excel表,首先看到下面的excel表格式,如下所示

abcRef   21
ABC      20
ERT      60
FGT      57


  abcRef   SMS  Seal         Total        
    12      45   DRT         3000      
    23      36   QWE         2000  
                 subtotal    5000



                            abcRef   SMS  Seal     Total        
                             18      25   hRT      1000      
                             29      16   tWE      2100  
                                       subtotal    3100


RTY 57
TDZ 21
YUI 98
正如你在上面看到的,我需要捕捉所有表范围,从abcRef开始,到Total列结束,因此我添加了header单元格的条件,首先捕捉这个组合,换句话说,首先找到header单元格

abcRef+SMS+Seal+Total 
这是我能够实现的,所以我下面的程序首先遍历所有行,然后尝试找到组合的标题单元格,然后打印它正在执行的所有值

但我需要你的建议的挑战很少

我只想在控制台上打印表格,所以我想随时中断我的程序

  • 1) 遇到任何空行
  • 2) 任何单元格值为的行 小计
  • 3) 遇到的任何标题单元格都是 (abcRef+SMS+印章+总计)
因此,请告知我如何定制我的以下代码,以便我可以在我的控制台上打印以下内容

abcRef   SMS  Seal         Total        
    12      45   DRT         3000      
    23      36   QWE         2000 

 abcRef   SMS  Seal     Total        
  18      25   hRT      1000      
  29      16   tWE      2100  
下面是我在下面程序rite now中的一段代码,其中只打印标题行,而不是上面显示的我想要的

public class abc{
    public static void main(String[] args) throws Exception {


        FileInputStream file = null ;
         try {


                HSSFRow r =findgetRowNo();

                getAllColumnFromRow(r);





             }

             catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{

                file.close();
            }

    }



    public static void getAllColumnFromRow(HSSFRow RowObject){
        Iterator<Cell> itr = RowObject.iterator();
        HSSFRow headerRow = RowObject.getSheet().getRow(0);
        String cellValue = "";
        String information = "";
        int headerCellCount = 0;
        //to avoid first column


        while(itr.hasNext()){
            Cell cell = itr.next();
            Cell headerValue = headerRow.getCell(headerCellCount++);
            switch(cell.getCellType()) {
            case HSSFCell.CELL_TYPE_BOOLEAN:
                cellValue = cell.getBooleanCellValue() +"";
                information = information + " " +headerValue+" - "+cellValue+ "; ";
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                cellValue = cell.getNumericCellValue() + "";
                information = information + " " +headerValue+" - "+cellValue+ "; ";
                break;
            case HSSFCell.CELL_TYPE_STRING:
                cellValue = cell.getStringCellValue();
                System.out.println(cellValue);
                information = information + " " +headerValue+" - "+cellValue+ "; ";
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                break;
            }
        }
        System.out.println("@@@@");
        }


    public static int findHeaderCell(HSSFSheet firstSheet) {



        final String[] headers1 = {             "abcRef", "SMS", "Seal", "Total "};
        List<String> listHeader=Arrays.asList(headers1);

          for (Row row : firstSheet) {


                for (Cell cell : row) {
                      if(listHeader.contains(cell.getStringCellValue())) {


                    int row1 = cell.getRowIndex() + 1;
                    int col = cell.getColumnIndex();


                    if (firstSheet.getRow(row1) == null)
                      throw new RuntimeException("Row " + row1 + 1 + " is empty!");
                    Cell startOfFirstDataRow = firstSheet.getRow(row1).getCell(col);
                    if (startOfFirstDataRow == null) {
                      CellReference ref = new CellReference(row1, col);
                      throw new RuntimeException("Data not found at " + ref.formatAsString());
                    }


                 // now we take row from above to be the Row object where we seek our headers
                    int last = row.getLastCellNum();
                    for (int c = row.getFirstCellNum(); c < last; c++) {
                        int h = 0;
                        // check if the cell at (c + h) has the required value
                      for (; h < listHeader.size() && c + h < last; h++) {

                            if (!listHeader.get(h).equals(row.getCell(c + h).getStringCellValue())) {
                                System.out.println("headers not match as Expected");
                                break; // if the cell value differs from our header
                            }
                        }
                        int t;

                        if (h == listHeader.size()) // this means the break was never invoked 
                        System.out.println("headers Matched as Expected");
                        return row.getRowNum();
                        //  return c; // found it
                    }
                 // not found

                    return -1;
                  }
                }
              }
              throw new RuntimeException("TradingRef header cell not found!");
    }






public static HSSFRow findgetRowNo() throws Exception {

    FileInputStream file = null ;

     file = new FileInputStream(new File("C:\\abc.xls"));
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet firstSheet1 = workbook.getSheetAt(0);
        Iterator<Row> rowItr = firstSheet1.iterator();
        final String[] headers1 = { "abcRef", "SMS", "Seal", "Total "}
        List<String> listHeader=Arrays.asList(headers1);

          for (Row row : firstSheet1) {


                for (Cell cell : row) {
                      if(listHeader.contains(cell.getStringCellValue())) {


                    int row1 = cell.getRowIndex() + 1;
                    int col = cell.getColumnIndex();


                    if (firstSheet1.getRow(row1) == null)
                      throw new RuntimeException("Row " + row1 + 1 + " is empty!");
                    Cell startOfFirstDataRow = firstSheet1.getRow(row1).getCell(col);
                    if (startOfFirstDataRow == null) {
                      CellReference ref = new CellReference(row1, col);
                      throw new RuntimeException("Data not found at " + ref.formatAsString());
                    }


                 // now we take row from above to be the Row object where we seek our headers
                    int last = row.getLastCellNum();
                    for (int c = row.getFirstCellNum(); c < last; c++) {
                        int h = 0;
                        // check if the cell at (c + h) has the required value
                      for (; h < listHeader.size() && c + h < last; h++) {

                            if (!listHeader.get(h).equals(row.getCell(c + h).getStringCellValue())) {
                                System.out.println("headers not match as Expected");
                                break; // if the cell value differs from our header
                            }
                        }
                        int t;

                        if (h == listHeader.size()) // this means the break was never invoked 
                        System.out.println("headers Matched as Expected");

                        row.getPhysicalNumberOfCells();
                        return (HSSFRow) row;
                        //  return c; // found it
                    }
                 // not found

                    return null;
                  }
                }
              }
              throw new RuntimeException("abcRef header cell not found!");


    }





}
公共类abc{
公共静态void main(字符串[]args)引发异常{
FileInputStream文件=null;
试一试{
HSSFRow r=findgetRowNo();
getAllColumnFromRow(r);
}
catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
file.close();
}
}
公共静态void getAllColumnFromRow(HSSFRow RowObject){
迭代器itr=RowObject.Iterator();
HSSFRow headerRow=RowObject.getSheet().getRow(0);
字符串cellValue=“”;
字符串信息=”;
int headerCellCount=0;
//避免第一列
while(itr.hasNext()){
Cell-Cell=itr.next();
Cell headerValue=headerRow.getCell(headerCellCount++);
开关(cell.getCellType()){
案例HSSFCell.CELL\u类型\u布尔值:
cellValue=cell.getBooleanCellValue()+“”;
信息=信息+“”+headerValue+“-”+cellValue+“;”;
打破
案例HSSFCell.CELL\u类型\u数字:
cellValue=cell.getNumericCellValue()+“”;
信息=信息+“”+headerValue+“-”+cellValue+“;”;
打破
案例HSSFCell.CELL\u类型\u字符串:
cellValue=cell.getStringCellValue();
系统输出打印项次(cellValue);
信息=信息+“”+headerValue+“-”+cellValue+“;”;
打破
案例HSSFCell.CELL\u类型\u空白:
打破
}
}
System.out.println(“@@@”);
}
公共静态内部findHeaderCell(HSSF表第一张){
最后一个字符串[]headers1={“abcRef”、“SMS”、“Seal”、“Total”};
List listHeader=Arrays.asList(headers1);
用于(行:第一页){
用于(单元格:行){
if(listHeader.contains(cell.getStringCellValue())){
int row1=cell.getRowIndex()+1;
int col=cell.getColumnIndex();
if(firstSheet.getRow(行1)==null)
抛出新的RuntimeException(“行“+row1+1+”为空!”);
Cell startOfFirstDataRow=firstSheet.getRow(行1.getCell(列);
if(startOfFirstDataRow==null){
CellReference ref=新的CellReference(第1行,第2列);
抛出新的RuntimeException(“在“+ref.formataString()处找不到数据”);
}
//现在,我们从上面取row作为row对象,在其中查找标题
int last=row.getLastCellNum();
for(int c=row.getFirstCellNum();c