java 使用 Apache POI Excel 写入特定单元格位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12112958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing to a particular cell location using Apache POI Excel
提问by nowaycomputer
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!
任何意见或建议将不胜感激,谢谢!
回答by Gagravarr
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);