Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想在java的JTable中显示我的excel工作表_Java_Excel_Swing_Jtable - Fatal编程技术网

我想在java的JTable中显示我的excel工作表

我想在java的JTable中显示我的excel工作表,java,excel,swing,jtable,Java,Excel,Swing,Jtable,我有三个类叫做边界、控制器和实体。我不允许直接从实体类访问边界类。我必须使用控制器类与边界和实体类通信。实体类由我的所有方法和getter/setter组成。控制器将检查表格是否可用。如果返回true,它将把类发送到GUI 我想做的是: 我试图在实体类中显示excel工作表,然后将其传递给控制器,然后从那里将数据解析到GUI,GUI是显示表的主要方法 我曾尝试在互联网上查找,但大多数示例都来自主方法本身 这将是非常有帮助的,如果你能给我一个链接,我可以参考或建议我可以做什么!谢谢!:) 这就是我

我有三个类叫做边界、控制器和实体。我不允许直接从实体类访问边界类。我必须使用控制器类与边界和实体类通信。实体类由我的所有方法和getter/setter组成。控制器将检查表格是否可用。如果返回true,它将把类发送到GUI

我想做的是: 我试图在实体类中显示excel工作表,然后将其传递给控制器,然后从那里将数据解析到GUI,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;
}
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);
    }
    
}
我试图调用实体类中的方法以将excel工作表显示到JTable中的边界类。

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;
}
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);
    }
    
}

您具体在做什么?请参阅:。它演示了如何使用ResultSet中的数据创建标题和数据向量。因此,基本逻辑将是相同的,只是您的代码将从工作簿类获取数据。因此,修改代码以使用工作簿而不是结果集。或者您也可以搜索论坛/网站,例如使用Workbood和DefaultTableModel类的论坛/网站。您具体在做什么?请参阅:。它演示了如何使用ResultSet中的数据创建标题和数据向量。因此,基本逻辑将是相同的,只是您的代码将从工作簿类获取数据。因此,修改代码以使用工作簿而不是结果集。或者您也可以搜索论坛/网站,例如使用Workbood和DefaultTableModel类的论坛/网站。