Changeset 39

User picture

Author: lazylt

(2010/01/09 23:26) Over 2 years ago

Changes made on 1/9/2010 11am.

Affected files

Updated Practice/Practice/ropingExample.java Download diff

3839
27
import javax.swing.event.TableModelEvent;
27
import javax.swing.event.TableModelEvent;
28
import javax.swing.event.TableModelListener;
28
import javax.swing.event.TableModelListener;
29
import javax.swing.table.TableModel;
29
import javax.swing.table.TableModel;
30
import jxl.Workbook;
31
import jxl.write.WritableSheet;
32
import jxl.write.WritableWorkbook;
33
import jxl.write.WriteException;
34
30
35
class ropingExample implements ActionListener, TableModelListener{
31
class ropingExample implements ActionListener, TableModelListener{
36
    JFrame frame;
32
    JFrame frame;
...
...
45
    int rounds = 4;
41
    int rounds = 4;
46
    String[] columnNames;
42
    String[] columnNames;
47
    private WriteExcelFile ropingDataExcelFile;
43
    private WriteExcelFile ropingDataExcelFile;
48
    private WritableSheet ropingDataSheet;
49
    private File ropingDataFileName = new File("RopingData.xls");
44
    private File ropingDataFileName = new File("RopingData.xls");
50
45
51
    ropingExample() throws WriteException{
46
    ropingExample(){
52
        try {
47
        ropingDataExcelFile = new WriteExcelFile();
53
            ropingDataExcelFile = new WriteExcelFile();
48
        ropingDataExcelFile.setOutputFile("RopingData.xls");
54
            ropingDataExcelFile.setOutputFile("RopingData.xls");
49
        ropingDataExcelFile.setSheet("Roping Data", 0);
55
            ropingDataExcelFile.setSheet("Roping Data", 0);
56
        }
57
        catch (IOException ex) {
58
            Logger.getLogger(ropingExample.class.getName()).log(Level.SEVERE, null, ex);
59
        }
60
        catch (WriteException ex){
61
            Logger.getLogger(ropingExample.class.getName()).log(Level.SEVERE, null, ex);
62
        }
63
        rows = new Vector();
50
        rows = new Vector();
64
        columns = new Vector();
51
        columns = new Vector();
65
52
...
...
167
            deletRow(table.getSelectedRow());
154
            deletRow(table.getSelectedRow());
168
        }
155
        }
169
        if(source.getSource() == (JButton)saveFile){
156
        if(source.getSource() == (JButton)saveFile){
170
            try {
157
171
                saveDataExcel();
158
            saveDataExcel();
172
                String msg = "Data Saved to " + ropingDataFileName.getName();
159
            String msg = "Data Saved to " + ropingDataFileName.getName();
173
                JOptionPane.showMessageDialog(null, msg, "Table Example", JOptionPane.INFORMATION_MESSAGE);
160
            JOptionPane.showMessageDialog(null, msg, "Table Example", JOptionPane.INFORMATION_MESSAGE);
174
            } catch (IOException ex) {
175
                Logger.getLogger(ropingExample.class.getName()).log(Level.SEVERE, null, ex);
176
            } catch (WriteException ex) {
177
                Logger.getLogger(ropingExample.class.getName()).log(Level.SEVERE, null, ex);
178
            }
179
        }
161
        }
180
162
181
        if(source.getSource() == (JButton)cmdGetValue){
163
        if(source.getSource() == (JButton)cmdGetValue){
...
...
207
	return colNames;
189
	return colNames;
208
    }
190
    }
209
191
210
    public void saveDataExcel() throws IOException, WriteException{
192
    public void saveDataExcel(){
211
        ropingDataExcelFile.write("Roping Data");
193
        ropingDataExcelFile.write("Roping Data");
212
    }
194
    }
213
195
...
...
215
     * @param args the command line arguments
197
     * @param args the command line arguments
216
     */
198
     */
217
    public static void main(String[] args) {
199
    public static void main(String[] args) {
218
        try {
200
        ropingExample t = new ropingExample();
219
            ropingExample t = new ropingExample();
220
        } catch (WriteException ex) {
221
            Logger.getLogger(ropingExample.class.getName()).log(Level.SEVERE, null, ex);
222
        }
223
    }
201
    }
224
}
202
}

Updated Practice/Practice/WriteExcelFile.java Download diff

3839
7
import java.io.IOException;
7
import java.io.IOException;
8
import java.util.Locale;
8
import java.util.Locale;
9
import java.util.Vector;
9
import java.util.Vector;
10
import java.util.logging.Level;
11
import java.util.logging.Logger;
12
import javax.swing.JOptionPane;
10
import jxl.CellView;
13
import jxl.CellView;
11
import jxl.Workbook;
14
import jxl.Workbook;
12
import jxl.WorkbookSettings;
15
import jxl.WorkbookSettings;
...
...
31
    Vector rows, columns;
