2013-02-18 91 views
4

我在Java項目中工作,我需要用一些信息創建一個xls文件。 因此,根據信息量,我需要自動創建行和單元格來放置此信息。如何使用Apache POI自動創建行?

示例:如果輸入文檔具有13個站點信息,則需要創建13個行,並使用4個單元格。 我該怎麼做? .. 我試圖代碼:

Workbook wb = new HSSFWorkbook(); 
    Sheet sheet = wb.createSheet("new sheet"); 

    int numberrows = Integer.parseInt(JOptionPane.showInputDialog(null, "numbers of sites??")); 


    String siteName = JOptionPane.showInputDialog(null, "Site name"); 
    String rncname = JOptionPane.showInputDialog(null, "RncName"); 


    for (int i = 0; i < numberrows; i++) { 
     HSSFRow linha = (HSSFRow) sheet.createRow(i); 

     linha.createCell((short) i).setCellValue(siteName); 
     linha.createCell((short) i).setCellValue(rncname); 

    } 

在此先感謝..

回答

6

難道你們就不能只是做喜歡的事很簡單:

int nextRow = 12; 

Row r = sheet.getRow(nextRow); 
if (r == null) { 
    r = sheet.createRow(nextRow); 
} 

Cell c = r.getCell(2, Row.CREATE_NULL_AS_BLANK); 
c.setCellValue("My String"); 

nextRow++; 
+0

這將幫助我自動創建的安裝我需要的行? '因爲我嘗試並且不能像我想要的那樣工作>< >< – user2083481 2013-02-19 11:48:49

+1

謝謝,我的問題已解決..對於那些想知道我是如何做到的,我在每個循環中都用MainRow + 1做了新的行... – user2083481 2013-02-19 12:21:54