如何在JavaFX应用程序中从Excel读取数据?

如何在JavaFX应用程序中从Excel读取数据?,java,excel,javafx,apache-poi,fxml,Java,Excel,Javafx,Apache Poi,Fxml,我正在用hibernate作为Java中的ORM创建一个JavaFX FXMl应用程序。我想从Excel中读取数据并使用hibernate将其存储到MySQL,但它无法创建工作簿 我花了数小时阅读教程和其他帖子,无论是在SO还是其他网站上,无论是接受的答案还是评论都对我没有帮助 在我的Form_Main.fxml文件中,我有一个按钮,用于调用相关方法从Excel读取数据。我添加了以下JAR文件: *poi-4.1.0 *poi-ooxml-4.1.0 *poi-ooxml-schemas-4.1

我正在用hibernate作为Java中的ORM创建一个JavaFX FXMl应用程序。我想从Excel中读取数据并使用hibernate将其存储到MySQL,但它无法创建工作簿

我花了数小时阅读教程和其他帖子,无论是在SO还是其他网站上,无论是接受的答案还是评论都对我没有帮助

在我的
Form_Main.fxml
文件中,我有一个按钮,用于调用相关方法从Excel读取数据。我添加了以下JAR文件:
*poi-4.1.0
*poi-ooxml-4.1.0
*poi-ooxml-schemas-4.1.0
*xmlbeans-3.0.1
*commons-collections4-4.2
对该项目的支持。我要读取的数据导入如下所示:

import org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.XSSFRow;
我定义的方法是:

