我想在Java的JTable中显示我的Excel工作表

问题描述

我有3个类,分别称为边界,控制器和实体。我不允许直接从实体类访问边界类。我必须使用控制器类与边界和实体类进行通信。实体类包括我所有的方法获取器/设置器。控制器将检查该表是否可用。如果返回true,它将把类发送到GUI。

我想做的是: 我试图在实体类显示excel工作表,然后将其传递给控制器​​,然后从那里将数据解析为包含显示表的主要方法的GUI。

我试图查找互联网,但是这些示例大多来自主要方法本身。

如果您可以将我引荐给我可以参考的链接或为我提供建议,将对您有很大帮助!谢谢! :)

这是我到目前为止所做的,如果有人说我在询问之前没有尝试过:)

我的实体类中的方法试图调用Excel工作表。

public boolean getAllBugs1(ListBugReportEntity ListBugReportEntity) throws IOException 
{
    
    Vector headers = new Vector();
    Vector data = new Vector();
    
    File file = new File("src/BugReportDB.xls");
    try {
    Workbook workbook = Workbook.getWorkbook(file);
    Sheet sheet = workbook.getSheet(0);
    headers.clear();
    for (int i = 0; i < sheet.getColumns(); i++) {
    Cell cell1 = sheet.getCell(i,0);
    headers.add(cell1.getContents());
    }
    data.clear();
    for (int j = 1; j < sheet.getRows(); j++) {
    Vector d = new Vector();
    for (int i = 0; i < sheet.getColumns(); i++) {
    Cell cell = sheet.getCell(i,j);
    d.add(cell.getContents());
    }
    d.add("\n");
    data.add(d);
    }
    }
    catch (Exception e) {
    e.printstacktrace();
    }
    
    return true;
}

要检查是否存在此类或Excel工作表的控制器

import java.io.IOException;

public class ListBugController {

ListBugReportEntity ListBugReportEntity = new ListBugReportEntity();

public boolean getAll(ListBugReportEntity ListBugReportEntity) throws IOException
{
    if(ListBugReportEntity.getAllBugs1(ListBugReportEntity))
    {
        return true;
    }
    
    return false;
}

}

我试图在实体类调用方法以将excel工作表显示到JTable中的边界类。

import java.awt.*;
import java.io.IOException;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class ListBugReportUI extends JFrame{

private JLabel sBug;
private JTextField sBugF;
private JLabel sAssignee;
private JTextField sAssigneeF;
private JButton searchB;
private JTextField searchF;
private JLabel search;
private JPanel panel1;
private boolean help;
private boolean what;
private JPanel panel2;
private Container c;

ListBugReportEntity ListBugReportEntity = new ListBugReportEntity();

public ListBugReportUI() throws IOException
{
    setTitle("View All Bugs");
    
    c = getContentPane();
    c.setLayout(null); 
    
    sBug = new JLabel("List of Bugs"); 
    sBug.setFont(new Font("Arial",Font.PLAIN,30)); 
    sBug.setSize(300,30); 
    sBug.setLocation(225,30); 
    c.add(sBug);
    
    search = new JLabel("Search By"); 
    search.setFont(new Font("Arial",20)); 
    search.setSize(100,20); 
    search.setLocation(100,100); 
    c.add(search);
    
    searchF = new JTextField(); 
    searchF.setFont(new Font("Arial",15)); 
    searchF.setSize(190,20); 
    searchF.setLocation(300,100); 
    c.add(searchF);
    
    //table
    
    ListBugController ui = new ListBugController();
    ListBugReportEntity ui2 = new ListBugReportEntity();
    
   
    
    help = ui.getAll(ListBugReportEntity);
    
    Vector headers = new Vector();
    Vector data = new Vector();

    JTable table = new JTable();
    DefaultTableModel model = new DefaultTableModel(data,headers);
    table.setModel(model);
    table.setautocreateRowSorter(true);
    model = new DefaultTableModel(data,headers);
    table.setModel(model);
    JScrollPane scroll = new JScrollPane(table);
    
    c.add(scroll);
    
    //end table
    
    
    
    
    
}



public static void main (String[] args) throws IOException
    {
        ListBugReportUI ui = new ListBugReportUI();
        
        ui.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        ui.setSize(700,600);
        ui.setVisible(true);
    }
    
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)