34
    Vector rows, columns;
32
    String[] columnNames;
35
    String[] columnNames;
33
    
36
    
34
    public WriteExcelFile() throws WriteException, IOException{
37
    public WriteExcelFile(){
38
        try {
35
            // Lets create a times font
39
            // Lets create a times font
36
        WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
40
            WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
37
        // Define the cell format
41
            // Define the cell format
38
        times = new WritableCellFormat(times10pt);
42
            times = new WritableCellFormat(times10pt);
39
        // Lets automatically wrap the cells
43
                // Lets automatically wrap the cells
40
        times.setWrap(true);
44
                times.setWrap(true);
45
            WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.SINGLE);
46
            timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
47
            // Lets automatically wrap the cells
48
            timesBoldUnderline.setWrap(true);
49
            CellView cv = new CellView();
50
            cv.setFormat(times);
51
            cv.setFormat(timesBoldUnderline);
52
            cv.setAutosize(true);
53
            inputFile = "Test.xls";
54
            excelFile = new File(inputFile);
55
            WorkbookSettings wbSettings = new WorkbookSettings();
56
            wbSettings.setLocale(new Locale("en", "EN"));
57
            workbook = Workbook.createWorkbook(excelFile, wbSettings);
58
            workbook.createSheet("Test Data", 0);
59
        } catch (WriteException ex) {
60
                String msg = "Error: " +  ex.toString();
61
                JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.INFORMATION_MESSAGE);
62
        }catch (IOException ex) {
63
                String msg = "Error: " +  ex.toString();
64
                JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.INFORMATION_MESSAGE);
65
        }
41
66
42
        // Create create a bold font with unterlines
43
        WritableFont times10ptBoldUnderline = new WritableFont(
44
                WritableFont.TIMES, 10, WritableFont.BOLD, false,
45
                UnderlineStyle.SINGLE);
46
        timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
47
        // Lets automatically wrap the cells
48
        timesBoldUnderline.setWrap(true);
49
        CellView cv = new CellView();
50
        cv.setFormat(times);
51
        cv.setFormat(timesBoldUnderline);
52
        cv.setAutosize(true);
53
        inputFile = "Test.xls";
54
        excelFile = new File(inputFile);
55
        WorkbookSettings wbSettings = new WorkbookSettings();
56
        wbSettings.setLocale(new Locale("en", "EN"));
57
        workbook = Workbook.createWorkbook(excelFile, wbSettings);
58
        workbook.createSheet("Test Data", 0);
59
60
    }
67
    }
61
68
62
    public void setOutputFile(String inputFile) throws IOException {
69
    public void setOutputFile(String inputFile){
63
    	this.inputFile = inputFile;
70
    	this.inputFile = inputFile;
64
        excelFile = new File(inputFile);
71
        excelFile = new File(inputFile);
65
        WorkbookSettings wbSettings = new WorkbookSettings();
72
        WorkbookSettings wbSettings = new WorkbookSettings();
66
        wbSettings.setLocale(new Locale("en", "EN"));
73
        wbSettings.setLocale(new Locale("en", "EN"));
67
        workbook = Workbook.createWorkbook(excelFile, wbSettings);
74
        try {
75
            workbook = Workbook.createWorkbook(excelFile, wbSettings);
76
        } catch (IOException ex) {
77
                String msg = "Error: " +  ex.toString();
78
                JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.INFORMATION_MESSAGE);
79
        }
68
    }
80
    }
69
81
70
    public void write(String name) throws IOException, WriteException {
82
    public void write(String name){
71
        WritableSheet sheet = workbook.getSheet(name);
83
        try {
72
        createContent(sheet);
84
            WritableSheet sheet = workbook.getSheet(name);
73
        workbook.write();
85
            createContent(sheet);
74
        workbook.close();
86
            workbook.write();
87
            workbook.close();
88
        } catch (WriteException ex) {
89
            String msg = "Error: " +  ex.toString();
90
            JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.INFORMATION_MESSAGE);
91
       } catch (IOException ex) {
92
            String msg = "Error: " +  ex.toString();
93
            JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.INFORMATION_MESSAGE);
94
       }
75
    }
95
    }
76
96
77
    public void setSheet(String name, int sheetNumber){
97
    public void setSheet(String name, int sheetNumber){
...
...
86
        rows = row;
106
        rows = row;
87
    }
107
    }
88
108
89
//    public void setColomns(Vector col){
90
//        columns = col;
91
//    }
92
//
93
//    public void setRowsColumns(Vector row, Vector col){
94
//        setRows(row);
95
//        setColomns(col);
96
//    }
97
//
98
    private void createContent(WritableSheet ws)throws WriteException,
109
    private void createContent(WritableSheet ws)throws WriteException,
99
            RowsExceededException {
110
            RowsExceededException {
100
111