Package csv.impl
Class ExcelWriter
java.lang.Object
csv.impl.AbstractTableWriter
csv.impl.AbstractStreamTableWriter
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
Constructors Constructor Description 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
Modifier and Type Method Description voidclose()Closes the writer and writes the Excel to the underlying stream.org.apache.poi.ss.usermodel.SheetcreateSheet()Creates a new sheet for the workbook.org.apache.poi.ss.usermodel.SheetcreateSheet(int index)Creates a new sheet for the workbook at specified index.org.apache.poi.ss.usermodel.SheetcreateSheet(int index, java.lang.String name)Creates a new sheet for the workbook at specified index.ExcelFormattergetFormatter()Returns the formatter set for this ExcelWriter.org.apache.poi.ss.usermodel.CellgetOrCreateCell(int row, int column)Returns an existing cell or creates one.org.apache.poi.ss.usermodel.CellgetOrCreateCell(org.apache.poi.ss.usermodel.Row row, int column)Returns an existing cell or creates one.org.apache.poi.ss.usermodel.RowgetOrCreateRow(int row)Returns an existing row or creates one.org.apache.poi.ss.usermodel.SheetgetSheet()Returns the current sheet or creates a fresh one.org.apache.poi.ss.usermodel.WorkbookgetWorkbook()Returns the workbook or creates a fresh one.protected voidnotifyExcelListeners(org.apache.poi.ss.usermodel.Row row)Notifies all Excel Listeners about the new row.voidprintComment(java.lang.String comment, int row, int column)Prints a comment into the output stream.voidprintRow(java.lang.Object[] columns)Prints the values to the Excel file.voidprintRow(java.lang.Object[] columns, int rowNum)Prints the values to the Excel file at the given row.voidregisterExcelListener(ExcelListener l)Registers an Excel Listener.org.apache.poi.ss.usermodel.SheetselectSheet(int index)This method selects the sheet at given index.voidselectSheet(org.apache.poi.ss.usermodel.Sheet sheet)This method selects the given sheet.voidsetFormatter(ExcelFormatter formatter)Sets the formatter for this ExcelWriter.protected voidsetStyle(org.apache.poi.ss.usermodel.Cell cell, java.lang.Object value)Sets the style of a cell.voidsetValue(int row, int column, java.lang.Object value)Sets the value at the specified cell.voidsetValue(org.apache.poi.ss.usermodel.Cell cell, java.lang.Object value)Sets the value at the specified cell.voidsetValue(org.apache.poi.ss.usermodel.Row row, int column, java.lang.Object value)Sets the value at the specified cell.voidunregisterExcelListener(ExcelListener l)Unregisters an ExcelListener.Methods inherited from class csv.impl.AbstractStreamTableWriter
createOutputStreamWriter, createWriter, getCharsetEncoder, getOutputStream, getWriter, setCharset, setCharset, setCharsetEncoder, setOutputStreamMethods inherited from class csv.impl.AbstractTableWriter
convert, convert, getRowCount, getTypeConversionHandler, incrementRowCount, init, printComment, printRow, printRow, printRow, registerTypeConversionHandler, unregisterTypeConversionHandler
-
Constructor Details
-
ExcelWriter
public ExcelWriter()Default constructor. Please, notice that you are required to set the output stream before closing the writer. -
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 usedout- output stream to be used
-
ExcelWriter
public ExcelWriter(java.io.File file) throws java.io.IOExceptionConstructor 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.IOExceptionConstructor with existing workbook that needs to be written to a file.- Parameters:
workbook- the workbook to be usedfile- 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.IOExceptionConstructor 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.IOExceptionConstructor with existing workbook that needs to be written to a file.- Parameters:
workbook- the workbook to be usedfile- output file to be used- Throws:
java.io.IOException- when the file cannot be written to
-
-
Method Details
-
printRow
public void printRow(java.lang.Object[] columns) throws java.io.IOExceptionPrints 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 usingprintRow(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.IOExceptionPrints 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 sheetrowNum- index of row to be written- Throws:
java.io.IOException- when an error occurs while writing- See Also:
printRow(Object[])
-
printComment
public void printComment(java.lang.String comment, int row, int column) throws java.io.IOExceptionPrints a comment into the output stream. This implementation does nothing by default.- Specified by:
printCommentin interfaceTableWriter- Overrides:
printCommentin classAbstractTableWriter- Parameters:
comment- the comment to writerow- index of row for commentcolumn- 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 indexcolumn- 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 objectcolumn- 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 allExcelListeners 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 indexcolumn- column indexvalue- 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 objectcolumn- column indexvalue- 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 theExcelFormatterto set the correct style on this cell.- Parameters:
cell- cell objectvalue- 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 (seeprintRow(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 (seeprintRow(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 sheetindex- 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:
closein interfaceTableWriter- Overrides:
closein classAbstractStreamTableWriter- See Also:
AbstractStreamTableWriter.close()
-
registerExcelListener
Registers an Excel Listener. This listener will be informed whenever a new row was created.- Parameters:
l- the listener
-
unregisterExcelListener
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
Returns the formatter set for this ExcelWriter.- Returns:
- the formatter
-
setFormatter
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 callExcelFormatter.setStyle(ExcelWriter, Cell, Object).- Parameters:
cell- cell to be formattedvalue- value that was set- See Also:
setFormatter(ExcelFormatter)
-