我目前正在用 Java 编写一个程序,该程序将运行多个测试并在 excel 中生成报告。我能够通过 Excel 进行读写,结果列中显示通过或失败的结果。我能够在 Excel 中编写这些内容,但通过在单元格上提供默认值(例如默认值),这样代码就会覆盖它。我想在“评论”列中写评论,但我不知道如何在 null 单元格中写入。这是我生成的报告的屏幕截图(可用图像的链接)以及在 Excel 中读写的代码。
FileInputStream fileInputStream = new FileInputStream("ReportExcel.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet worksheet = workbook.getSheetAt(0);
fileInput csvInputFile = new fileInput();
String[] sReturnValue = csvInputFile.arrayReturnSingleValue("fileInput.csv");
if (prodnameresult.equals(prodname) ){
Pass++;
totalResult++;
System.out.println ("Testcase1: Branding-Customised Product Name is PASSED");
//Harold's Input
HSSFRow row = worksheet.getRow(1);
HSSFCell cell = row.getCell((short) 4);
cell.setCellValue("Passed");
}
else{
Fail++;
totalResult++;
System.out.println("Testcase1: Branding-Customised Product Name is FAILED");
//assertEquals(prodnameresult.equals(prodname), true);
//Harold's Input
HSSFRow row = worksheet.getRow(1);
HSSFCell cell = row.getCell((short) 4);
cell.setCellValue("Failed");
}
//Harold's Input
FileOutputStream os = new FileOutputStream("ReportExcel.xls");
workbook.write(os);
os.close();
}
报告:
最佳答案
我无权访问该报告,但为了回答您的问题,如果您的单元格为null,您应该创建它而不是按如下方式获取它:
HSSFCell cell = row.getCell((short) 4);
if(cell == null)
cell = row.createCell((short) 4);
cell.setCellValue("Passed");
如果单元格与行的样式不同,您也许应该将样式重新应用到单元格。
关于java - 如何使用java在空单元格上写入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11926920/