ExcelExportHelper.java :  » XML-UI » xui32 » com » xoetrope » io » Java Open Source

Java Open Source » XML UI » xui32 
xui32 » com » xoetrope » io » ExcelExportHelper.java
package com.xoetrope.io;

import java.io.IOException;
import java.io.File;

import jxl.CellView;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;


/**
 * Export an Excel worksheet to a new workbook.
 *
 * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
 * the GNU Public License (GPL), please see license.txt for more details. If
 * you make commercial use of this software you must purchase a commercial
 * license from Xoetrope.</p>
 * <p> $Revision: 1.4 $</p>
 * @deprecated use the com.xoetrope.export classes instead
 */
public class ExcelExportHelper extends XExportHelper
{
  /**
   * The excel workbook being output
   */
  protected WritableWorkbook workbookOut = null;
  /**
   * The excel sheet being output
   */
  protected WritableSheet sheetOut = null;

  /**
   * The excel format of the title fields
   */
  protected WritableCellFormat titleFormat;

  /**
   * The excel format of the subtitle fields
   */
  protected WritableCellFormat subTitleFormat;

  /**
   * The excel format of the section divider fields
   */
  protected WritableCellFormat sectionFormat;

  /**
   * The excel format of the body fields
   */
  protected WritableCellFormat bodyFormat;

  /**
   * Flag indicating if the exporter is in the process of exporting a table
   */
  protected boolean isInTable;
  /**
   * Flag indicating if the exporter is in the process of exporting a table header
   */
  protected boolean isInTableHeader;

  /**
   * Flag indicating if the exporter is in the process of exporting a table record
   */
  protected boolean isInRecord;

  /**
   * Flag indicating if the exporter is in the process of exporting grouped elements
   */
  protected boolean isInGroup;

  /**
   * The current record or row index
   */
  protected int recordIndex;

  /**
   * The current colum or field index
   */
  protected int columnIndex;

  /**
   * Setup an exporter for excel sheets.
   * @param fileName The name of the file to export
   * @throws java.io.IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public ExcelExportHelper( String fileName ) throws IOException
  {
    defaultExtension = ".xls";

    workbookOut = Workbook.createWorkbook( new File( fileName ) );
    sheetOut = workbookOut.createSheet( "export", 0 );

    titleFormat = new WritableCellFormat();
    subTitleFormat = new WritableCellFormat();
    sectionFormat = new WritableCellFormat();
    bodyFormat = new WritableCellFormat();

    try {
      titleFormat.setBackground( Colour.VERY_LIGHT_YELLOW );
      subTitleFormat.setBackground( Colour.VERY_LIGHT_YELLOW );
      sectionFormat.setBackground( Colour.GRAY_25 );
      bodyFormat.setBackground( Colour.WHITE );
    }
    catch ( WriteException ex ) {
    }

    int[] charWidths = { 50, 50, 30, 20 };
    CellView cv;
    for ( int i = 0; i < charWidths.length; i++ ) {
      cv = sheetOut.getColumnView( i );
      cv.setSize( charWidths[ i ] * 256 );
      sheetOut.setColumnView( i, cv );
    }
  }

  /**
   * Write the opening of a document
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void startDocument() throws IOException
  {
    addCell( recordIndex++, 0, "CoolSelector Export", titleFormat );
  }

  /**
   * Write the ending of a document
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void endDocument() throws IOException
  {
  }

  /**
   * Write the opening of a table
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void startTable() throws IOException
  {
  }

  /**
   * Write the ending of a table
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void endTable() throws IOException
  {
  }

  /**
   * Write the opening of a table group of records
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void startGroup() throws IOException
  {
    isInGroup = true;
    columnIndex = 0;
  }

  /**
   * Write the ending of a table group of records
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void endGroup() throws IOException
  {
    isInGroup = false;
  }

  /**
   * Write the opening of a table record
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void startRecord() throws IOException
  {
    isInRecord = true;
  }

  /**
   * Write the ending of a table record
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void endRecord() throws IOException
  {
    recordIndex++;
  }

  /**
   * Write the opening of a table record
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void startHeader() throws IOException
  {
    isInTableHeader = true;
    columnIndex = 0;
  }

  /**
   * Write the ending of a table record
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void endHeader() throws IOException
  {
    isInTableHeader = false;
    recordIndex++;
  }

  /**
   * Write a field
   * @param name the field name
   * @param value the field value
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void writeField( String name, String value ) throws IOException
  {
    if ( isInTableHeader )
      addCell( recordIndex, columnIndex++, name, bodyFormat );
    else if ( isInGroup )
      addCell( recordIndex, columnIndex++, value, bodyFormat );
    else {
      addCell( recordIndex, 0, name, bodyFormat );
      addCell( recordIndex++, 1, value, bodyFormat );
    }
  }

  /**
   * Write the section title and terminate with a line feed, new line as appropriate
   * @param text the text to output
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void writeSectionTitle( String text ) throws IOException
  {
    addCell( recordIndex++, 0, text, sectionFormat );
  }

  /**
   * Write the begining/opening of an element
   * @param elementName the element name
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void startElement( String elementName ) throws IOException
  {
    firstField = true;
    columnIndex = 0;
  }

  /**
   * Write the matching ending element
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void matchElement() throws IOException
  {
  }

  /**
   * Write the end of an element opening
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void endElement() throws IOException
  {
  }

  /**
   * Write the closing of an element
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void closeElement() throws IOException
  {
  }

  /**
   * Write the end of a section
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void writeSectionEnd() throws IOException
  {
  }

  /**
   * Write the text and terminate with a line feed, new line as appropriate
   * @param elementName the element name
   * @param name the field or attribute name
   * @param value the field or attribute value
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void writeElement( String elementName, String name, String value ) throws IOException
  {
    addCell( recordIndex, 0, name, bodyFormat );
    addCell( recordIndex++, 1, value, bodyFormat );
  }

  /**
   * Write the text and terminate with a line feed, new line as appropriate
   * @param elementName the element name
   * @param names the field or attribute names
   * @param values the field or attribute value
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void writeElement( String elementName, String[] names, String[] values ) throws IOException
  {
    for ( int i = 0; i < names.length; i++ ) {
//      addCell( recordIndex, i, names[ i ], bodyFormat );
      addCell( recordIndex, i, values[ i ], bodyFormat );
    }
  }

  /**
   * Output a blank line
   * @throws IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void writeBlankLine() throws IOException
  {
    recordIndex++;
  }

  /**
   * Flushes and closes any existing stream
   * @throws java.io.IOException thrown if the stream is not ready or if it is in an invalid state
   */
  public void close() throws IOException
  {
    workbookOut.write();
    workbookOut.close();
  }

  /**
   * Adds a cell to an Excel sheet. Zero based indexing is used.
   * @param row the row in the sheet
   * @param col the column in the sheet 
   * @param value the value to write to the excel sheet's cell
   * @param cellFormat the cell format
   */
  protected void addCell( int row, int col, String value, WritableCellFormat cellFormat )
  {
    try {
      Label labelOut = new Label( col, row, value );
      labelOut.setCellFormat( cellFormat );
      sheetOut.addCell( labelOut );
    }
    catch ( WriteException ex ) {
      ex.printStackTrace();
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.