English 中文(简体)
我需要把位于贾瓦的一栏与阿帕奇帕伊组织连接起来。
原标题:I need to iterate to the end of a column in Java on an Excel Sheet with Apache POI

我在打下一列价值,直到打上空白。 我需要收集整个一栏并储存该栏,而没有其他价值。 我曾试图对空白、无效、0、“”和CELL_TYPE_BLANK(int = 3)进行核对,因此我无法回避一个无效的例外。 法典的刀切和错误如下。 我能做些什么? 这并不是整个方法或方案,只是相关内容。

String s = list[i];
//find the directory from the list array to locate the file
InputStream input = new FileInputStream(s);
//create a new workbook object to hold the excel file
Workbook wb = new XSSFWorkbook(input);
//create an arbitrary starting location
int column = 2; //absolutely the correct number
int rownum = 10;
//get the value from the first sheet
org.apache.poi.ss.usermodel.Sheet insheet = wb.getSheetAt(0);       
//of the second columm
Row row = insheet.getRow(rownum);
//in the 11th row (arbitrary number used to reduce iterations and skip whitespace)
Cell cell = row.getCell(column); 
System.out.println("Skimming sheet: " + insheet.getSheetName());
//iterate until the very end of the column is found
System.out.println("Cell value B" + (rownum-1) +  ": " + cell);

//3 denotes CELL_TYPE_BLANK
while (cell.getCellType() != 3 ) {
    //go to the next location
    //update to the next cell
    System.out.println("Cell value B" + rownum +  ": " + cell);
    row = insheet.getRow(rownum);
    if(row.getCell(column).getCellType() != 3){
        cell = row.getCell(column); //error occurs here, line 241
    }
    rownum++;
}


Exception in thread "main" java.lang.NullPointerException
    at FileTest.skim(FileTest.java:241)
    at FileTest.main(FileTest.java:121)
最佳回答

你再次发现这一错误,因为你试图进入的行子是无效的,而不是牢房。 请参看Apache POI Iterator

//taken from the example
Sheet sheet = wb.getsheetat(0);
for (Iterator<Row> rit = sheet.rowiterator(); rit.hasnext(); ) {
    Row row = rit.next();
    //now, based in your needs
    Cell cell = row.getCell(column);
    //do what you need with the row-column
}
问题回答

预留一行,以至最后一行,这将防止该例外情况发生:

 for(int i =0 ; i< insheet.getLastRowNum(); i++){
//do stuff
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签