Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.

csv.impl
Class ExcelWriter

java.lang.Object
  extended by csv.impl.AbstractTableWriter
      extended by csv.impl.AbstractStreamTableWriter
          extended by csv.impl.ExcelWriter
All Implemented Interfaces:
TableWriter

public class ExcelWriter
extends AbstractStreamTableWriter

Provides ability to write Excel files. The Excel will be written with a call to close() only! Please notice that this implementation does not support writing formulas into cells, yet. Example:

java.io.File f = new java.io.File("excel-test.xls");
ExcelWriter out = new ExcelWriter(f);
out.printRow(new Object[] { "0:0", new Integer(3), new Date() });
out.printRow(new Object[] { "1:0", new Double(), "another String value" });
out.close();

Author:
RalphSchuster
See Also:
close()

Constructor Summary
ExcelWriter()
          Default constructor.
ExcelWriter(java.io.File file)
          Constructor for writing into a file.
ExcelWriter(java.io.OutputStream out)
          Constructor with defined output stream.
ExcelWriter(java.lang.String file)
          Constructor for writing into a file.
ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook)
          Constructor with existing workbook.
ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook, java.io.File file)
          Constructor with existing workbook that needs to be written to a file.
ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook, java.io.OutputStream out)
          Constructor with existing workbook and defined output stream.
ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook, java.lang.String file)
          Constructor with existing workbook that needs to be written to a file.
 
Method Summary
 void close()
          Closes the writer and writes the Excel to the underlying stream.
 org.apache.poi.ss.usermodel.Sheet createSheet()
          Creates a new sheet for the workbook.
 org.apache.poi.ss.usermodel.Sheet createSheet(int index)
          Creates a new sheet for the workbook at specified index.
 org.apache.poi.ss.usermodel.Sheet createSheet(int index, java.lang.String name)
          Creates a new sheet for the workbook at specified index.
 ExcelFormatter getFormatter()
          Returns the formatter set for this ExcelWriter.
 org.apache.poi.ss.usermodel.Cell getOrCreateCell(int row, int column)
          Returns an existing cell or creates one.
 org.apache.poi.ss.usermodel.Cell getOrCreateCell(org.apache.poi.ss.usermodel.Row row, int column)
          Returns an existing cell or creates one.
 org.apache.poi.ss.usermodel.Row getOrCreateRow(int row)
          Returns an existing row or creates one.
 org.apache.poi.ss.usermodel.Sheet getSheet()
          Returns the current sheet or creates a fresh one.
 org.apache.poi.ss.usermodel.Workbook getWorkbook()
          Returns the workbook or creates a fresh one.
protected  void notifyExcelListeners(org.apache.poi.ss.usermodel.Row row)
          Notifies all Excel Listeners about the new row.
 void printComment(java.lang.String comment, int row, int column)
          Prints a comment into the output stream.
 void printRow(java.lang.Object[] columns)
          Prints the values to the Excel file.
 void printRow(java.lang.Object[] columns, int rowNum)
          Prints the values to the Excel file at the given row.
 void registerExcelListener(ExcelListener l)
          Registers an Excel Listener.
 org.apache.poi.ss.usermodel.Sheet selectSheet(int index)
          This method selects the sheet at given index.
 void selectSheet(org.apache.poi.ss.usermodel.Sheet sheet)
          This method selects the given sheet.
 void setFormatter(ExcelFormatter formatter)
          Sets the formatter for this ExcelWriter.
protected  void setStyle(org.apache.poi.ss.usermodel.Cell cell, java.lang.Object value)
          Sets the style of a cell.
 void setValue(org.apache.poi.ss.usermodel.Cell cell, java.lang.Object value)
          Sets the value at the specified cell.
 void setValue(int row, int column, java.lang.Object value)
          Sets the value at the specified cell.
 void setValue(org.apache.poi.ss.usermodel.Row row, int column, java.lang.Object value)
          Sets the value at the specified cell.
 void unregisterExcelListener(ExcelListener l)
          Unregisters an ExcelListener.
 
