gpt4 book ai didi

java - 使用 Apache POI 将工作表附加到 Excel 文件 - 由 NullPointerException 引起的 POIXMLException

转载 作者:行者123 更新时间:2023-12-04 21:15:20 26 4
gpt4 key购买 nike

这是我的代码:

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();
}
}
}

创建文件工作正常 - 文件按预期创建。但是,如果文件存在,我希望此方法创建一个工作表并将其添加到工作簿中。使用我目前拥有的代码,我得到以下异常:
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

引发此异常的行是 workbook.write(fileOut)但是 workbook 和 fileOut 都不为空。输出文件在第二次运行时也会损坏。

我究竟做错了什么?

最佳答案

这个错误是因为我在创建工作簿时没有将 FileInputStream 作为参数传递的。尽管 API 建议您可以将文件/路径作为参数传递,但该文件实际上是以只读模式打开的,因此当再次写入时会引发异常。

这可以通过传入 FileInputStream 来解决。然后我的代码变为:

FileInputStream fis = new FileInputStream(excelFile);
workbook = new XSSFWorkbook(fis);

关于java - 使用 Apache POI 将工作表附加到 Excel 文件 - 由 NullPointerException 引起的 POIXMLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com