Source Code Cross Referenced for ExporterForXLSFile.java in  » Database-Client » DBBrowser » org » dbbrowser » ui » helper » exporthelper » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » DBBrowser » org.dbbrowser.ui.helper.exporthelper 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.dbbrowser.ui.helper.exporthelper;
002:
003:        import java.io.File;
004:        import java.io.IOException;
005:        import java.util.Date;
006:
007:        import javax.swing.event.ChangeEvent;
008:        import javax.swing.event.ChangeListener;
009:        import javax.swing.table.AbstractTableModel;
010:        import jxl.Workbook;
011:        import jxl.format.Alignment;
012:        import jxl.format.Colour;
013:        import jxl.write.DateTime;
014:        import jxl.write.Label;
015:        import jxl.write.Number;
016:        import jxl.write.WritableCellFormat;
017:        import jxl.write.WritableFont;
018:        import jxl.write.WritableSheet;
019:        import jxl.write.WritableWorkbook;
020:        import jxl.write.WriteException;
021:
022:        /**
023:         * Class used to export a table to Excel file
024:         */
025:        public class ExporterForXLSFile implements  ExportHelper {
026:            private static WritableWorkbook xlsWorkBook = null;
027:            private boolean stop = false;
028:
029:            /**
030:             * Export the data in the table to the specified file as a PDF file
031:             * @param abstractTableModel
032:             * @param changeListener
033:             * @param fileToExportTo
034:             * @throws ExportHelperException
035:             */
036:            public void export(AbstractTableModel abstractTableModel,
037:                    ChangeListener changeListener, File fileToExportTo)
038:                    throws ExportHelperException {
039:                this .stop = false;
040:
041:                xlsWorkBook = setupXLSWorkBook(abstractTableModel,
042:                        fileToExportTo);
043:                WritableSheet workSheet = xlsWorkBook.createSheet("data sheet",
044:                        1);
045:
046:                int rowCount = abstractTableModel.getRowCount();
047:                int columnCount = abstractTableModel.getColumnCount();
048:                // keeps info of the max character count for each column
049:                int[] maxCharacterCount = new int[columnCount];
050:
051:                // Set to 0 if you want to include row number
052:                int startPosition = 0;
053:
054:                // Processing column names - start from 1 as the first column is the row
055:                // number
056:                WritableFont verdanaFont = new WritableFont(
057:                        WritableFont.TAHOMA, 12);
058:                WritableCellFormat coloredFormat = new WritableCellFormat();
059:                WritableCellFormat simpleFormat = new WritableCellFormat();
060:                coloredFormat.setFont(verdanaFont);
061:                simpleFormat.setFont(verdanaFont);
062:
063:                try {
064:                    simpleFormat.setAlignment(Alignment.CENTRE);
065:                    coloredFormat.setAlignment(Alignment.CENTRE);
066:                    coloredFormat.setBackground(Colour.GRAY_25);
067:                    //Process each row
068:                    for (int rowIndex = startPosition; rowIndex < rowCount; rowIndex++) {
069:                        //If stopped, exit and export incomplete table
070:                        if (stop == true) {
071:                            break;
072:                        }
073:
074:                        // Process each column in the row - start from 1 as the first column
075:                        // is the row number
076:                        for (int columnIndex = startPosition; columnIndex < columnCount; columnIndex++) {
077:                            Object cell = abstractTableModel.getValueAt(
078:                                    rowIndex, columnIndex);
079:
080:                            // Change put in for demo only - please remove
081:                            Class aClass = abstractTableModel
082:                                    .getColumnClass(columnIndex);
083:                            if (aClass.getName().equals("java.lang.Number")) {
084:                                if (cell != null) {
085:                                    String value = cell.toString();
086:                                    Number tableCell = new Number(columnIndex,
087:                                            rowIndex + 2, Double
088:                                                    .parseDouble(value));
089:                                    tableCell.setCellFormat(simpleFormat);
090:                                    workSheet.addCell(tableCell);
091:                                    maxCharacterCount[columnIndex] = maxCharacterCount[columnIndex] <= value
092:                                            .length() ? value.length()
093:                                            : maxCharacterCount[columnIndex];
094:                                } else {
095:                                    Label tableCell = new Label(columnIndex,
096:                                            rowIndex + 2, "");
097:                                    tableCell.setCellFormat(simpleFormat);
098:                                    workSheet.addCell(tableCell);
099:                                }
100:                            } else {
101:                                if (aClass.getName().equals("java.util.Date")) {
102:                                    if (cell != null) {
103:                                        String value = cell.toString();
104:                                        DateTime tableCell = new DateTime(
105:                                                columnIndex, rowIndex + 2,
106:                                                new Date(value));
107:                                        tableCell.setCellFormat(simpleFormat);
108:                                        workSheet.addCell(tableCell);
109:                                        maxCharacterCount[columnIndex] = maxCharacterCount[columnIndex] <= value
110:                                                .length() ? value.length()
111:                                                : maxCharacterCount[columnIndex];
112:                                    } else {
113:                                        Label tableCell = new Label(
114:                                                columnIndex, rowIndex + 2, "");
115:                                        tableCell.setCellFormat(simpleFormat);
116:                                        workSheet.addCell(tableCell);
117:                                    }
118:                                } else {
119:                                    if (cell != null) {
120:
121:                                        String value = cell.toString();
122:                                        Label tableCell = new Label(
123:                                                columnIndex, rowIndex + 2,
124:                                                value);
125:                                        tableCell.setCellFormat(simpleFormat);
126:                                        workSheet.addCell(tableCell);
127:                                        maxCharacterCount[columnIndex] = maxCharacterCount[columnIndex] <= value
128:                                                .length() ? value.length()
129:                                                : maxCharacterCount[columnIndex];
130:                                    } else {
131:                                        Label tableCell = new Label(
132:                                                columnIndex, rowIndex + 2, "");
133:                                        tableCell.setCellFormat(simpleFormat);
134:                                        workSheet.addCell(tableCell);
135:                                    }
136:                                }
137:                            }
138:                            // End of change
139:
140:                        }
141:
142:                        //Inform the change listener
143:                        changeListener.stateChanged(new ChangeEvent(
144:                                new Integer(rowIndex)));
145:                    }
146:
147:                    for (int columnIndex = startPosition; columnIndex < columnCount; columnIndex++) {
148:                        String columnName = abstractTableModel
149:                                .getColumnName(columnIndex);
150:                        Label tableCell = new Label(columnIndex, 1, columnName);
151:
152:                        tableCell.setCellFormat(coloredFormat);
153:
154:                        // workSheet.setColumnView(columnIndex,fMetrics.stringWidth(columnName));
155:                        workSheet.addCell(tableCell);
156:                        maxCharacterCount[columnIndex] = maxCharacterCount[columnIndex] <= columnName
157:                                .length() ? columnName.length()
158:                                : maxCharacterCount[columnIndex];
159:
160:                        workSheet.setColumnView(columnIndex,
161:                                maxCharacterCount[columnIndex] + 10);
162:
163:                    }
164:
165:                    // write and flush the workbook			
166:                    xlsWorkBook.write();
167:                    xlsWorkBook.close();
168:                } catch (WriteException exc) {
169:                    throw new ExportHelperException(exc.getMessage());
170:                } catch (IOException exc) {
171:                    throw new ExportHelperException(exc.getMessage());
172:                }
173:            }
174:
175:            /**
176:             * Call this to stop the export.  This is required as it may be a long process and it runs in a seperate thread
177:             */
178:            public void stop() {
179:                this .stop = true;
180:            }
181:
182:            private static WritableWorkbook setupXLSWorkBook(
183:                    AbstractTableModel abstractTableModel, File fileToExportTo)
184:                    throws ExportHelperException {
185:                WritableWorkbook xlsWrkBook = null;
186:                try {
187:                    // Build a workbook
188:                    xlsWrkBook = Workbook.createWorkbook(fileToExportTo);
189:                } catch (IOException exc) {
190:                    throw new ExportHelperException(exc.getMessage());
191:                }
192:                return xlsWrkBook;
193:            }
194:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.