2017-09-27 104 views
2

您好,我對Java編程還很陌生,並且想要在我的代碼中獲得一些輸入。我知道這非常混亂,我還沒有機會清理它。使用POI(XSSF)讀取和存儲excel表格值 - 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)); 


} 



} 

時,我打運行,並啓動線程是如下

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 

回答

0

它看起來像公地collections4-xxjar文件,我得到的錯誤(可here)是從構建路徑中缺失。

添加它,它應該工作!

相關問題