Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to a particular cell location using Apache POI Excel

If I've got an list of parameters 'x,y,z' that aren't sorted, is there a straightforward way to write them to particular cells in an excel document created with POI, as though the first two parameters are X and Y coordinates?

For example, I have rows like:

10,4,100

Is it possible to write the value '100' in the cell at the 10th row, 4th column?

Looking at the documentation, it looks straightforward to iterate values into the next row, but I can't see any way of creating a fixed number of rows and columns and writing particular values to only certain cells.

Any advice or suggestions would be appreciated, thanks!

like image 995
nowaycomputer Avatar asked Dec 16 '22 20:12

nowaycomputer


1 Answers

Sure, it's very easy, just remember that POI is 0 based not 1 based in addressing. Assuming you want to write to the 10th row, 4th column, you'd do something like

Row r = sheet.getRow(9); // 10-1
if (r == null) {
   // First cell in the row, create
   r = sheet.createRow(9);
}

Cell c = r.getCell(3); // 4-1
if (c == null) {
    // New cell
    c = r.createCell(3, Cell.CELL_TYPE_NUMERIC);
}
c.setCellValue(100);
like image 90
Gagravarr Avatar answered Dec 28 '22 09:12

Gagravarr