Methods inherited from class csv.impl.AbstractStreamTableWriter
getOutputStream, getWriter, setOutputStream
 
Methods inherited from class csv.impl.AbstractTableWriter
convert, convert, getRowCount, getTypeConversionHandler, incrementRowCount, init, printComment, registerTypeConversionHandler, unregisterTypeConversionHandler
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExcelWriter

public ExcelWriter()
Default constructor. Please, notice that you are required to set the output stream before closing the writer.

See Also:
AbstractStreamTableWriter.setOutputStream(OutputStream)

ExcelWriter

public ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook)
Constructor with existing workbook. You can use this constructor if you wanna write to an existing workbook. Please, notice that you are required to set the output stream before closing the writer.

Parameters:
workbook - the workbook to be used
See Also:
AbstractStreamTableWriter.setOutputStream(OutputStream)

ExcelWriter

public ExcelWriter(java.io.OutputStream out)
Constructor with defined output stream. A new workbook will be created.

Parameters:
out - output stream to be used.

ExcelWriter

public ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook,
                   java.io.OutputStream out)
Constructor with existing workbook and defined output stream.

Parameters:
workbook - the workbook to be used
out - output stream to be used

ExcelWriter

public ExcelWriter(java.io.File file)
            throws java.io.IOException
Constructor for writing into a file. A new workbook will be created.

Parameters:
file - output file to be used
Throws:
java.io.IOException - when the file cannot be written to

ExcelWriter

public ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook,
                   java.io.File file)
            throws java.io.IOException
Constructor with existing workbook that needs to be written to a file.

Parameters:
workbook - the workbook to be used
file - output file to be used
Throws:
java.io.IOException - when the file cannot be written to

ExcelWriter

public ExcelWriter(java.lang.String file)
            throws java.io.IOException
Constructor for writing into a file. A new workbook will be created.

Parameters:
file - output file to be used
Throws:
java.io.IOException - when the file cannot be written to

ExcelWriter

public ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook,
                   java.lang.String file)
            throws java.io.IOException
Constructor with existing workbook that needs to be written to a file.

Parameters:
workbook - the workbook to be used
file - output file to be used
Throws:
java.io.IOException - when the file cannot be written to
Method Detail

printRow

public void printRow(java.lang.Object[] columns)
              throws java.io.IOException
Prints the values to the Excel file. Please note that this method maintains an internal row counter and will always start with row index 0 to write to. The method will automatically increase this internal counter. You could avoid this by using printRow(Object[], int)

Parameters:
columns - values to be written to the Excel sheet
Throws:
java.io.IOException - when an exception occurs
See Also:
TableWriter.printRow(java.lang.Object[])

printRow

public void printRow(java.lang.Object[] columns,
                     int rowNum)
              throws java.io.IOException
Prints the values to the Excel file at the given row. This method is useful in case you want to write specific rows, e.g. when writing to an existing workbook.

Parameters:
columns - values to be written to the Excel sheet
rowNum - index of row to be written
Throws:
java.io.IOException
See Also:
printRow(Object[])

printComment

public void printComment(java.lang.String comment,
                         int row,
                         int column)
                  throws java.io.IOException
Prints a comment into the output stream. This implementation does nothing by default.

Specified by:
printComment in interface TableWriter
Overrides:
printComment in class AbstractTableWriter
Parameters:
comment - the comment to write
row - index of row for comment
column - index of column for comment
Throws:
java.io.IOException - when an exception occurs

getOrCreateCell

public org.apache.poi.ss.usermodel.Cell getOrCreateCell(int row,
                                                        int column)
Returns an existing cell or creates one.

Parameters:
row - row index
column - column index
Returns:
cell object

getOrCreateCell

public org.apache.poi.ss.usermodel.Cell getOrCreateCell(org.apache.poi.ss.usermodel.Row row,
                                                        int column)
Returns an existing cell or creates one.

Parameters:
row - row object
column - column index
Returns:
cell object

getOrCreateRow

public org.apache.poi.ss.usermodel.Row getOrCreateRow(int row)
Returns an existing row or creates one. This method also notifies all ExcelListeners about a new row.

