[英]Appending a sheet to an Excel file using Apache POI - POIXMLException caused by NullPointerException
Here is my code: 这是我的代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.*;
public class testingsyncclass {
public static void main(String[] args) throws IOException, InvalidFormatException{
writeParametersTableToFile("C:/ENTERPRISE_TESTING", "XLSX_TEST", "SHEETNAME");
}
public static void writeParametersTableToFile(String fileLocation, String fileName, String tableLocator) throws InvalidFormatException{
String[] columnHeaders = {"What", "is", "a", "computer", "?"};
ArrayList<Object[]> xlsxFileData = new ArrayList<Object[]>();
for(int i = 0; i < 10; i++){
Object[] o = {"CoffeeCup", "AnglerJS", "Emerald", "C♭", "C--"};
xlsxFileData.add(o);
}
try{
if(fileName.contains(".xlsx")){
fileName = fileName.replaceAll(".xlsx", "");
}
File file = new File(fileLocation);
if(file.mkdirs() || file.exists()){
File excelFile = new File(fileLocation + "/" + fileName + ".xlsx");
XSSFWorkbook workbook;
XSSFSheet sheet;
if(excelFile.exists()){
workbook = new XSSFWorkbook(excelFile);
} else {
workbook = new XSSFWorkbook();
}
if(workbook.getSheet(tableLocator)==null){
sheet = workbook.createSheet(tableLocator);
} else {
sheet = workbook.createSheet("NEW_TEST_SHEET");
}
XSSFRow firstRow = sheet.createRow(0);
//write headers
for(int i = 0; i < columnHeaders.length; i++){
XSSFCell cell = firstRow.createCell(i);
cell.setCellValue(columnHeaders[i]);
}
//write data
for(int i = 0; i < xlsxFileData.size(); i++){
XSSFRow row = sheet.createRow(i+1);
for(int j = 0; j < xlsxFileData.get(i).length; j++){
XSSFCell cell = row.createCell(j);
String value = (String) xlsxFileData.get(i)[j];
cell.setCellValue(value);
}
}
FileOutputStream fileOut = new FileOutputStream(fileLocation + "/" + fileName + ".xlsx");
//write this workbook to an Outputstream.
System.out.println(fileOut + " (fileout)");
System.out.println(workbook + " (workbook)");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
workbook.close();
} else {
System.err.println(fileLocation + " was an invalid directory.");
}
} catch (IOException e){
System.err.println("Something went wrong!");
e.printStackTrace();
}
}
}
Creating the file works fine- the file is created as expected. 创建文件可以正常工作-文件已按预期创建。 However, if the file exists I want this method to create a sheet and add it to the workbook.
但是,如果文件存在,我希望此方法创建一个工作表并将其添加到工作簿中。 With the code I currently have, I get the following exception:
使用我目前拥有的代码,我得到以下异常:
Exception in thread "main" org.apache.poi.POIXMLException: java.lang.NullPointerException
at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:168)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:246)
at testframework.testingsyncclass.writeParametersTableToFile(testingsyncclass.java:67)
at testframework.testingsyncclass.main(testingsyncclass.java:17)
Caused by: java.lang.NullPointerException
The line throwing this exception is workbook.write(fileOut)
yet both workbook and fileOut are not null. workbook.write(fileOut)
此异常的行是workbook.write(fileOut)
但workbook和fileOut都不为空。 The output file also becomes corrupted on the second run. 在第二次运行时,输出文件也会损坏。
What am I doing wrong? 我究竟做错了什么?
This error was caused because I didn't pass a FileInputStream as a parameter when creating the workbook. 造成此错误的原因是,在创建工作簿时,我没有将FileInputStream作为参数传递。 Although the API suggests that you can pass a File/Path as a parameter, the file is then essentially opened in ReadOnly mode- so when writing to it again an exception is thrown.
尽管该API建议您可以将文件/路径作为参数传递,但实际上该文件以ReadOnly模式打开-因此,当再次对其进行写入时,将引发异常。
This is fixed by passing in a FileInputStream instead. 通过传入FileInputStream来解决此问题。 My code then becomes:
然后我的代码变为:
FileInputStream fis = new FileInputStream(excelFile);
workbook = new XSSFWorkbook(fis);
工作簿wb = WorkbookFactory.create(new FileInputStream(“ data / df.xlsx”));;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.