Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用POI读取excel文件的另一行_Java_Excel_Apache Poi - Fatal编程技术网

Java 使用POI读取excel文件的另一行

Java 使用POI读取excel文件的另一行,java,excel,apache-poi,Java,Excel,Apache Poi,使用ApachePOI,我试图读取excel文件。该文件有1000行和1列。使用此代码: XSSFSheet ws = workbook.getSheetAt(0); Iterator< Row > rowIt = ws.iterator(); XSSFRow row; int i = 0; while ( rowIt.hasNext() ) { row = (XSSFRow) rowIt.next(); Iterato

使用ApachePOI,我试图读取excel文件。该文件有1000行和1列。使用此代码:

    XSSFSheet ws = workbook.getSheetAt(0);
    Iterator< Row > rowIt = ws.iterator();
    XSSFRow row;
    int i = 0;
    while ( rowIt.hasNext() ) {
      row = (XSSFRow) rowIt.next();
      Iterator< Cell > cellIt = row.cellIterator();
      while ( cellIt.hasNext() ) {
        Cell cell = cellIt.next();
        my_array[ i ] = cell.getStringCellValue();
      }
      ++i;
    }
XSSFSheet ws=workbook.getSheetAt(0);
迭代器rowIt=ws.Iterator();
XSSFRow行;
int i=0;
while(rowIt.hasNext()){
row=(XSSFRow)rowIt.next();
迭代器cellIt=row.cellIterator();
while(cellIt.hasNext()){
Cell=cellIt.next();
my_数组[i]=cell.getStringCellValue();
}
++一,;
}
它似乎读取1001行,因为最后一行是“”,所以my_数组获取无效字符串。有没有办法解决这个问题?我希望
rowIt.hasneat()
对此负责,但它并没有按预期工作

该文件有1000行和1列:您必须指定正在读取的列。 下面是使用此excel文件指定列的示例:

公共类测试讲座{
公共静态void main(字符串[]args)引发IOException{
List mys_List=new ArrayList();
FileInputStream文件=新的FileInputStream(新文件(“test.xlsx”);
//获取XLS文件的工作簿实例
XSSF工作簿=新XSSF工作簿(文件);
//从工作簿中获取第一张工作表
XSSFSheet ws=workbook.getSheetAt(0);
//获取当前工作表中所有行的迭代器
迭代器rowIt=ws.Iterator();
while(rowIt.hasNext()){
Row=rowIt.next();
迭代器cellIt=row.Iterator();
while(cellIt.hasNext()){
Cell=cellIt.next();
int columnIndex=cell.getColumnIndex();
开关(列索引){
案例2:
mys_list.add(cell.getStringCellValue());
打破
}
}
}
System.out.println(mys_list.size());
for(字符串g:mys_列表){
系统输出打印ln(g);
}
}
}

public class TestLecture {

public static void main(String[] args) throws IOException{

    List<String> mys_list= new ArrayList<String>();
    FileInputStream file = new FileInputStream(new File("test.xlsx"));

    //Get the workbook instance for XLS file 
    XSSFWorkbook workbook = new XSSFWorkbook (file);

    //Get first sheet from the workbook
    XSSFSheet ws = workbook.getSheetAt(0);

    //Get iterator to all the rows in current sheet
    Iterator<Row> rowIt = ws.iterator();


     while (rowIt.hasNext()) {

         Row row = rowIt.next();
         Iterator<Cell> cellIt = row.iterator();

         while (cellIt.hasNext()) {

             Cell cell = cellIt.next();
             int columnIndex = cell.getColumnIndex();
             switch (columnIndex) {
             case 2:

                 mys_list.add(cell.getStringCellValue());

                 break;


             }

         }


     }

     System.out.println(mys_list.size());
     for(String g :mys_list){
         System.out.println(g);
     }



}