Class ExcelWriter
Writes records to a Microsoft Excel document.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.northconcepts.datapipeline.core.DataEndpoint
DataEndpoint.State -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intFields inherited from class com.northconcepts.datapipeline.core.AbstractWriter
currentRecordFields inherited from class com.northconcepts.datapipeline.core.DataEndpoint
lastRecord, PRODUCT, PRODUCT_VERSION, VENDOR, XML_INPUT_FACTORY_KEYFields inherited from class com.northconcepts.datapipeline.core.Endpoint
BUFFER_SIZE, captureElapsedTime, DEFAULT_READ_BUFFER_SIZEFields inherited from class com.northconcepts.datapipeline.core.DataObject
id, log, name, TIMESTAMP_FORMAT -
Constructor Summary
ConstructorsConstructorDescriptionExcelWriter(ExcelDocument document) Creates a writer for the document and appliesinitStyles(), enabling metadata writing on POI providers. -
Method Summary
Modifier and TypeMethodDescriptionaddDataCellStyle(FieldLocationPredicate predicate, ExcelCellStyleDecorator decorator) Adds cell styles to data cells matching the specified predicate.addDataHyperlink(FieldLocationPredicate predicate, ExcelHyperlinkFunction function) Adds hyperlinks to data cells matching the specified predicate.protected voidaddDataMetadata(Record record) Attaches the hyperlinks and cell styles registered for data cells to the record's fields; called only when metadata writing is enabled.addExceptionProperties(DataException exception) Adds this endpoint's current state to aDataException.addHeaderCellStyle(FieldLocationPredicate predicate, ExcelCellStyleDecorator decorator) Adds cell styles to header cells matching the specified predicate.addHeaderHyperlink(FieldLocationPredicate predicate, ExcelHyperlinkFunction function) Adds hyperlinks to header cells matching the specified predicate.protected voidaddHeaderMetadata(Record record) Attaches the hyperlinks and cell styles registered for header cells to the header record's fields; called only when metadata writing is enabled.voidclose()Indicates that this endpoint has finished reading or writing.intReturns the 0-based column that each record's first field is written to (defaults to 0).intReturns the 0-based row that the first record (or the header row) is written to (defaults to 0).Returns the number of columns to freeze at the left side of the Excel sheet.
Supported ProviderTypes are:Returns the number of rows to freeze at the top of the Excel sheet.
Supported ProviderTypes are:Returns how text values longer thanMAX_CELL_LENGTHcharacters are handled (defaults toLargeCellHandler.FAIL).intReturns the 0-based index of the sheet to write when no sheet name is set, or the position of a newly created sheet (default is -1, the end).Returns the name of the sheet to write, reusing an existing sheet by that name or creating it (default is null).voidInitializes default cell style formats for different field types.booleanIndicates if drop-down filters are added to the first written row (usually the header) on close (default is false).booleanIndicates if the columns should be expanded to fit the widest value (default is false).booleanReturns whether Excel metadata (such as hyperlinks and cell styles) should be written.voidopen()Makes this endpoint ready for reading or writing.setAutoFilterColumns(boolean autoFilterColumns) Indicates if a row containing a drop-down filter with values from each column should be added above the data rows.setAutofitColumns(boolean autofitColumns) Indicates if the columns should be expanded to fit the widest value.setDataFormat(FieldType fieldType, String dataFormat) Sets the data format for cells containing values of the specified field type.setDescription(String description) Sets the optional text shown for this endpoint inEndpoint.toString()and exception properties.setFieldNamesInFirstRow(boolean fieldNamesInFirstRow) Indicates if a header row of field names is written before the first record (default is true); must be called beforeAbstractWriter.open().setFirstColumnIndex(int firstColumnIndex) Sets the 0-based column that each record's first field is written to (defaults to 0).setFirstRowIndex(int firstRowIndex) Sets the 0-based row that the first record (or the header row) is written to (defaults to 0).setFreezeColumns(Integer freezeColumns) Sets the number of columns to freeze at the left side of the Excel sheet.
Supported ProviderTypes are:setFreezeRows(Integer freezeRows) Sets the number of rows to freeze at the top of the Excel sheet.setLargeCellHandler(LargeCellHandler largeCellHandler) Sets how text values longer thanMAX_CELL_LENGTHcharacters are handled; null restores the defaultLargeCellHandler.FAIL.setSheetIndex(int sheetIndex) Sets the 0-based index of the sheet to write when no sheet name is set, or the position of a newly created sheet (default is -1, the end).setSheetName(String sheetName) Sets the name of the sheet to write, reusing an existing sheet by that name or creating it (default is null).setStyleFormat(FieldType fieldType, String dataFormat) Deprecated.setWriteMetadata(boolean writeMetadata) Sets whether Excel metadata (such as hyperlinks and cell styles) should be written.protected voidwriteRecord(Record record) Writes one row; called first for the header row (seeAbstractWriter.isHeaderRow()) when field names are written, then for each record.Methods inherited from class com.northconcepts.datapipeline.core.AbstractWriter
createHeaderRecord, isFieldNamesInFirstRow, isHeaderRow, write, writeImplMethods inherited from class com.northconcepts.datapipeline.core.DataWriter
available, getNestedEndpoint, getNestedWriter, getRootEndpoint, getRootWriter, getWriterMethods inherited from class com.northconcepts.datapipeline.core.DataEndpoint
decrementRecordCount, enableJmx, getLastRecord, getRecordCount, getRecordCountAsBigInteger, getRecordCountAsString, incrementRecordCount, isRecordCountBigInteger, resetRecordCount, toStringMethods inherited from class com.northconcepts.datapipeline.core.Endpoint
addElapsedtime, assertClosed, assertNotOpened, assertOpened, finalize, getClosedOn, getDescription, getElapsedTime, getElapsedTimeAsString, getOpenedOn, getOpenElapsedTime, getOpenElapsedTimeAsString, getSelfTime, getSelfTimeAsString, getState, isCaptureElapsedTime, isClosed, isOpen, setCaptureElapsedTime
-
Field Details
-
MAX_CELL_LENGTH
public static final int MAX_CELL_LENGTH- See Also:
-
MAX_CELL_LENGTH_INDEX
public static final int MAX_CELL_LENGTH_INDEX- See Also:
-
-
Constructor Details
-
ExcelWriter
Creates a writer for the document and appliesinitStyles(), enabling metadata writing on POI providers.
-
-
Method Details
-
initStyles
public void initStyles()Initializes default cell style formats for different field types. This method is called automatically and sets formats like "m/d/yy" for dates, "0.00" for decimals, etc. -
addExceptionProperties
Description copied from class:EndpointAdds this endpoint's current state to aDataException. Since this method is called whenever an exception is thrown, subclasses should override it to add their specific information.- Overrides:
addExceptionPropertiesin classAbstractWriter
-
isAutofitColumns
public boolean isAutofitColumns()Indicates if the columns should be expanded to fit the widest value (default is false). -
setAutofitColumns
Indicates if the columns should be expanded to fit the widest value. -
isAutofilterColumns
public boolean isAutofilterColumns()Indicates if drop-down filters are added to the first written row (usually the header) on close (default is false). -
setAutoFilterColumns
Indicates if a row containing a drop-down filter with values from each column should be added above the data rows. -
getSheetName
Returns the name of the sheet to write, reusing an existing sheet by that name or creating it (default is null). -
setSheetName
Sets the name of the sheet to write, reusing an existing sheet by that name or creating it (default is null). -
getSheetIndex
public int getSheetIndex()Returns the 0-based index of the sheet to write when no sheet name is set, or the position of a newly created sheet (default is -1, the end). -
setSheetIndex
Sets the 0-based index of the sheet to write when no sheet name is set, or the position of a newly created sheet (default is -1, the end). -
getFirstColumnIndex
public int getFirstColumnIndex()Returns the 0-based column that each record's first field is written to (defaults to 0). -
setFirstColumnIndex
Sets the 0-based column that each record's first field is written to (defaults to 0). -
getFirstRowIndex
public int getFirstRowIndex()Returns the 0-based row that the first record (or the header row) is written to (defaults to 0). -
setFirstRowIndex
Sets the 0-based row that the first record (or the header row) is written to (defaults to 0). -
setFieldNamesInFirstRow
Description copied from class:AbstractWriterIndicates if a header row of field names is written before the first record (default is true); must be called beforeAbstractWriter.open().- Overrides:
setFieldNamesInFirstRowin classAbstractWriter
-
getLargeCellHandler
Returns how text values longer thanMAX_CELL_LENGTHcharacters are handled (defaults toLargeCellHandler.FAIL). -
setLargeCellHandler
Sets how text values longer thanMAX_CELL_LENGTHcharacters are handled; null restores the defaultLargeCellHandler.FAIL. -
getFreezeRows
Returns the number of rows to freeze at the top of the Excel sheet.
Supported ProviderTypes are:-
ExcelDocument.ProviderType.POI, -ExcelDocument.ProviderType.POI_XSSF, -ExcelDocument.ProviderType.POI_SXSSF, -ExcelDocument.ProviderType.POI_XSSF_SAX -
setFreezeRows
Sets the number of rows to freeze at the top of the Excel sheet.
Supported ProviderTypes are:-
ExcelDocument.ProviderType.POI, -ExcelDocument.ProviderType.POI_XSSF, -ExcelDocument.ProviderType.POI_SXSSF, -ExcelDocument.ProviderType.POI_XSSF_SAX -
getFreezeColumns
Returns the number of columns to freeze at the left side of the Excel sheet.
Supported ProviderTypes are:-
ExcelDocument.ProviderType.POI, -ExcelDocument.ProviderType.POI_XSSF, -ExcelDocument.ProviderType.POI_SXSSF, -ExcelDocument.ProviderType.POI_XSSF_SAX -
setFreezeColumns
Sets the number of columns to freeze at the left side of the Excel sheet.
Supported ProviderTypes are:-
ExcelDocument.ProviderType.POI, -ExcelDocument.ProviderType.POI_XSSF, -ExcelDocument.ProviderType.POI_SXSSF, -ExcelDocument.ProviderType.POI_XSSF_SAX -
isWriteMetadata
public boolean isWriteMetadata()Returns whether Excel metadata (such as hyperlinks and cell styles) should be written. When enabled, metadata from field session properties is written to the Excel file.- Returns:
- true if metadata writing is enabled, false otherwise
- See Also:
-
setWriteMetadata
Sets whether Excel metadata (such as hyperlinks and cell styles) should be written. When enabled, metadata from field session properties is written to the Excel file.- Parameters:
writeMetadata- true to enable metadata writing, false to disable (default is false)- Returns:
- this ExcelWriter instance for method chaining
- See Also:
-
setDescription
Description copied from class:EndpointSets the optional text shown for this endpoint inEndpoint.toString()and exception properties.- Overrides:
setDescriptionin classEndpoint
-
open
public void open()Description copied from class:DataEndpointMakes this endpoint ready for reading or writing.- Overrides:
openin classAbstractWriter
-
close
public void close()Description copied from class:DataEndpointIndicates that this endpoint has finished reading or writing.- Overrides:
closein classDataEndpoint
-
getDocument
-
writeRecord
Description copied from class:AbstractWriterWrites one row; called first for the header row (seeAbstractWriter.isHeaderRow()) when field names are written, then for each record.- Specified by:
writeRecordin classAbstractWriter- Throws:
Throwable
-
setStyleFormat
Deprecated.usesetDataFormat(FieldType, String)instead.Sets the data format for cells containing values of the specified field type.
- Parameters:
fieldType- the field type to set the data format fordataFormat- the Excel data format string (e.g. "m/d/yy" for dates, "0.00" for decimals)- Returns:
- this ExcelWriter instance for method chaining
- See Also:
-
setDataFormat
Sets the data format for cells containing values of the specified field type.- Parameters:
fieldType- the field type to set the data format fordataFormat- the Excel data format string (e.g. "m/d/yy" for dates, "0.00" for decimals)- Returns:
- this ExcelWriter instance for method chaining
- See Also:
-
addHeaderHyperlink
public ExcelWriter addHeaderHyperlink(FieldLocationPredicate predicate, ExcelHyperlinkFunction function) Adds hyperlinks to header cells matching the specified predicate. Automatically enables metadata writing and field names in the first row.- Parameters:
predicate- the predicate to match header cell locations, or null to apply to all headersfunction- the function that creates hyperlinks based on field location- Returns:
- this ExcelWriter instance for method chaining
- See Also:
-
addHeaderCellStyle
public ExcelWriter addHeaderCellStyle(FieldLocationPredicate predicate, ExcelCellStyleDecorator decorator) Adds cell styles to header cells matching the specified predicate. Automatically enables metadata writing and field names in the first row.- Parameters:
predicate- the predicate to match header cell locations, or null to apply to all headersdecorator- the decorator that applies cell styles based on field location- Returns:
- this ExcelWriter instance for method chaining
- See Also:
-
addHeaderMetadata
Attaches the hyperlinks and cell styles registered for header cells to the header record's fields; called only when metadata writing is enabled.- Throws:
Throwable
-
addDataHyperlink
public ExcelWriter addDataHyperlink(FieldLocationPredicate predicate, ExcelHyperlinkFunction function) Adds hyperlinks to data cells matching the specified predicate. Automatically enables metadata writing.- Parameters:
predicate- the predicate to match data cell locations, or null to apply to all data cellsfunction- the function that creates hyperlinks based on field location- Returns:
- this ExcelWriter instance for method chaining
- See Also:
-
addDataCellStyle
public ExcelWriter addDataCellStyle(FieldLocationPredicate predicate, ExcelCellStyleDecorator decorator) Adds cell styles to data cells matching the specified predicate. Automatically enables metadata writing.- Parameters:
predicate- the predicate to match data cell locations, or null to apply to all data cellsdecorator- the decorator that applies cell styles based on field location- Returns:
- this ExcelWriter instance for method chaining
- See Also:
-
addDataMetadata
Attaches the hyperlinks and cell styles registered for data cells to the record's fields; called only when metadata writing is enabled.- Throws:
Throwable
-
setDataFormat(FieldType, String)instead.