Pregunta

I want to insert a string to a Matrix of Cells in Excel, Using Apache POI, Here is the code :

for (int j=0;j<13;j++){
     for(int i=6;i<30;i++)
     {
        XSSFRow row=sheet6.createRow(i);
        cell0=row.createCell(j);
        cell0.setCellValue("SampleRules_CI_01");
    }
}

After executing my program, I get the column j=12 correctly filled, but the other columns (from 0 to 11) are still empty

¿Fue útil?

Solución

You are replacing the row each time you get back around to the same value of i, when you call the createRow method. This has the effect of erasing whatever was on that row already, and only the last column doesn't get erased this way.

First, inside your i for loop, call the getRow method to see if it already exists. If it returns null, then the row doesn't exist yet and then you can call createRow.

XSSFRow row = sheet6.getRow(i);
if (row == null)
{
    row = sheet6.createRow(i);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top