首页 > 解决方案 > 我想在 java 中的 JTable 中显示我的 excel 表

问题描述

我有 3 个类,称为边界、控制器和实体。我不允许直接从实体类访问边界类。我必须使用控制器类与边界和实体类进行通信。实体类由我所有的方法和 getter/setter 组成。控制器将检查该表是否可用。如果返回 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", Font.PLAIN, 20)); 
    search.setSize(100, 20); 
    search.setLocation(100, 100); 
    c.add(search);
    
    searchF = new JTextField(); 
    searchF.setFont(new Font("Arial", Font.PLAIN, 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);
    }
    
}

标签: javaexcelswingjtable

解决方案


推荐阅读