使用POI(XSSF)-JAVA读取和存储excel工作表值

使用POI(XSSF)-JAVA读取和存储excel工作表值,java,multidimensional-array,xssf,Java,Multidimensional Array,Xssf,您好,我对Java编程相当陌生,希望获得一些代码输入。我知道它真的很乱,我还没有机会把它清理干净 我能够让JFilechooser正常工作,而且据我所知,我正在为XSSFWorkbook正确设置文件路径。但是当我试图打开工作表并读取其内容时,我的程序抛出异常 我需要我的程序做的就是读取一个excel文件,将2列+X行存储到一个2d int数组中 我的代码(我遇到问题的部分在run()部分。) PS(再次抱歉,代码太乱了,这可能不是我想做的事情的最佳方式,这里的JAVA新手) 构建路径中似乎缺少c

您好,我对Java编程相当陌生,希望获得一些代码输入。我知道它真的很乱,我还没有机会把它清理干净

我能够让JFilechooser正常工作,而且据我所知,我正在为XSSFWorkbook正确设置文件路径。但是当我试图打开工作表并读取其内容时,我的程序抛出异常

我需要我的程序做的就是读取一个excel文件,将2列+X行存储到一个2d int数组中

我的代码(我遇到问题的部分在run()部分。) PS(再次抱歉,代码太乱了,这可能不是我想做的事情的最佳方式,这里的JAVA新手)


构建路径中似乎缺少commons-collections4-x.x.jar文件(可用)

添加它,它应该工作

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;

import javax.swing.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
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;

import java.io.*;
import java.util.Arrays;
import java.util.Iterator;

public class ExtensionsTool extends JFrame implements ActionListener, Runnable {
final JFileChooser fc = new JFileChooser();
Thread go;
private JMenuBar menuBar1;
private JMenu FileMenu;
private JMenuItem OpenMenu;
private JMenuItem ExitMenu;
private JMenu HelpMenu;
private JMenuItem AboutMenu;
private JScrollPane scrollPane1;
private JTextArea Extensions;
private JButton OpenButton;
private JButton ClearButton;
private JButton ExtCopy;
private JButton SQLCopy;
private JScrollPane scrollPane2;
private JTextArea Queries;
private JLabel label1;
private JLabel label2;
private JButton RunButton;
private int rows;
private int columns;
private int[][] array;
String filepath;
File f;

public ExtensionsTool() {
    RunButton = new JButton();
    menuBar1 = new JMenuBar();
    FileMenu = new JMenu();
    OpenMenu = new JMenuItem();
    ExitMenu = new JMenuItem();
    HelpMenu = new JMenu();
    AboutMenu = new JMenuItem();
    scrollPane1 = new JScrollPane();
    Extensions = new JTextArea();
    OpenButton = new JButton();
    ClearButton = new JButton();
    ExtCopy = new JButton();
    SQLCopy = new JButton();
    scrollPane2 = new JScrollPane();
    Queries = new JTextArea();
    label1 = new JLabel();
    label2 = new JLabel();

    setTitle("Extension Tool");
    Container contentPane = getContentPane();
    setResizable(false);

    RunButton.addActionListener(this);
    OpenButton.addActionListener(this);
    ClearButton.addActionListener(this);
    ExtCopy.addActionListener(this);
    SQLCopy.addActionListener(this);
    OpenMenu.addActionListener(this);
    ExitMenu.addActionListener(this);
    AboutMenu.addActionListener(this);

    //======== menuBar1 ========
            {

                //======== FileMenu ========
                {
                    FileMenu.setText("File");

                    //---- OpenMenu ----
                    OpenMenu.setText("Open");
                    FileMenu.add(OpenMenu);

                    //---- ExitMenu ----
                    ExitMenu.setText("Exit");
                    FileMenu.add(ExitMenu);
                }
                menuBar1.add(FileMenu);

                //======== HelpMenu ========
                {
                    HelpMenu.setText("Help");

                    //---- AboutMenu ----
                    AboutMenu.setText("About");
                    HelpMenu.add(AboutMenu);
                }
                menuBar1.add(HelpMenu);
            }
            setJMenuBar(menuBar1);

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(Extensions);
            }

