有人可以帮忙处理这段代码吗?
我在第 41 行收到有关 NullPointerException 的错误消息。我知道它返回 null,但以下验证需要该值。一般任务是将结果写入excel文件。
public class ExcelUtils {
public static void main(String[] args)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("Employee Data");
String Result = "TEST TEST TEST";
int RowNum = 2;
int ColNum = 3;
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.getRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
//---------------------------------------------------
//try
// {
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
最佳答案
将结果字符串从测试方法传递到方法 writeexcel。您不需要创建对象,因为该方法是静态的
writeexcel("pass",2,3);
这将调用 writeexcel 方法,并将作为参数传递的字符串写入相应的单元格中,即“pass”
方法:
public static void writeexcel(String Result,int RowNum ,int ColNum)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("Employee Data");
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
编辑
public class Tests {
static XSSFWorkbook workbook = new XSSFWorkbook();
static XSSFSheet sheet = workbook.createSheet("Employee Data");
public static void main(String[] args) {
int j=3;
for(int i=2;i<=5;i++) {
String result = "Test "+i;
writeexcel(result, i, j);
}
writetoexcel();//write to cell(2,3),(3,3),(4,3)
}
public static void writeexcel(String Result,int RowNum ,int ColNum)
{
//Create a blank sheet
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void writetoexcel(){
try{
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}catch (Exception e)
{
e.printStackTrace();
}
}
}
希望这对您有帮助...如果您需要任何进一步的帮助,请回来
关于java - 尝试将结果写入 Excel 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783908/