读取Excel文件时,Cell.getNumericCellValue返回null值

时间:2013-06-17 04:47:38

标签: java nullpointerexception apache-poi

以下是打印循环中同一行中单元格总和的代码。当我运行时,第一个循环工作正常,我得到总和的值,但在第二个循环中,它在打印double z= cell.getNumericCellValue();的值后,在行z处抛出空指针异常。

try{
      FileInputStream file = new FileInputStream("C:/Documents and Settings/m.y/My Documents/test.xls");

      HSSFWorkbook workbook = new HSSFWorkbook(file);
      HSSFSheet sheet = workbook.getSheetAt(0);
 //   HSSFCell cell = null;
      Row r = sheet.getRow(0);
      Row row = sheet.createRow(0);
      Cell cell = row.createCell(0);
      double summ=1.0;

        for(int i =1 ; i<=sheet.getLastRowNum(); i++){

            for(int j=1; j<r.getLastCellNum();j++)
            {          
                cell = sheet.getRow(i).getCell(j); 
              //  if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType())
             //   {  
                  double z= cell.getNumericCellValue(); 
                   System.out.println(z);                  
                   summ=summ+z;  

             //    }                                     
            }  
             System.out.println(summ);
             Cell mycell1=r.createCell(3);
                   mycell1.setCellValue(summ); 
                   summ=0.0;        
                 }    

    file.close();    
    FileOutputStream outFile =new FileOutputStream("C:/Documents and Settings/m.y/My Documents/test.xls");
    workbook.write(outFile);
    outFile.close();   


}catch (IOException e) {
    e.printStackTrace();
}
    }

注意:excel文件可以是:

Name  sub1   Sub2
m      2      3
n      5      6
j      4      9

1 个答案:

答案 0 :(得分:0)

试试这个

try{
        FileInputStream file = null;
        try {
            file = new FileInputStream("C:/Documents and Settings/m.y/My Documents/test.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            HSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            double summ = 1.0;
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                int i = 0;
                double z = 0.0;
                while (cellIterator.hasNext()) {
                    cell = sheet.getRow(i).getCell(j);
                    if (i == 0) {
                        System.out.println(cell.getStringCellValue());
                    } else {
                        z = cell.getNumericCellValue();
                    }
                    System.out.println(z);
                    summ = summ + z;
                    i++;
                    //    }                                     
                }
                System.out.println(summ);
                Cell mycell1 = r.createCell(3);
                mycell1.setCellValue(summ);
                summ = 0.0;
            }
            file.close();
            FileOutputStream outFile = new FileOutputStream("C:/Documents and Settings/m.y/My Documents/test.xls");
            workbook.write(outFile);
            outFile.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                file.close();
            } catch (IOException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }