我正在尝试从一个.xlsx读取数据并将其放在另一个文件中。当循环第二次运行时会发生错误。没有空白单元格,所有187行都有数据(检查和验证)。只有一列包含数据(第一列A),所以我没有使用Null Checker。这是我到目前为止的代码。我不包括写入和关闭文件的代码,因为在循环的第二次迭代期间发生错误。
FileInputStream fis=new FileInputStream("C:\\test\\Migration_input.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
int rowCount = sheet.getLastRowNum();
System.out.println(rowCount);
FileOutputStream fos=new FileOutputStream("C:\\test\\Migration.xlsx");
for (int b=0; b<rowCount;b++)
{
System.out.println(b);
//This part to read from existing file
XSSFRow rowreader=sheet.getRow(b);
XSSFCell cellreader = rowreader.getCell(0);
**String cellinput = cellreader.getStringCellValue(); //Errors out on this line on second iteration - It is not a null value and has some string data in it.
//This part to write in a different file
XSSFRow rowwriter=sheet.createRow(b+1);
XSSFCell cellwriter= rowwriter.createCell(2);
System.out.println(cellinput);
cellwriter.setCellType(cellwriter.CELL_TYPE_STRING);
cellwriter.setCellValue(cellinput);
System.out.println("done");
}
我将代码更改为此但仍然出现错误:
for (int b=0; b<rowCount;b++)
{
System.out.println(b);
//This part to read from existing file
XSSFRow rowreader=sheet.getRow(b);
XSSFCell cellreader = rowreader.getCell(0);
System.out.println(cellreader.getStringCellValue());
**cellinput[b] = cellreader.getStringCellValue(); //Errors out
System.out.println("stored in array string all the values");
}
这是更新的输出
0
Academic Cert Expected Date
FAILED: get_labels
java.lang.NullPointerException
at crm_migration.labels_on_given_object.get_labels(labels_on_given_object.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
它从.xlsx读取的值很好但是当我尝试将它存储到字符串数组中时,就是它现在抛出错误的时候。我已经将循环分开,以便它现在只读取并将值存储在数组中,**同样在第一列A中没有空值,其中包含187行。请协助。
答案 0 :(得分:0)
您实现的读取逻辑是错误的。首先在所有行上创建一个循环,然后遍历每行中的单元格值
String.split(String)
类似地实现写逻辑
我已经测试了上面的代码,它运行正常。如果你有任何问题,请回来