尝试在空白单元格上设置CellValue

Mar*_*olo 0 groovy apache-poi

我正在尝试将文本插入空白单元格

getRow( 0 ).getCell( 0 ).setCellValue( 'Hat Selection' ) 
Run Code Online (Sandbox Code Playgroud)

我不断得到

无法在空对象上调用方法 setCellValue()

为什么我不能在这个空白单元格中插入文本?我怎样才能?

San*_*ngh 5

如果您的任何对象为空,您应该首先创建它,然后在其上设置值。如果您尚未创建该行,请使用 createRow。

sheet.createRow(0);
Run Code Online (Sandbox Code Playgroud)

如果您已经创建了行,但没有创建相应的单元格,则首先创建相应的单元格,然后输入所需的值。用

sheet.getRow(0).createCell(0).setCellValue(value);
Run Code Online (Sandbox Code Playgroud)

或者你可以结合所有

sheet.createRow(0).createCell(0).setCellValue(value);
Run Code Online (Sandbox Code Playgroud)