Package net.sf.basedb.util.excel
Class XlsxTableWriter
java.lang.Object
java.io.Writer
java.io.PrintWriter
net.sf.basedb.util.export.TableWriter
net.sf.basedb.util.excel.XlsxTableWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
This is a simple wrapper for the TableWriter class to make it
create an Excel workbook instead of a CSV file. There are some
things to consider:
The
tablePrintHeaders(Object...)
and TableWriter.tablePrintData(Object...)
(including overloaded variants) methods can be used to write a single row at
a time. The various print() and write() methods write to a single cell and
will advance one column at a time. The println() variants ends the current row
and starts over on the first column.
Writing data to Excel is by default handled by the AutoFormatter
implementation
which will try to infer the correct Excel data type and format string from the class
and value of the data.
It is possible to assign a different formatter implementation to a given column by
calling TableWriter.setColumnFormatter(int, Formatter)
, but note that the formatter is
only used by this class if it also implements the ExcelFormatter
interface.
Call saveTo(OutputStream)
to write the Excel workbook to an output location.- Since:
- 3.15
-
Field Summary
Modifier and TypeFieldDescriptionprivate final AutoFormatter
private int
private org.apache.poi.ss.usermodel.Row
private final boolean
private final String
private int
private final org.apache.poi.ss.usermodel.Sheet
private final CellStyleCreator
private final org.apache.poi.ss.usermodel.Workbook
Fields inherited from class java.io.PrintWriter
out
-
Constructor Summary
ConstructorDescriptionXlsxTableWriter
(String name) Create a new workbook with a single worksheet.XlsxTableWriter
(org.apache.poi.ss.usermodel.Workbook workbook, String name) Add a sheet to the given Excel workbook. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the workbook unless it was supplied in the constructor from an external source.Get the auto-formatter instance to use for columns that has no specified formatter viaTableWriter.setColumnFormatter(int, Formatter)
.Get the style creator that can be used for custom styling of cells.org.apache.poi.ss.usermodel.Row
Get row that was the last one to be printed.org.apache.poi.ss.usermodel.Sheet
getSheet()
Get the current sheet in the workbook.org.apache.poi.ss.usermodel.Workbook
Get the workbook.void
print
(boolean b) void
print
(char c) void
print
(char[] s) void
print
(double d) void
print
(float f) void
print
(int i) void
print
(long l) void
void
void
println()
void
println
(boolean b) void
println
(char c) void
println
(char[] s) void
println
(double d) void
println
(float f) void
println
(int i) void
println
(long l) void
void
void
saveTo
(OutputStream out) Save the workbook to the given stream.void
tablePrintData
(EncoderDecoder encoder, Map<Integer, Formatter<?>> formatters, Object... data) Print a data line to the parent writer.void
tablePrintHeaders
(Object... headers) Print a header line to the parent writer.void
write
(char[] buf) void
write
(char[] buf, int off, int len) void
write
(int c) void
void
private void
Write the data to the next cell.Methods inherited from class net.sf.basedb.util.export.TableWriter
getColumnFormatter, getDataSeparator, getEncoder, getNullValue, setColumnFormatter, setDataSeparator, setEncoder, setNullValue, tableEncode, tablePrintData, tablePrintData
Methods inherited from class java.io.PrintWriter
append, append, append, checkError, clearError, flush, format, format, printf, printf, setError
Methods inherited from class java.io.Writer
nullWriter
-
Field Details
-
name
-
workbook
private final org.apache.poi.ss.usermodel.Workbook workbook -
sheet
private final org.apache.poi.ss.usermodel.Sheet sheet -
externalWorkbook
private final boolean externalWorkbook -
styleCreator
-
autoFormatter
-
rowNo
private int rowNo -
currentRow
private org.apache.poi.ss.usermodel.Row currentRow -
colNo
private int colNo
-
-
Constructor Details
-
XlsxTableWriter
Create a new workbook with a single worksheet.- Parameters:
name
- The name of the worksheet
-
XlsxTableWriter
Add a sheet to the given Excel workbook.- Parameters:
name
- The name of the worksheet
-
-
Method Details
-
getWorkbook
public org.apache.poi.ss.usermodel.Workbook getWorkbook()Get the workbook. -
getSheet
public org.apache.poi.ss.usermodel.Sheet getSheet()Get the current sheet in the workbook. -
getCurrentRow
public org.apache.poi.ss.usermodel.Row getCurrentRow()Get row that was the last one to be printed. -
getAutoFormatter
Get the auto-formatter instance to use for columns that has no specified formatter viaTableWriter.setColumnFormatter(int, Formatter)
. -
getCellStyleCreator
Get the style creator that can be used for custom styling of cells. -
tablePrintHeaders
Description copied from class:TableWriter
Print a header line to the parent writer. The only difference between this method and theTableWriter.tablePrintData(Object...)
method is that no column formatters are used (since they operate on data and the headers are typically strings).- Overrides:
tablePrintHeaders
in classTableWriter
-
tablePrintData
public void tablePrintData(EncoderDecoder encoder, Map<Integer, Formatter<?>> formatters, Object... data) Description copied from class:TableWriter
Print a data line to the parent writer. Each value is separated by the data separator string ('tab' by default). Null values are replaced with the null value string (empty string by default). Non-null objects are converted to strings values by calling either their toString() method or using the formatter assigned for each column. If an encoder has been specified, the value is finally encoded withEncoderDecoder.encode(String)
.- Overrides:
tablePrintData
in classTableWriter
- Parameters:
encoder
- The encoder to use, or null to not use any encoderformatters
- Column formatters to use (null to always use the toString() method)data
- The values to write
-
close
public void close()Closes the workbook unless it was supplied in the constructor from an external source.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classPrintWriter
-
saveTo
Save the workbook to the given stream.- Throws:
IOException
-
write
public void write(int c) - Overrides:
write
in classPrintWriter
-
write
public void write(char[] buf, int off, int len) - Overrides:
write
in classPrintWriter
-
write
public void write(char[] buf) - Overrides:
write
in classPrintWriter
-
write
- Overrides:
write
in classPrintWriter
-
write
- Overrides:
write
in classPrintWriter
-
print
public void print(boolean b) - Overrides:
print
in classPrintWriter
-
print
public void print(char c) - Overrides:
print
in classPrintWriter
-
print
public void print(int i) - Overrides:
print
in classPrintWriter
-
print
public void print(long l) - Overrides:
print
in classPrintWriter
-
print
public void print(float f) - Overrides:
print
in classPrintWriter
-
print
public void print(double d) - Overrides:
print
in classPrintWriter
-
print
public void print(char[] s) - Overrides:
print
in classPrintWriter
-
print
- Overrides:
print
in classPrintWriter
-
print
- Overrides:
print
in classPrintWriter
-
println
public void println()- Overrides:
println
in classPrintWriter
-
println
public void println(boolean b) - Overrides:
println
in classPrintWriter
-
println
public void println(char c) - Overrides:
println
in classPrintWriter
-
println
public void println(int i) - Overrides:
println
in classPrintWriter
-
println
public void println(long l) - Overrides:
println
in classPrintWriter
-
println
public void println(float f) - Overrides:
println
in classPrintWriter
-
println
public void println(double d) - Overrides:
println
in classPrintWriter
-
println
public void println(char[] s) - Overrides:
println
in classPrintWriter
-
println
- Overrides:
println
in classPrintWriter
-
println
- Overrides:
println
in classPrintWriter
-
writeData
Write the data to the next cell. If colNo=0 a new row is created, otherwise the next cell in the current row is used. If 'endOfRow' the current row is ended (by setting colNo to 0) so that a new row is created the next time this method is called.
-