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

发布于 2024-08-20 03:33:09 字数 167 浏览 6 评论 0原文

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

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


编辑: 我正在处理 XLSX 文件

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

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

EDIT:
I'm dealing with XLSX files

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半岛未凉 2024-08-27 03:33:10

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

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

难如初 2024-08-27 03:33:10

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

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

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

Row r = sheet.getRow(rowNum);
int maxCell=  r.getLastCellNum();
狼性发作 2024-08-27 03:33:10

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

语法:

sheet.getrow(RowNumber).getLastCellNum();

RowNumber -->是您想要了解具有值的最后一列的行号

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

绅刃 2024-08-27 03:33:10

试试这个功能:

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

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文