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: }
|