write 2d arrays of integers on excel using apache poi

You should iterate through the whole excel sheet and then check if you are in the desired cell, write the appropriate value.

One way to do this, is with CellReference . With that you can reference to a cell and then take its row/col index an write on it.

For example:

CellReference A1 = new CellReference("A1");

int row = cr.getRow();
int col = cr.getCol();

Now you got the index for row/col of A1 and you can write on this position like this:

Row row1 = ((sheet.getRow(row)==null) ? sheet.createRow(row) : sheet.getRow(row));

With the line above you check if the row exists, and if not, then you create one.

Cell cell;
if (row1.getCell(col) == null) {
    cell = row1.createCell(col);
} else {
    cell = row1.getCell(col);
}

With the above, you check the specific cell on the specific row if its empty, and if it is then you create a new one, else you the its reference.

Now, you are ready to write on A1 cell. Simply do:

cell.SetCellValue("arrayOne");

So now, just use row and col that represent the index for row/col in your sheet and write in the cells of your choice.

For example by doing col+=1 you getting B1 and by repeating the above code in a for loop and increment every time the col and/or row index inside your loop, you can write the arrays in the way you want on your sheet.

Please feel free to ask me for extra clarification.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top