Writing to a particular cell location using Apache POI Excel

会有一股神秘感。 提交于 2019-12-07 09:21:29

问题


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!


回答1:


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);


来源:https://stackoverflow.com/questions/12112958/writing-to-a-particular-cell-location-using-apache-poi-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!