Java 如何获取读取excel文件的最后一列索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2194284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:37:23  来源:igfitidea点击:

How to get the last column index reading excel file?

javaapache-poixssf

提问by marcosbeirigo

How do i get the index of the last column when reading a xlsxfile using the Apache POI API?

xlsx使用 Apache POI API读取文件时如何获取最后一列的索引?

There's a getLastRowNummethod, but I can't find nothing related to the number of columns...

有一个getLastRowNum方法,但我找不到与列数相关的任何内容......


EDIT: I'm dealing with XLSXfiles


编辑:我正在处理XLSX文件

采纳答案by Henning

I think you'll have to iterate through the rows and check HSSFRow.getLastCellNum()on each of them.

我认为您必须遍历行并检查HSSFRow.getLastCellNum()每一行。

回答by Helenice Silva

Check each Row and call Row.getLastCellNum()the max cell number is the last column number.

检查每一行并调用Row.getLastCellNum()最大单元格编号是最后一列编号。

Row r = sheet.getRow(rowNum);
int maxCell=  r.getLastCellNum();

回答by Arpan Saini

To get to know the last column that has value of any row , First you need to get the row and then you can find the last column that has value

要了解具有任何行值的最后一列,首先您需要获取该行,然后您可以找到具有值的最后一列

Syntax :

句法 :

sheet.getrow(RowNumber).getLastCellNum();

RowNumber--> is the row number for which you want to know the last column that has value

RowNumber--> 是您想知道具有值的最后一列的行号

回答by Darshan Lal

Try this function:

试试这个功能:

private void maxExcelrowcol() {
    int row, col, maxrow, maxcol;

    //Put file name here for example filename.xls
    String filename = "filename.xls";
    static String TAG = "ExelLog";

    //you can use 'this' in place of context if you want
    Context context = getApplicationContext();

    try {
        // Creating Input Stream
        File file = new File(context.getExternalFilesDir(null), filename);
        FileInputStream myInput = new FileInputStream(file);

        // Create a POIFSFileSystem object
        POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

        // Create a workbook using the File System
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

        // Get the first sheet from workbook
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);

        //Row iterator 
        Iterator rowIter = mySheet.rowIterator();

        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            //Cell iterator for iterating from cell to next cell of a row
            Iterator cellIter = myRow.cellIterator();
            while (cellIter.hasNext()) {
                HSSFCell myCell = (HSSFCell) cellIter.next();

                row = myCell.getRowIndex();
                col = myCell.getColumnIndex();

                if (maxrow < row) {
                    maxrow = row;
                }
                if (maxcol < col) {
                    maxcol = col;
                }
            }
        }
    } catch(FileNotFoundException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }
}