Parameters:
row - row index
Returns:
row object

setValue

public void setValue(int row,
                     int column,
                     java.lang.Object value)
Sets the value at the specified cell.

Parameters:
row - row index
column - column index
value - value to be set
See Also:
setValue(Cell, Object)

setValue

public void setValue(org.apache.poi.ss.usermodel.Row row,
                     int column,
                     java.lang.Object value)
Sets the value at the specified cell.

Parameters:
row - row object
column - column index
value - value to be set
See Also:
setValue(Cell, Object)

setValue

public void setValue(org.apache.poi.ss.usermodel.Cell cell,
                     java.lang.Object value)
Sets the value at the specified cell. This method automatically selects the correct type for the cell and notifies the ExcelFormatter to set the correct style on this cell.

Parameters:
cell - cell object
value - value to be set

getWorkbook

public org.apache.poi.ss.usermodel.Workbook getWorkbook()
Returns the workbook or creates a fresh one.

Returns:
the workbook

getSheet

public org.apache.poi.ss.usermodel.Sheet getSheet()
Returns the current sheet or creates a fresh one.

Returns:
the sheet

selectSheet

public void selectSheet(org.apache.poi.ss.usermodel.Sheet sheet)
This method selects the given sheet. This will reset the internal row counter (see printRow(Object[])).

Parameters:
sheet - sheet to be selected

selectSheet

public org.apache.poi.ss.usermodel.Sheet selectSheet(int index)
This method selects the sheet at given index. If no such sheet exists, it will be created. This will reset the internal row counter (see printRow(Object[])).

Parameters:
index - sheet index
Returns:
sheet selected

createSheet

public org.apache.poi.ss.usermodel.Sheet createSheet()
Creates a new sheet for the workbook.

Returns:
sheet created

createSheet

public org.apache.poi.ss.usermodel.Sheet createSheet(int index)
Creates a new sheet for the workbook at specified index.

Parameters:
index - of sheet (-1 adds the sheet at the end of all sheet)
Returns:
sheet created

createSheet

public org.apache.poi.ss.usermodel.Sheet createSheet(int index,
                                                     java.lang.String name)
Creates a new sheet for the workbook at specified index.

Parameters:
name - name of new sheet
index - of sheet (-1 adds the sheet at the end of all sheet)
Returns:
sheet created

close

public void close()
Closes the writer and writes the Excel to the underlying stream. Please note that all modifications of an Excel sheet appear in memory only and need to be written finally by calling this method.

Specified by:
close in interface TableWriter
Overrides:
close in class AbstractStreamTableWriter
See Also:
AbstractStreamTableWriter.close()

registerExcelListener

public void registerExcelListener(ExcelListener l)
Registers an Excel Listener. This listener will be informed whenever a new row was created.

Parameters:
l - the listener

unregisterExcelListener

public void unregisterExcelListener(ExcelListener l)
Unregisters an ExcelListener. Registered ExcelListeners will be informed whenever a new row was created.

Parameters:
l - the listener

notifyExcelListeners

protected void notifyExcelListeners(org.apache.poi.ss.usermodel.Row row)
Notifies all Excel Listeners about the new row.

Parameters:
row - the row that was created

getFormatter

public ExcelFormatter getFormatter()
Returns the formatter set for this ExcelWriter.

Returns:
the formatter

setFormatter

public void setFormatter(ExcelFormatter formatter)
Sets the formatter for this ExcelWriter. ExcelFormatter are responsible to set the correct style of cells. The ExcelFormatter will be informed whenever a value in a cell was modified.

Parameters:
formatter - the formatter to set
See Also:
ExcelFormatter, setValue(Cell, Object), setStyle(Cell, Object)

setStyle

protected void setStyle(org.apache.poi.ss.usermodel.Cell cell,
                        java.lang.Object value)
Sets the style of a cell. The method is called immediately after a cell was modified. The default implementation will call ExcelFormatter.setStyle(ExcelWriter, Cell, Object).

Parameters:
cell - cell to be formatted
value - value that was set
See Also:
setFormatter(ExcelFormatter)

Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.

Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.