001: package com.xoetrope.io;
002:
003: import java.io.IOException;
004: import java.io.File;
005:
006: import jxl.CellView;
007: import jxl.Workbook;
008: import jxl.format.Colour;
009: import jxl.write.Label;
010: import jxl.write.WritableCellFormat;
011: import jxl.write.WritableSheet;
012: import jxl.write.WritableWorkbook;
013: import jxl.write.WriteException;
014:
015: /**
016: * Export an Excel worksheet to a new workbook.
017: *
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * <p> $Revision: 1.4 $</p>
023: * @deprecated use the com.xoetrope.export classes instead
024: */
025: public class ExcelExportHelper extends XExportHelper {
026: /**
027: * The excel workbook being output
028: */
029: protected WritableWorkbook workbookOut = null;
030: /**
031: * The excel sheet being output
032: */
033: protected WritableSheet sheetOut = null;
034:
035: /**
036: * The excel format of the title fields
037: */
038: protected WritableCellFormat titleFormat;
039:
040: /**
041: * The excel format of the subtitle fields
042: */
043: protected WritableCellFormat subTitleFormat;
044:
045: /**
046: * The excel format of the section divider fields
047: */
048: protected WritableCellFormat sectionFormat;
049:
050: /**
051: * The excel format of the body fields
052: */
053: protected WritableCellFormat bodyFormat;
054:
055: /**
056: * Flag indicating if the exporter is in the process of exporting a table
057: */
058: protected boolean isInTable;
059: /**
060: * Flag indicating if the exporter is in the process of exporting a table header
061: */
062: protected boolean isInTableHeader;
063:
064: /**
065: * Flag indicating if the exporter is in the process of exporting a table record
066: */
067: protected boolean isInRecord;
068:
069: /**
070: * Flag indicating if the exporter is in the process of exporting grouped elements
071: */
072: protected boolean isInGroup;
073:
074: /**
075: * The current record or row index
076: */
077: protected int recordIndex;
078:
079: /**
080: * The current colum or field index
081: */
082: protected int columnIndex;
083:
084: /**
085: * Setup an exporter for excel sheets.
086: * @param fileName The name of the file to export
087: * @throws java.io.IOException thrown if the stream is not ready or if it is in an invalid state
088: */
089: public ExcelExportHelper(String fileName) throws IOException {
090: defaultExtension = ".xls";
091:
092: workbookOut = Workbook.createWorkbook(new File(fileName));
093: sheetOut = workbookOut.createSheet("export", 0);
094:
095: titleFormat = new WritableCellFormat();
096: subTitleFormat = new WritableCellFormat();
097: sectionFormat = new WritableCellFormat();
098: bodyFormat = new WritableCellFormat();
099:
100: try {
101: titleFormat.setBackground(Colour.VERY_LIGHT_YELLOW);
102: subTitleFormat.setBackground(Colour.VERY_LIGHT_YELLOW);
103: sectionFormat.setBackground(Colour.GRAY_25);
104: bodyFormat.setBackground(Colour.WHITE);
105: } catch (WriteException ex) {
106: }
107:
108: int[] charWidths = { 50, 50, 30, 20 };
109: CellView cv;
110: for (int i = 0; i < charWidths.length; i++) {
111: cv = sheetOut.getColumnView(i);
112: cv.setSize(charWidths[i] * 256);
113: sheetOut.setColumnView(i, cv);
114: }
115: }
116:
117: /**
118: * Write the opening of a document
119: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
120: */
121: public void startDocument() throws IOException {
122: addCell(recordIndex++, 0, "CoolSelector Export", titleFormat);
123: }
124:
125: /**
126: * Write the ending of a document
127: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
128: */
129: public void endDocument() throws IOException {
130: }
131:
132: /**
133: * Write the opening of a table
134: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
135: */
136: public void startTable() throws IOException {
137: }
138:
139: /**
140: * Write the ending of a table
141: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
142: */
143: public void endTable() throws IOException {
144: }
145:
146: /**
147: * Write the opening of a table group of records
148: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
149: */
150: public void startGroup() throws IOException {
151: isInGroup = true;
152: columnIndex = 0;
153: }
154:
155: /**
156: * Write the ending of a table group of records
157: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
158: */
159: public void endGroup() throws IOException {
160: isInGroup = false;
161: }
162:
163: /**
164: * Write the opening of a table record
165: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
166: */
167: public void startRecord() throws IOException {
168: isInRecord = true;
169: }
170:
171: /**
172: * Write the ending of a table record
173: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
174: */
175: public void endRecord() throws IOException {
176: recordIndex++;
177: }
178:
179: /**
180: * Write the opening of a table record
181: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
182: */
183: public void startHeader() throws IOException {
184: isInTableHeader = true;
185: columnIndex = 0;
186: }
187:
188: /**
189: * Write the ending of a table record
190: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
191: */
192: public void endHeader() throws IOException {
193: isInTableHeader = false;
194: recordIndex++;
195: }
196:
197: /**
198: * Write a field
199: * @param name the field name
200: * @param value the field value
201: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
202: */
203: public void writeField(String name, String value)
204: throws IOException {
205: if (isInTableHeader)
206: addCell(recordIndex, columnIndex++, name, bodyFormat);
207: else if (isInGroup)
208: addCell(recordIndex, columnIndex++, value, bodyFormat);
209: else {
210: addCell(recordIndex, 0, name, bodyFormat);
211: addCell(recordIndex++, 1, value, bodyFormat);
212: }
213: }
214:
215: /**
216: * Write the section title and terminate with a line feed, new line as appropriate
217: * @param text the text to output
218: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
219: */
220: public void writeSectionTitle(String text) throws IOException {
221: addCell(recordIndex++, 0, text, sectionFormat);
222: }
223:
224: /**
225: * Write the begining/opening of an element
226: * @param elementName the element name
227: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
228: */
229: public void startElement(String elementName) throws IOException {
230: firstField = true;
231: columnIndex = 0;
232: }
233:
234: /**
235: * Write the matching ending element
236: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
237: */
238: public void matchElement() throws IOException {
239: }
240:
241: /**
242: * Write the end of an element opening
243: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
244: */
245: public void endElement() throws IOException {
246: }
247:
248: /**
249: * Write the closing of an element
250: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
251: */
252: public void closeElement() throws IOException {
253: }
254:
255: /**
256: * Write the end of a section
257: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
258: */
259: public void writeSectionEnd() throws IOException {
260: }
261:
262: /**
263: * Write the text and terminate with a line feed, new line as appropriate
264: * @param elementName the element name
265: * @param name the field or attribute name
266: * @param value the field or attribute value
267: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
268: */
269: public void writeElement(String elementName, String name,
270: String value) throws IOException {
271: addCell(recordIndex, 0, name, bodyFormat);
272: addCell(recordIndex++, 1, value, bodyFormat);
273: }
274:
275: /**
276: * Write the text and terminate with a line feed, new line as appropriate
277: * @param elementName the element name
278: * @param names the field or attribute names
279: * @param values the field or attribute value
280: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
281: */
282: public void writeElement(String elementName, String[] names,
283: String[] values) throws IOException {
284: for (int i = 0; i < names.length; i++) {
285: // addCell( recordIndex, i, names[ i ], bodyFormat );
286: addCell(recordIndex, i, values[i], bodyFormat);
287: }
288: }
289:
290: /**
291: * Output a blank line
292: * @throws IOException thrown if the stream is not ready or if it is in an invalid state
293: */
294: public void writeBlankLine() throws IOException {
295: recordIndex++;
296: }
297:
298: /**
299: * Flushes and closes any existing stream
300: * @throws java.io.IOException thrown if the stream is not ready or if it is in an invalid state
301: */
302: public void close() throws IOException {
303: workbookOut.write();
304: workbookOut.close();
305: }
306:
307: /**
308: * Adds a cell to an Excel sheet. Zero based indexing is used.
309: * @param row the row in the sheet
310: * @param col the column in the sheet
311: * @param value the value to write to the excel sheet's cell
312: * @param cellFormat the cell format
313: */
314: protected void addCell(int row, int col, String value,
315: WritableCellFormat cellFormat) {
316: try {
317: Label labelOut = new Label(col, row, value);
318: labelOut.setCellFormat(cellFormat);
319: sheetOut.addCell(labelOut);
320: } catch (WriteException ex) {
321: ex.printStackTrace();
322: }
323: }
324: }
|