【问题标题】:While writing into xlsx file not working using apache-poi使用 apache-poi 写入 xlsx 文件时不起作用
【发布时间】:2018-04-02 17:47:22
【问题描述】:

我正在尝试在“xlsx”文件中附加数据,但出现空指针异常。我尝试使用现有的解决方案来解决问题,但没有一个对我有用。

以下是我在 xlsx 文件中编写的代码。

package test;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test1 {
    public static void main(String[] args) throws IOException {
        String excelFileName = "Y:\\RB and UCM Production Support\\RB\\RB Prod Support Knowledge Base\\UAR\\test.xlsx";//name of excel file

        String sheetName = "Sheet1";//name of sheet

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(sheetName) ;

        //XSSFRow r = sheet.createRow(0);

        //iterating r number of rows
        for (int r=0;r < 5; r++ )
        {
            XSSFRow row = sheet.createRow(r);

            //iterating c number of columns
            for (int c=0;c < 5; c++ )
            {
                XSSFCell cell = row.createCell(c);

                cell.setCellValue("Cell "+r+" "+c);
                System.out.println("Cell "+r+" "+c);
            }
        }
//      wb.close();
//      String excelFileName1 = "Y:\\RB and UCM Production Support\\RB\\RB Prod Support Knowledge Base\\UAR\\test1.xlsx";
        FileOutputStream fileOut = new FileOutputStream(excelFileName);

        //write this workbook to an Outputstream.
        wb.write(fileOut);

        fileOut.flush();
        fileOut.close();
        wb.close();
    }
}

我还把所有的jar文件都添加到了项目路径中。

此外,在执行项目时,会弹出一个错误。 下面是相同的屏幕截图。

运行上述代码时遇到错误。

请帮忙!!!!

【问题讨论】:

  • 项目上的红色感叹号表示缺少某些构建路径条目。检查它们。另外,提供完整的堆栈跟踪并告诉我们哪一行产生了异常。
  • 如果代码仍然存在编译错误,您甚至不应该尝试运行您的代码。打开问题视图,并在尝试运行代码之前修复其中列出的所有编译错误。
  • 我运行了代码。运行代码后出现这样的错误。
  • 再一次,你不应该运行你的代码。在您修复所有错误之前。如果你知道你的车引擎坏了,四个轮子中只有两个,你不应该尝试驾驶它。如果你无论如何尝试,然后发生事故,那么,你活该。先修车。然后开车。然后,如果仍然无法正常工作,请询问。
  • 你能建议我一些修复吗..因为我尝试了很多东西,但没有一个对我有用。

标签: java apache-poi


【解决方案1】:

这似乎是一个构建问题,或者您没有使用正确的 Poi.jar。

//Implementation org.apache.poi.POIXMLDocument.write(OutputStream)
public final void write(OutputStream stream) throws IOException {
    //force all children to commit their changes into the underlying OOXML Package
    Set<PackagePart> context = new HashSet<PackagePart>();
    onSave(context);
    context.clear();

    //save extended and custom properties
    getProperties().commit();

    getPackage().save(stream);
}

根据附加的错误屏幕截图,出现异常getProperties(),当 POI jar 没有获取底层属性或在没有 OPCPackage 的情况下无法创建属性时会发生这种情况。

注意: POI 在创建XSSFWorkbook() 的实例时创建 OPCPackage

更多详情请参考:XSSFWorkbook() 的 Javadoc:

要获取有效的 jar,请使用以下链接/或使用 maven 依赖项: https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.17

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多