2013-12-10 49 views
0

我使用apache poi包来创建表示形状(区域,周长,质心)特征的数字电子表格。的问题是,我有一个方法:writeDatabase(),因为它们被发现,其输出的形状的特征,输出电子表格看起来像这样: http://s23.postimg.org/hqsfg76jv/Capture.png使用Apache POI在Excel中启动新行的问题

所有的这些数字需要在同一直线上,并那么下一条记录需要换行。该writeDatabase方法如下所示

public static void writeDatabase(int value, int cellNum){ 

    try { 
     Cell cell1=null; 
     FileInputStream file = new FileInputStream(new File("features.xls")); 
     Workbook workbook = new HSSFWorkbook(file); 
     Sheet sheet = workbook.getSheetAt(0); 

     int lastRow = sheet.getPhysicalNumberOfRows(); 

     cell1 = sheet.createRow(lastRow).createCell(cellNum); 
     cell1.setCellValue(value); 

     FileOutputStream outFile =new FileOutputStream(new File("features.xls")); 
     workbook.write(outFile); 
     outFile.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我认为这个问题是与该行中的每个时间被调用,但我不能认为一个替代的: INT LASTROW = sheet.getPhysicalNumberOfRows();

任何想法?

回答

0

您需要提供一个新的线路是否应该启动或没有一些指示(除非你可以告诉大家,一个新的行只是开始,因为cellNum是0?)

然后,您可以创建一个新的行,或者使用现有行:

int lastRow = sheet.getPhysicalNumberOfRows(); 

    Row row; 
    if (startNewRow) { 
     row = sheet.createRow(lastRow); 
    } else { 
     row = sheet.getRow(lastRow - 1); 
    } 

    cell1 = row.createCell(cellNum); 
    cell1.setCellValue(value); 

startNewRow可能基于cellNum进行设置,或者可能是被传递到writeDatabase额外的参数,无论是适当的。