使用 javaFx 修改并打开现有的 Excel (xlsx) 文件



我对编程和javaFX很陌生,并开始了我自己的实践小项目。我正在尝试修改一个简单的现有Excel工作表。用户应在文本字段中提供一些信息,程序应使用该信息修改Excel工作表,然后在单击按钮时将其打开。我正在使用Apache POI来修改这个Excel工作表。

我的代码看起来像这个 atm(如果代码不完美,我很抱歉 - 我刚刚开始(:

@FXML
private Textfield name;
public class Controller {
    public Controller() {
}
@FXML
    private void showExcel(ActionEvent e) throws Exception {

        String eingabeName = name.getText();
        try {
            FileInputStream fsIP = new FileInputStream(new File("C:\Users\hendr\Documents\Vorlage.xlsx"));
            HSSFWorkbook wb = new HSSFWorkbook(fsIP);
            HSSFSheet worksheet = wb.getSheetAt(0);
            Cell cell = null;
            cell = worksheet.getRow(1).getCell(2);
            cell.setCellValue(eingabeName);
             File excelFile = new File("C:\Users\hendr\Documents\Vorlage.xlsx");
                if (excelFile.exists())
                {
                 if (Desktop.isDesktopSupported())
                 {
                  try
                  {
                   Desktop.getDesktop().open(excelFile);
                  }
                  catch (IOException e1)
                  {
                   e1.printStackTrace();
                  }
                 }
                 else
                  {
                   System.out.println("Desktop is not supported!");
                  }
                }
                else
                {
                 System.out.println("File does not exist!");
                }       
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }

目前它抛出此错误:线程"JavaFX Application Thread" java.lang.RuntimeException中的异常: java.lang.reflect.InvocationTargetException

还有更多的红行..

非常感谢帮助。

完全错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
    at javafx.graphics/javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.controls/javafx.scene.control.Button.fire(Unknown Source)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.reflect.Trampoline.invoke(Unknown Source)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(Unknown Source)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(Unknown Source)
    ... 48 more
Caused by: org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:126)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:113)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:301)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:413)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:394)
    at application.Controller.showExcel(Controller.java:138)
    ... 58 more

将评论提升为答案...

异常的关键行是:

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

方便的是,该异常可以准确地告诉您做错了什么:

提供的数据似乎在 Office 2007+ XML 中。您正在调用 POI 中处理 OLE2 办公文档的部分

以及修复代码需要做什么:

您需要调用POI的不同部分来处理此数据(例如XSSF而不是HSSF(

如果您只处理XLSX文件,请将当前HSSF调用更改为XSSF

如果要同时处理XLSXLSX文件,则应改为使用 WorkbookFactory.create(File( 打开文件

更改这些行:

        FileInputStream fsIP = new FileInputStream(new File("C:\Users\hendr\Documents\Vorlage.xlsx"));
        HSSFWorkbook wb = new HSSFWorkbook(fsIP);
        HSSFSheet worksheet = wb.getSheetAt(0);

改为:

File fs = new File("C:\Users\hendr\Documents\Vorlage.xlsx");
Workbook wb = WorkbookFactory.create(fs);
Sheet worksheet = wb.getSheetAt(0);

您的代码现在可以在 HSSF 和 XSSF 上正常工作

最新更新