Reading multiple excel sheet

此生再无相见时 提交于 2019-12-08 09:07:15

问题


I am trying to read the sheets of a spread sheet uisng a foor loop. I wanted to know is this the right way of reading especially the use of Sheet Propety [highlighted in the code] :

Cell[][] newcell=new Cell[200][200];   
int newsheet = workbook1.getNumberOfSheets();
for (int q=1;q < newsheet;q++)    
{
    for(int p=0;p < sheet(q).getColumns();p++)
    {
         for(int p1=0;p1<sheet(q).getRows();p1++)
                       /*^^^^^^^^^*/
         {
               newcell[p][p1] = sheet(q).getCell(p, p1);
                              /*^^^^^^^^^*/
               if(newcell[p][p1].equals(saved[j]))
               {
                    System.out.print( newcell[p][0]);
                }
          }
     }   
}

Can I use the property of sheet() as sheet(q) because its throwing NullPointerException?


回答1:


The usual style for working with all the cells in POI is:

for(int sheetNum=0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
    Sheet sheet = wb.getSheetAt(sheetNum);
    for (Row row : sheet) {
        for (Cell cell : row) {
            // Do something here
        }
    }
}

Maybe switch your code to something more like that?




回答2:


With jxl (JExcelAPI), this should work:

for (Sheet sheet:workbook1.getSheets()) {  // getSheet() returns a Sheet[]
  int numCols = sheet.getColumns();        // getColumns() returns an int
  for(for int i = 0; i <= numCols; i++) {
     Cell[] column = sheet.getColumn(i);
     for(Cell cell:column) {               // column is a Cell[]
        if(cell.equals(saved[j])) {
           System.out.print(cell);
        }
     }
  }   
}


来源:https://stackoverflow.com/questions/6056427/reading-multiple-excel-sheet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!