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