1
2
3
4
5 package oscon2006.swing;
6
7 import javax.swing.table.*;
8 import org.apache.poi.hssf.usermodel.*;
9 import java.io.*;
10
11 public class WorkbookTableModel extends AbstractTableModel
12 {
13 private HSSFWorkbook wb;
14 private HSSFSheet sheet;
15
16 public WorkbookTableModel(HSSFWorkbook w)
17 {
18 this.wb = w;
19 this.sheet = this.wb.getSheetAt(0);
20 }
21
22 public WorkbookTableModel(String filename) throws IOException
23 {
24 this(getInputStream(filename));
25 }
26
27 public WorkbookTableModel(File f) throws IOException
28 {
29 this(new FileInputStream(f));
30 }
31
32
33 public WorkbookTableModel(InputStream in) throws IOException
34 {
35 this(new HSSFWorkbook(in));
36 in.close();
37 }
38
39 public int getColumnCount()
40 {
41 return 3;
42 }
43
44 public int getRowCount()
45 {
46 return this.sheet.getPhysicalNumberOfRows();
47 }
48
49 private HSSFRow getHSSFRow(int rowIndex)
50 {
51 return this.sheet.getRow(rowIndex);
52 }
53
54 public Object getValueAt(int rowIndex, int columnIndex)
55 {
56 HSSFRow row = getHSSFRow(rowIndex);
57
58 if (row == null)
59 {
60 return "";
61 }
62
63 HSSFCell cell = row.getCell( (short) columnIndex);
64
65 if (cell == null)
66 {
67 return "";
68 }
69
70 int cellType = cell.getCellType();
71
72 if (cellType == HSSFCell.CELL_TYPE_STRING)
73 {
74 return cell.getStringCellValue();
75 }
76 else if (cellType == HSSFCell.CELL_TYPE_NUMERIC)
77 {
78 return "" + cell.getNumericCellValue();
79 }
80 else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN)
81 {
82 return "" + cell.getBooleanCellValue();
83 }
84 else
85 {
86 return "";
87 }
88
89 }
90
91 private static InputStream getInputStream(String filename) throws IOException
92 {
93 FileInputStream input = null;
94 input = new FileInputStream(filename);
95 return input;
96 }
97
98 }