使用Apache POI Excel写入特定单元格位置

时间:2022-01-01 22:18:45

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?

如果我有一个未排序的参数'x,y,z'列表,是否有一种直接的方法将它们写入使用POI创建的Excel文档中的特定单元格,就好像前两个参数是X和Y坐标?

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?

是否可以在第10行第4列的单元格中写入值“100”?

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 个解决方案

#1


12  

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

当然,这很简单,只需记住POI为0而不是基于寻址的1。假设您要写入第10行,第4列,您可以执行类似的操作

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

#1


12  

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

当然,这很简单,只需记住POI为0而不是基于寻址的1。假设您要写入第10行,第4列,您可以执行类似的操作

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