如何使用Apache POI自动创建行?

use*_*481 6 apache xls apache-poi

我在Java项目中工作,我需要创建一个包含一些信息的xls文件.因此,根据信息量,我需要自动创建行和单元格以放置此信息.

示例:如果输入文档有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);

    }
Run Code Online (Sandbox Code Playgroud)

提前致谢..

Gag*_*arr 8

你能不能做一些简单的事情:

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++;
Run Code Online (Sandbox Code Playgroud)