swing java read excel sheet

Solutions on MaxInterview for swing java read excel sheet by the best coders in the world

showing results for - "swing java read excel sheet"
Elisa
11 Jan 2019
1 import java.io.*;
2import java.awt.*;
3import java.util.*;
4import javax.swing.*;
5import java.awt.event.*;
6import javax.swing.table.*;
7
8import jxl.Cell;
9import jxl.Sheet;
10import jxl.Workbook;
11
12
13public class ExcelToJtable {
14public static void main(String[] args) {
15
16Vector headers = new Vector();
17Vector data = new Vector();
18
19File file = new File("c:/data.xls");
20try {
21Workbook workbook = Workbook.getWorkbook(file);
22Sheet sheet = workbook.getSheet(0);
23headers.clear();
24for (int i = 0; i < sheet.getColumns(); i++) {
25Cell cell1 = sheet.getCell(i, 0);
26headers.add(cell1.getContents());
27}
28data.clear();
29for (int j = 1; j < sheet.getRows(); j++) {
30Vector d = new Vector();
31for (int i = 0; i < sheet.getColumns(); i++) {
32Cell cell = sheet.getCell(i, j);
33d.add(cell.getContents());
34}
35d.add("\n");
36data.add(d);
37}
38}
39catch (Exception e) {
40e.printStackTrace();
41}
42JTable table = new JTable();
43DefaultTableModel model = new DefaultTableModel(data,headers);
44table.setModel(model);
45table.setAutoCreateRowSorter(true);
46model = new DefaultTableModel(data, headers);
47table.setModel(model);
48JScrollPane scroll = new JScrollPane(table);
49JFrame f=new JFrame();
50f.add(scroll);
51f.setSize(400, 200);
52f.setResizable(true);
53f.setVisible(true);
54}
55}
56