如何在 Excel 文件中添加新行?
How to add new row in Excel file?
我在 excel 文件中添加新行时遇到问题。我的代码:
public static void setExcelFile(String Path, String SheetName) throws Exception {
try {
FileInputStream ExcelFile = new FileInputStream(Path);
workBook = new XSSFWorkbook(ExcelFile);
workSheet = workBook.getSheet(SheetName);
} catch (Exception e) {
throw (e);
}
}
public static void setCellData(String path, String value, int RowNum, int ColNum) throws Exception {
try{
row = workSheet.getRow(RowNum);
cell = row.getCell(ColNum);
if (cell == null) {
cell = row.createCell(ColNum);
cell.setCellValue(value);
} else {
cell.setCellValue(value);
}
FileOutputStream fileOut = new FileOutputStream(path);
workBook.write(fileOut);
fileOut.flush();
fileOut.close();
}catch(Exception e){
throw (e);
}
并且:
public static void main(String[] args) {
String exPath = "C:\Drivers\excel\test.xlsx";
try {
ExcelUtility.setExcelFile(exPath, "MainTable");
ExcelUtility.setCellData(exPath, "Pizza", 4, 0);
ExcelUtility.setCellData(exPath, "Thin", 4, 1);
ExcelUtility.setCellData(exPath, "Peperoni", 4, 2);
ExcelUtility.setCellData(exPath, "Katy", 4, 3);
ExcelUtility.setCellData(exPath, "N/A", 4, 4);
ExcelUtility.setCellData(exPath, "14.99", 4, 5);
} catch (Exception e) {
e.printStackTrace();
}
}
在我的 excel 文件中有 4 行。使用上面的代码,当我尝试更新任何现有行时,效果很好。但是当我尝试添加新行时,它给了我例外:
java.lang.NullPointerException
at ui_automation.utils.ExcelUtility.setCellData(ExcelUtility.java:65)
at ui_automation.stepDefinition.ExcelTest.main(ExcelTest.java:18)
知道我做错了什么吗?
空指针异常发生在您自己的代码中。您没有提供完整的源代码,也没有提供 excel 文件(或从一开始就描述了它有多少行和列),所以我不能说空到底是什么。但是您可以只查看 ExcelUtility.java.
第 65 行的代码
但只看 javadoc,有几个候选人。其一,workSheet 对象可能为 null,因为 workBook.getSheet(SheetName) returns null 如果没有 sheet 命名为“主表”。否则,如果没有这样的行,workSheet.getRow(RowNum) 仍然会导致空值。请记住,该行是 zero-based,因此当您询问行“4”时,它实际上是您要询问的第 5th 行为了。您的 excel 文件在这个 sheet 中是否有五行?
如果这不是问题,那么尝试使用您正在使用的IDE的调试器,并在方法“setCellData”中的第一行放置一个断点(即放置一个断点row = workSheet.getRow(RowNum);) 然后使用调试器查看所有变量的实际值。找到导致 Null Pointer Exception 的对象,然后向后跟踪代码,直到找到它为什么为 null。如果你不能让你的调试器工作,你可以在代码的不同地方添加一些简单的 System.out.println("variable: " + variable") ,这样您可以在代码运行时看到这些值。
如果这些都不能帮助您找到问题,请 post 完整的源代码(包括导入),行号与您的代码中的行号匹配。以及打开“MainTable”sheet 的 excel 文件的屏幕截图也会有所帮助。
我在 excel 文件中添加新行时遇到问题。我的代码:
public static void setExcelFile(String Path, String SheetName) throws Exception {
try {
FileInputStream ExcelFile = new FileInputStream(Path);
workBook = new XSSFWorkbook(ExcelFile);
workSheet = workBook.getSheet(SheetName);
} catch (Exception e) {
throw (e);
}
}
public static void setCellData(String path, String value, int RowNum, int ColNum) throws Exception {
try{
row = workSheet.getRow(RowNum);
cell = row.getCell(ColNum);
if (cell == null) {
cell = row.createCell(ColNum);
cell.setCellValue(value);
} else {
cell.setCellValue(value);
}
FileOutputStream fileOut = new FileOutputStream(path);
workBook.write(fileOut);
fileOut.flush();
fileOut.close();
}catch(Exception e){
throw (e);
}
并且:
public static void main(String[] args) {
String exPath = "C:\Drivers\excel\test.xlsx";
try {
ExcelUtility.setExcelFile(exPath, "MainTable");
ExcelUtility.setCellData(exPath, "Pizza", 4, 0);
ExcelUtility.setCellData(exPath, "Thin", 4, 1);
ExcelUtility.setCellData(exPath, "Peperoni", 4, 2);
ExcelUtility.setCellData(exPath, "Katy", 4, 3);
ExcelUtility.setCellData(exPath, "N/A", 4, 4);
ExcelUtility.setCellData(exPath, "14.99", 4, 5);
} catch (Exception e) {
e.printStackTrace();
}
}
在我的 excel 文件中有 4 行。使用上面的代码,当我尝试更新任何现有行时,效果很好。但是当我尝试添加新行时,它给了我例外:
java.lang.NullPointerException
at ui_automation.utils.ExcelUtility.setCellData(ExcelUtility.java:65)
at ui_automation.stepDefinition.ExcelTest.main(ExcelTest.java:18)
知道我做错了什么吗?
空指针异常发生在您自己的代码中。您没有提供完整的源代码,也没有提供 excel 文件(或从一开始就描述了它有多少行和列),所以我不能说空到底是什么。但是您可以只查看 ExcelUtility.java.
第 65 行的代码但只看 javadoc,有几个候选人。其一,workSheet 对象可能为 null,因为 workBook.getSheet(SheetName) returns null 如果没有 sheet 命名为“主表”。否则,如果没有这样的行,workSheet.getRow(RowNum) 仍然会导致空值。请记住,该行是 zero-based,因此当您询问行“4”时,它实际上是您要询问的第 5th 行为了。您的 excel 文件在这个 sheet 中是否有五行?
如果这不是问题,那么尝试使用您正在使用的IDE的调试器,并在方法“setCellData”中的第一行放置一个断点(即放置一个断点row = workSheet.getRow(RowNum);) 然后使用调试器查看所有变量的实际值。找到导致 Null Pointer Exception 的对象,然后向后跟踪代码,直到找到它为什么为 null。如果你不能让你的调试器工作,你可以在代码的不同地方添加一些简单的 System.out.println("variable: " + variable") ,这样您可以在代码运行时看到这些值。
如果这些都不能帮助您找到问题,请 post 完整的源代码(包括导入),行号与您的代码中的行号匹配。以及打开“MainTable”sheet 的 excel 文件的屏幕截图也会有所帮助。