-1

I'm pretty new to programming and javaFX and started my own little project for pratice. Im trying to modify a simple existing Excel-Sheet. The User shall give some information in a textfield and the programm shall modify the excel sheet with that information and open it afterwards, when clicking a button. I'm using Apache POI to modify this Excel Sheet.

My Code looks like this atm (i'm sorry if the code isn't perfect - i just started):

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

        }
    }

At the moment it throws this Error: Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

and a lot of more red rows..

Help is much appreciated.

Full Error:

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
James_D
  • 201,275
  • 16
  • 291
  • 322
TheHennii
  • 63
  • 8
  • 2
    Post the complete stack trace in the question. Did you read it and try to [diagnose it](https://stackoverflow.com/q/3988788/2775450)? – James_D Mar 12 '18 at 17:32
  • The error code? I tired, but i failed. It's super long. But i gonna post it. – TheHennii Mar 12 '18 at 17:41
  • Seems pretty self explanatory: "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)" – James_D Mar 12 '18 at 17:44
  • So it says my excel is a too new version? How can i fix this problem? – TheHennii Mar 12 '18 at 17:48
  • But overall the code should work in your opinion? – TheHennii Mar 12 '18 at 17:48
  • 3
    Again, it says: *"You need to call a different part of POI to process this data (eg XSSF instead of HSSF)"*. I don't know POI, but wouldn't you just do `XSSFWorkbook wb = new XSSFWorkbook(fsIP);`, etc etc? – James_D Mar 12 '18 at 17:48
  • 1
    Use [WorkbookFactory.create(File)](http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html#create-java.io.File-) - handling both XLS and XLSX is what it's there for! – Gagravarr Mar 12 '18 at 18:41

1 Answers1

0

Promoting a comment to an answer...

The key line of your exception is:

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)

Handily, that exception tells you exactly what you've done wrong:

The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents

And exactly what you need to do to fix your code:

You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

If you'll only ever work with XLSX files, change your current HSSF calls to XSSF ones

If you'll be working with both XLS and XLSX files, you should instead use WorkbookFactory.create(File) to have your file opened

Change these lines:

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

To instead be:

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

And your code will now work correctly for both HSSF and XSSF

Gagravarr
  • 47,320
  • 10
  • 111
  • 156