문제

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

도움이 되었습니까?

해결책

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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top