FileInputStream\u File=newfileinputstream(新文件(“D:\\Students\u Info.xlsx”);
XSSF工作簿_工作簿=新XSSF工作簿(_文件);
XSSFSheet _Sheet=_工作簿.getSheetAt(0);
XSSFRow_行;
剩下的代码将使用hibernate将数据写入MySQL。但它不起作用

调试程序时,在第行:

XSSFWorkbook\u工作簿=新XSSFWorkbook(\u文件);
它跳转到第行的文件
fxmloader.java

catch(InvocationTargetException异常){
抛出新的RuntimeException(异常);
}
我的堆栈跟踪(如果我是正确的)是:

[java]线程“JavaFX应用程序线程”java.lang.RuntimeException中的异常:java.lang.reflect.InvocationTargetException
[java]位于javafx.fxml.fxmloader$MethodHandler.invoke(fxmloader.java:1774)
[java]位于javafx.fxml.fxmloader$ControllerMethodEventHandler.handle(fxmloader.java:1657)
[java]位于com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
[java]位于com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
[java]位于com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
[java]位于com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
[java]位于com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
[java]位于com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
[java]位于com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
[java]位于com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
[java]位于com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
[java]位于com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
[java]位于com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
[java]位于com.sun.javafx.event.EventUtil.firevent(EventUtil.java:49)
[java]位于javafx.event.event.fireEvent(event.java:198)
[java]位于javafx.scene.Node.firevent(Node.java:8411)
[java]位于javafx.scene.control.Button.fire(Button.java:185)
[java]位于com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
[java]位于com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
[java]位于com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
[java]位于com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
[java]位于com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
[java]位于com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
[java]位于com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
[java]位于com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
[java]位于com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
[java]位于com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
[java]位于com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
[java]位于com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
[java]位于com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
[java]位于com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
[java]位于com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
[java]位于com.sun.javafx.event.EventUtil.firevent(EventUtil.java:54)
[java]位于javafx.event.event.fireEvent(event.java:198)
[java]位于javafx.scene.scene$MouseHandler.process(scene.java:3757)
[java]位于javafx.scene.scene$MouseHandler.access$1500(scene.java:3485)
[java]位于javafx.scene.scene.impl_processMouseEvent(scene.java:1762)
[java]位于javafx.scene.scene$ScenePeerListener.mouseEvent(scene.java:2494)
[java]位于com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
[java]位于com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
[java]位于java.security.AccessController.doPrivileged(本机方法)
[java]位于com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$358(GlassViewEventHandler.java:432)
[java]位于com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
[爪哇]
List<List<String>> excelData = ExcelFileUtility.readExcelFile("fileName.xlsx");
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Cell;
import static org.apache.poi.ss.usermodel.CellType.BLANK;
import static org.apache.poi.ss.usermodel.CellType.BOOLEAN;
import static org.apache.poi.ss.usermodel.CellType.FORMULA;
import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static org.apache.poi.ss.usermodel.CellType.STRING;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

/**
 *
 * @author blj0011
 */
public class ExcelFileUtility
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        readExcelFileWithHeaderFirstRow("Copy of Database_Details_Report_AAA.xlsx", false).forEach((t) -> {
            t.forEach((z) -> {
                System.out.print(z + " - ");
            });

            System.out.println();
        });
    }

    public static List<List<String>> readExcelFile(String fileName)
    {
        List<List<String>> excelContent = new ArrayList();
        int columnCounter = 0;
        int rowCounter = 0;

        try (InputStream inputStream = new FileInputStream(fileName)) {
            DataFormatter formatter = new DataFormatter();

            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                List<String> tempList = new ArrayList();

                for (Cell cell : row) {
                    String text = formatter.formatCellValue(cell);
                    System.out.print(++columnCounter + ": " + text);
                    System.out.println(text.length());
                    tempList.add(text);
                }
                columnCounter = 0;
                excelContent.add(tempList);

                ++rowCounter;
                //Used for testing
                //if (rowCounter == 5) {
                //    break;
                //}
                //System.out.println(String.join(" - ", tempList));
                //System.out.println(tempList.size());

            }
        }
        catch (IOException | EncryptedDocumentException ex) {
            System.out.println(ex.toString());
        }

        return excelContent;
    }

    public static List<List<String>> readExcelFileWithHeaderFirstRow(String fileName, boolean allCellHaveData)
    {
        List<List<String>> sheetData = new ArrayList();
        int headerCount = 0;

        try (FileInputStream fis = new FileInputStream(fileName);
             Workbook wb = WorkbookFactory.create(fis)) {

            Sheet sheet = wb.getSheetAt(0);
            for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
                Row row = sheet.getRow(i);
                String value;

                List<String> rowData = new ArrayList();
                //System.out.println("\nROW " + row.getRowNum() + " has " + row.getPhysicalNumberOfCells() + " cell(s).");
                //System.out.println("last cell number: " + row.getLastCellNum());
                if (i == 0) {
                    headerCount = row.getLastCellNum();
                }

                for (int c = 0; c < row.getLastCellNum(); c++) {

                    Cell cell = row.getCell(c);

                    if (cell != null) {
                        switch (cell.getCellType()) {
                            case FORMULA:
                                value = "FORMULA value=" + cell.getCellFormula();
                                rowData.add(cell.getCellFormula());
                                break;

                            case NUMERIC:
                                value = "NUMERIC value=" + cell.getNumericCellValue();
                                rowData.add(Double.toString(cell.getNumericCellValue()));
                                break;

                            case STRING:
                                value = "STRING value=" + cell.getStringCellValue();
                                rowData.add(cell.getStringCellValue());
                                break;

                            case BLANK:
                                value = "<BLANK>";
                                rowData.add("");
                                break;

                            case BOOLEAN:
                                value = "BOOLEAN value-" + cell.getBooleanCellValue();
                                rowData.add(cell.getBooleanCellValue() ? "true" : "false");
                                break;

                            case ERROR:
                                value = "ERROR value=" + cell.getErrorCellValue();
                                rowData.add(Byte.toString(cell.getErrorCellValue()));
                                break;

                            default:
                                value = "UNKNOWN value of type " + cell.getCellType();
                                rowData.add(cell.getCellType().toString());
                        }
                        //System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);
                    }
                    else {
                        //System.out.println("CELL col=" + " VALUE=" + "<My Blank>");
                        rowData.add("<My Blank>");
                    }
                }

                if (!allCellHaveData) {
                    int currentRowCount = row.getLastCellNum();
                    while (currentRowCount < headerCount) {
                        rowData.add("<Blank end column>");
                        currentRowCount++;
                    }
                }
                sheetData.add(rowData);
            }
        }
        catch (FileNotFoundException ex) {
            System.out.println(ex);
        }
        catch (IOException ex) {
            System.out.println(ex);
        }

        return sheetData;
    }
}