您好我是Java编程的新手,并希望在我的代码上获得一些输入。我知道它真的很乱,我还没有机会清理它。
我能够让JFilechooser工作,据我所知,我正在为XSSFWorkbook正确设置我的文件路径。但是在试图打开表格并阅读其内容时,我的程序会抛出异常。
我需要我的程序才能读取一个excel文件并取2列+ X行并将其存储到2d int数组中。
我的代码(我遇到问题的部分是在run()部分。) PS(再次对不起,代码很乱,这可能不是我想做的最好的方式,JAVA新手在这里)
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));
}
}
当我点击Run并且线程启动时我得到的错误如下
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