在Apache的POI跳过空白Excel单元 [英] Skipping Blank Excel Cells in Apache POI

查看:567
本文介绍了在Apache的POI跳过空白Excel单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来了Apache POI,但我想要做的是阅读,虽然一个Excel文件(.xls),并将它放入一个ArrayList中存储,这样我以后可以操纵的。我可以得到整个表,但我的问题就是:我得到整个党表(〜54183行)

I'm new to the Apache POI, but what I want to do is read though an Excel file (.xls) and put that into a ArrayList to store so that I can later manipulate. I can get the whole sheet, but my problem is just that: I get the whole dang sheet (~54183 rows).

我要跳过的是空白单元格,它的类型是3出于某些原因,当我system.out.print ArrayList中,它拥有所有的空白单元格在那里。

I want to skip over the cells that are blank, which is of type 3. For some reason, when I system.out.print the ArrayList, it has all the blank cells in there.

有没有办法跳过这些,而不是将它们添加到ArrayList,我试图创造?

Is there a way to skip over those and not add them to the ArrayList I'm trying to create?

我有code以下位:

public ArrayList readExcelFile(String filePath) throws IOException {
    ArrayList cellVectorHolder = new ArrayList();
    try {
        FileInputStream inputFile = new FileInputStream(filePath);
        POIFSFileSystem myFileSystem = new POIFSFileSystem(inputFile);
        HSSFWorkbook wkbk = new HSSFWorkbook(myFileSystem);
        wb = wkbk;
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            HSSFSheet wkSheet = wkbk.getSheetAt(i);
            for (Row row : wkSheet) {
                ArrayList cellVector = new ArrayList();
                for (Cell cell : row) {
                    if(cell.getCellType() != 3){
                        cellVector.add(cell);
                    }
                }
                cellVectorHolder.add(cellVector);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cellVectorHolder;
}

不介意ArrayList的名字......我用向量,直到我终于发现,因为1.2或类似的东西,他们去preciated。

Don't mind the ArrayList names...I was using Vectors until I finally discovered they were depreciated since 1.2 or something like that.

推荐答案

添加单元格列表之前,您可以检查单元格的值。是这样的:

Before adding the cell to the List, you can check the cell value. Something like:

if (cell.getStringCellValue() != null &&  cell.getStringCellValue().length() != 0) {   
    cellVector.add(cell); 
}

您只有在有添加一些内容的单元格。

You only add the cell if there is some content.

这篇关于在Apache的POI跳过空白Excel单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