Package csv.impl
Class ExcelWriter
java.lang.Object
csv.impl.AbstractTableWriter
csv.impl.AbstractStreamTableWriter
csv.impl.ExcelWriter
- All Implemented Interfaces:
TableWriter
- Direct Known Subclasses:
XExcelWriter
Provides ability to write classic 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:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.ExcelWriter(File file) Constructor for writing into a file.ExcelWriter(OutputStream out) Constructor with defined output stream.ExcelWriter(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, File file) Constructor with existing workbook that needs to be written to a file.ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook, OutputStream out) Constructor with existing workbook and defined output stream.ExcelWriter(org.apache.poi.ss.usermodel.Workbook workbook, String file) Constructor with existing workbook that needs to be written to a file. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the writer and writes the Excel to the underlying stream.org.apache.poi.ss.usermodel.SheetCreates 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, String name) Creates a new sheet for the workbook at specified index.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.WorkbookReturns 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(String comment, int row, int column) Prints a comment into the output stream.voidPrints the values to the Excel file.voidPrints the values to the Excel file at the given row.voidRegisters 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 voidSets the style of a cell.voidSets the value at the specified cell.voidSets the value at the specified cell.voidSets the value at the specified cell.voidUnregisters 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, getMapper, getRowCount, incrementRowCount, init, printComment, printRow, printRow, printRow, setMapper
-
Field Details
-
workbook
protected org.apache.poi.ss.usermodel.Workbook workbook
-
-
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:
-
ExcelWriter
Constructor with defined output stream. A new workbook will be created.- Parameters:
out- output stream to be used.
-
ExcelWriter
Constructor with existing workbook and defined output stream.- Parameters:
workbook- the workbook to be usedout- output stream to be used
-
ExcelWriter
Constructor for writing into a file. A new workbook will be created.- Parameters:
file- output file to be used- Throws:
IOException- when the file cannot be written to
-
ExcelWriter
Constructor with existing workbook that needs to be written to a file.- Parameters:
workbook- the workbook to be usedfile- output file to be used- Throws:
IOException- when the file cannot be written to
-
ExcelWriter
Constructor for writing into a file. A new workbook will be created.- Parameters:
file- output file to be used- Throws:
IOException- when the file cannot be written to
-
ExcelWriter
Constructor with existing workbook that needs to be written to a file.- Parameters:
workbook- the workbook to be usedfile- output file to be used- Throws:
IOException- when the file cannot be written to
-
-
Method Details
-
printRow
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 usingprintRow(Object[], int)- Parameters:
columns- values to be written to the Excel sheet- Throws:
IOException- when an exception occurs- See Also:
-
printRow
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 sheetrowNum- index of row to be written- Throws:
IOException- when an error occurs while writing- See Also:
-
printComment
Prints 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:
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
Sets the value at the specified cell.- Parameters:
row- row indexcolumn- column indexvalue- value to be set- See Also:
-
setValue
Sets the value at the specified cell.- Parameters:
row- row objectcolumn- column indexvalue- value to be set- See Also:
-
setValue
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
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:
-
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:
-
setStyle
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:
-