Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在Apache POI XSSF 4.10中将单元格类型设置为字符串?_Excel_Apache Poi_Cell_Xssf - Fatal编程技术网

Excel 如何在Apache POI XSSF 4.10中将单元格类型设置为字符串?

Excel 如何在Apache POI XSSF 4.10中将单元格类型设置为字符串?,excel,apache-poi,cell,xssf,Excel,Apache Poi,Cell,Xssf,我正在尝试使用ApachePOIXSSF4.10将单元格值设置为字符串 我已经使用了代码 sheet.getRow(i).getCell(k).setCellType(CellType.STRING);但它抛出空指针异常 请帮助除非您已经在代码中创建了单元格,否则请始终检查它是否存在。如果只对每个单元格写入一次,并且只访问它们一次,那么代码就会容易得多 仅访问和写入一次(假设存在工作表): 多次访问单元格的完整代码: Row r = sheet.getRow(rownum); if (r ==

我正在尝试使用ApachePOIXSSF4.10将单元格值设置为字符串

我已经使用了代码

sheet.getRow(i).getCell(k).setCellType(CellType.STRING);但它抛出空指针异常


请帮助

除非您已经在代码中创建了单元格,否则请始终检查它是否存在。如果只对每个单元格写入一次,并且只访问它们一次,那么代码就会容易得多

仅访问和写入一次(假设存在工作表):

多次访问单元格的完整代码:

Row r = sheet.getRow(rownum);
if (r == null) {
    r = sheet.createRow(rownum);
}
Cell c = r.getCell(column);
if (c == null) {
    c = r.createCell(column);
}
c.setCellType(CellType.STRING);

如果尚未创建指定的工作表、行、单元格,这两个代码都确保单元格存在,因为
getXYZ
方法可以返回
NULL

确保在使用/写入行和单元格之前定义行和单元格?
Row r = sheet.getRow(rownum);
if (r == null) {
    r = sheet.createRow(rownum);
}
Cell c = r.getCell(column);
if (c == null) {
    c = r.createCell(column);
}
c.setCellType(CellType.STRING);