            //---- OpenButton ----
            OpenButton.setText("Open File");

            //---- ClearButton ----
            ClearButton.setText("Clear");

            //---- ExtCopy ----
            ExtCopy.setText("Copy Extensions");

            //---- SQLCopy ----
            SQLCopy.setText("Copy SQL Query");

            //======== scrollPane2 ========
            {
                scrollPane2.setViewportView(Queries);
            }

            //---- label1 ----
            label1.setText("Extensions:");

            //---- label2 ----
            label2.setText("SQL Queries");

            //---- RunButton ----
            RunButton.setText("Run");

            GroupLayout contentPaneLayout = new GroupLayout(contentPane);
            contentPane.setLayout(contentPaneLayout);
            contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                    .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                        .addContainerGap(156, Short.MAX_VALUE)
                        .addGroup(contentPaneLayout.createParallelGroup()
                            .addComponent(label2)
                            .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 599, GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28))
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addComponent(SQLCopy, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ExtCopy, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ClearButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(OpenButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(RunButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(33, 33, 33)
                        .addGroup(contentPaneLayout.createParallelGroup()
                            .addComponent(label1)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 599, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(28, Short.MAX_VALUE))
            );
            contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(label1)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(contentPaneLayout.createParallelGroup()
                            .addGroup(contentPaneLayout.createSequentialGroup()
                                .addComponent(OpenButton)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ClearButton)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ExtCopy)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SQLCopy)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(RunButton))
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 214, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(label2)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 214, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(12, Short.MAX_VALUE))
            );
            pack();
            setLocationRelativeTo(getOwner());
    setVisible(true);
}







public void actionPerformed(ActionEvent evt){
    Object source = evt.getSource();

    if(source == RunButton){
        if(go == null){
            go = new Thread(this);
            go.start();
        }
    }   else if (source == ExitMenu){
        System.exit(0);




    }   else if (source == OpenMenu){
        JFileChooser fc=new JFileChooser();    
        int i=fc.showOpenDialog(this);    
        if(i==JFileChooser.APPROVE_OPTION){    
            File f=fc.getSelectedFile();  
            filepath=f.getPath();

            try{      
            }catch (Exception ex) {ex.printStackTrace();  }         
    }


    }else if (source == OpenButton){
        JFileChooser fc=new JFileChooser();    
        int i=fc.showOpenDialog(this);    
        if(i==JFileChooser.APPROVE_OPTION){    
            File f=fc.getSelectedFile();  
            filepath=f.getPath();

            try{      
            }catch (Exception ex) {ex.printStackTrace();  }         
    }


    }

}



public static void main(String[] arguments){
    ExtensionsTool frame = new ExtensionsTool();
}


public void run() {
    Queries.setText(filepath);

    try {

        FileInputStream input_document = new FileInputStream(new File(filepath));
        XSSFWorkbook my_xlsx_workbook = new XSSFWorkbook(input_document);
        XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0); 
        Row row = sheet.getRow(2);
        Cell cell = row.getCell(3);

        int rows; // No of rows
        rows = sheet.getPhysicalNumberOfRows();
        int cols = 2; // No of columns
        array = new int[cols][rows];

        for (int j = 0; j < cols; j++) {
            for (int i = 0; i < rows; i++) 
            {
             array[j][i] = (int) cell.getNumericCellValue();
            }
        }
        my_xlsx_workbook.close();

    } catch(Exception ioe) {
        ioe.printStackTrace();
    }

    Extensions.setText(Arrays.toString(array));


}



}
Exception in thread "Thread-3" java.lang.NoClassDefFoundError: 
    org/apache/commons/collections4/ListValuedMap
    at ExtensionsTool.run(ExtensionsTool.java:261)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.ClassNotFoundException: 
org.apache.commons.collections4.ListValuedMap
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more