0

以下是循环打印同一行中单元格总和的代码。当我运行时,第一个循环工作正常,我得到了 sum 的值,但是在第二个循环中,它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
4

1 回答 1

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);
            }
        }
于 2013-06-17T04:57:25.277 回答