Package csv.impl
Class DefaultExcelFormatter
java.lang.Object
csv.impl.DefaultExcelFormatter
- All Implemented Interfaces:
ExcelFormatter
Default implementation of an ExcelFormatter.
This class provides a default implementation that provides some basic
functionality to emphasize the header row in a sheet by a bold font and
formatting hyperlinks in cells.
You can derive from this implementation to change formatting, e.g. just
setting another color or font size.
- Author:
- RalphSchuster
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classDescribes a style for a cell. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringdatetime format "dd.mm.yyyy hh:mm"static final Stringdatetime format "dd.mm.yyyy hh:mm"static final shortColor Blackstatic final StringArial font namestatic final shortFont Size 10static final Stringinteger format "0"static final Stringreal format "0.00"static final shortColor Blue -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.DefaultExcelFormatter(boolean emphasizeFirstRow) Constructor for defining the emphasizing of header rows.DefaultExcelFormatter(boolean emphasizeFirstRow, boolean autofilter) Constructor for defining the emphasizing of header rows.DefaultExcelFormatter(boolean emphasizeFirstRow, boolean autofilter, String defaultFontName, Short defaultFontSize, Short defaultFontColor, Short defaultHyperlinkColor) Constructor for defining the various properties.DefaultExcelFormatter(boolean emphasizeFirstRow, String defaultFontName, Short defaultFontSize, Short defaultFontColor, Short defaultHyperlinkColor) Constructor for defining the various properties. -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyAutofilter(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.usermodel.Row row) Sets the autofilter property in this row.voidfinalize(ExcelWriter writer) Finalizes the workbook.voidfinalize(ExcelWriter writer, int rowCount, int columnCount) Deprecated.voidfinalizeFirstRow(ExcelWriter writer, org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, int sheetIndex, org.apache.poi.ss.usermodel.Row row, org.apache.poi.ss.usermodel.Cell cell, int cellIndex) Finalized the first row of the given sheet by setting the autosize of the column.voidfinalizeSheet(ExcelWriter writer, org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, int sheetIndex) Finalizes the given sheet in the workbook.org.apache.poi.ss.usermodel.HorizontalAlignmentgetAlign(ExcelWriter writer, int row, int column, Object value) Returns the alignment to be used.getBackgroundColor(ExcelWriter writer, int row, int column, Object value) Returns the background color for the specified cell.org.apache.poi.ss.usermodel.FontgetBoldFont(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column, Object value) Returns the bold font used for header rows.getBottomBorderColor(ExcelWriter writer, int row, int column, Object value) org.apache.poi.ss.usermodel.BorderStylegetBottomBorderThickness(ExcelWriter writer, int row, int column, Object value) getCurrencyFormat(int row, int column, Object value) Returns the default format for currencies.protected ShortgetCurrencyFormat(ExcelWriter writer, String format) Returns the ID of the format or creates a new one if required.getDateFormat(int row, int column, Object value) Returns the default format for dates.protected ShortgetDateFormat(ExcelWriter writer, String format) Returns the ID of the format or creates a new one if required.getDateTimeFormat(int row, int column, Object value) Returns the default format for dates.org.apache.poi.ss.usermodel.FontgetDefaultBoldFont(org.apache.poi.ss.usermodel.Workbook workbook) Returns the default bold font.shortReturns the font color to be used in non-hyperlink cells.Returns the font name to be used.shortReturns the font size to be used.shortReturns the font color to be used for hyperlinks.org.apache.poi.ss.usermodel.FontgetDefaultHyperlinkFont(org.apache.poi.ss.usermodel.Workbook workbook) Returns the font to be used for hyperlinks.org.apache.poi.ss.usermodel.FontgetDefaultPlainFont(org.apache.poi.ss.usermodel.Workbook workbook) Returns the default font used for normal cells.org.apache.poi.ss.usermodel.FillPatternTypegetFillPattern(ExcelWriter writer, int row, int column, Object value) Returns the fill pattern for the background.org.apache.poi.ss.usermodel.FontgetFont(ExcelWriter writer, int row, int column, Object value) Returns the correct font for the cell.getForegroundColor(ExcelWriter writer, int row, int column, Object value) Returns the foreground color for the specified cell.getFormat(ExcelWriter writer, int row, int column, Object value) Returns the display format.org.apache.poi.ss.usermodel.HyperlinkgetHyperlink(ExcelWriter writer, int row, int column, Object value) Returns a hyperlink object when the given cell shall be linked.org.apache.poi.ss.usermodel.FontgetHyperlinkFont(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column, Object value) Returns the font to be used for hyperlinks.getIntegerFormat(int row, int column, Object value) Returns the default format for shorts, integers and longs.protected ShortgetIntegerFormat(ExcelWriter writer, String format) Returns the ID of the format or creates a new one if required.getLeftBorderColor(ExcelWriter writer, int row, int column, Object value) org.apache.poi.ss.usermodel.BorderStylegetLeftBorderThickness(ExcelWriter writer, int row, int column, Object value) org.apache.poi.ss.usermodel.FontgetPlainFont(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column, Object value) Returns the default font used for normal cells.getRealFormat(int row, int column, Object value) Returns the default format for real and float numbers.protected ShortgetRealFormat(ExcelWriter writer, String format) Returns the ID of the format or creates a new one if required.getRightBorderColor(ExcelWriter writer, int row, int column, Object value) org.apache.poi.ss.usermodel.BorderStylegetRightBorderThickness(ExcelWriter writer, int row, int column, Object value) getTopBorderColor(ExcelWriter writer, int row, int column, Object value) org.apache.poi.ss.usermodel.BorderStylegetTopBorderThickness(ExcelWriter writer, int row, int column, Object value) voidinit()Initializes the formatter.booleanReturns whether row 0 shall have autofilter activated.booleanReturns the setting of emphasizing the header row.booleanisTextWrap(ExcelWriter writer, int row, int column, Object value) voidsetAutofilter(boolean autofilter) Sets whether row 0 shall have autofilter activated.voidsetDefaultBorderColor(Short borderColor) voidsetDefaultBorderThickness(org.apache.poi.ss.usermodel.BorderStyle borderThickness) voidsetEmphasizeFirstRow(boolean emphasizeFirstRow) Sets the property of emphasizing header rows.voidsetStyle(ExcelWriter writer, org.apache.poi.ss.usermodel.Cell cell, Object value) Sets the cell style.
-
Field Details
-
DEFAULT_FONT_NAME
Arial font name- See Also:
-
DEFAULT_FONT_COLOR
public static final short DEFAULT_FONT_COLORColor Black -
HYPERLINK_FONT_COLOR
public static final short HYPERLINK_FONT_COLORColor Blue -
DEFAULT_FONT_SIZE
public static final short DEFAULT_FONT_SIZEFont Size 10- See Also:
-
DEFAULT_DATE_FORMAT
datetime format "dd.mm.yyyy hh:mm"- See Also:
-
DEFAULT_DATETIME_FORMAT
datetime format "dd.mm.yyyy hh:mm"- See Also:
-
DEFAULT_INTEGER_FORMAT
integer format "0"- See Also:
-
DEFAULT_REAL_FORMAT
real format "0.00"- See Also:
-
-
Constructor Details
-
DefaultExcelFormatter
public DefaultExcelFormatter()Default constructor. This is without any formatting. -
DefaultExcelFormatter
public DefaultExcelFormatter(boolean emphasizeFirstRow) Constructor for defining the emphasizing of header rows.- Parameters:
emphasizeFirstRow- whether row 0 shall be set in bold font- See Also:
-
DefaultExcelFormatter
public DefaultExcelFormatter(boolean emphasizeFirstRow, boolean autofilter) Constructor for defining the emphasizing of header rows.- Parameters:
emphasizeFirstRow- whether row 0 shall be set in bold fontautofilter- whether row 0 shall have autofilter activated- See Also:
-
DefaultExcelFormatter
public DefaultExcelFormatter(boolean emphasizeFirstRow, String defaultFontName, Short defaultFontSize, Short defaultFontColor, Short defaultHyperlinkColor) Constructor for defining the various properties.- Parameters:
emphasizeFirstRow- whether row 0 shall be set in bold fontdefaultFontName- font name of default fontdefaultFontSize- font size to be useddefaultFontColor- color to be used for fontdefaultHyperlinkColor- color for hyperlinks to be used- See Also:
-
DefaultExcelFormatter
public DefaultExcelFormatter(boolean emphasizeFirstRow, boolean autofilter, String defaultFontName, Short defaultFontSize, Short defaultFontColor, Short defaultHyperlinkColor) Constructor for defining the various properties.- Parameters:
emphasizeFirstRow- whether row 0 shall be set in bold fontautofilter- whether row 0 shall have autofilter activateddefaultFontName- font name of default fontdefaultFontSize- font size to be useddefaultFontColor- color to be used for fontdefaultHyperlinkColor- color for hyperlinks to be used- See Also:
-
-
Method Details
-
init
public void init()Initializes the formatter. -
isAutofilter
public boolean isAutofilter()Returns whether row 0 shall have autofilter activated.- Returns:
- whether row 0 shall have autofilter activated
-
setAutofilter
public void setAutofilter(boolean autofilter) Sets whether row 0 shall have autofilter activated.- Parameters:
autofilter- whether row 0 shall have autofilter activated
-
setStyle
Sets the cell style. This implementations calls various other methods to define the style of the cell.- Specified by:
setStylein interfaceExcelFormatter- Parameters:
writer- writer that requires the informationcell- cell to be formattedvalue- value in cell- See Also:
-
getFormat(ExcelWriter, int, int, Object)getBackgroundColor(ExcelWriter, int, int, Object)getFillPattern(ExcelWriter, int, int, Object)getForegroundColor(ExcelWriter, int, int, Object)getFont(ExcelWriter, int, int, Object)getAlign(ExcelWriter, int, int, Object)getHyperlink(ExcelWriter, int, int, Object)
-
getHyperlink
public org.apache.poi.ss.usermodel.Hyperlink getHyperlink(ExcelWriter writer, int row, int column, Object value) Returns a hyperlink object when the given cell shall be linked. Notice that you should return a blue underlined font ingetFont(ExcelWriter, int, int, Object)when you return a hyperlink here.- Parameters:
writer- the calling writerrow- row indexcolumn- column indexvalue- value in cell- Returns:
- hyperlink object for the cell
-
finalize
Deprecated.Usefinalize(ExcelWriter)instead.Finalizes the workbook. This method is called immediately before theExcelWriterwrites the complete workbook to the underlying output stream. This implementation just sets all columns to auto fit.- Specified by:
finalizein interfaceExcelFormatter- Parameters:
writer- the calling writerrowCount- the number of rows in the selected sheetcolumnCount- the number of columns modified in the selected sheet
-
finalize
Finalizes the workbook. This method is called immediately before theExcelWriterwrites the complete workbook to the underlying output stream.- Specified by:
finalizein interfaceExcelFormatter- Parameters:
writer- the calling writer- See Also:
-
finalizeSheet
public void finalizeSheet(ExcelWriter writer, org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, int sheetIndex) Finalizes the given sheet in the workbook. The method is automatically called byfinalize(ExcelWriter)and takes care of finalizing the first row in a sheet by callingfinalizeFirstRow(ExcelWriter, Workbook, Sheet, int, Row, Cell, int).- Parameters:
writer- - writer the calling writerworkbook- - the workbook to be finalizedsheet- - the sheet to be finalizedsheetIndex- - the index of the sheet- Since:
- 4.1
- See Also:
-
applyAutofilter
public void applyAutofilter(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.usermodel.Row row) Sets the autofilter property in this row.- Parameters:
workbook- - the workbook to be finalizedsheet- - the sheet to be finalizedrow- - the row
-
finalizeFirstRow
public void finalizeFirstRow(ExcelWriter writer, org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, int sheetIndex, org.apache.poi.ss.usermodel.Row row, org.apache.poi.ss.usermodel.Cell cell, int cellIndex) Finalized the first row of the given sheet by setting the autosize of the column.- Parameters:
writer- - writer the calling writerworkbook- - the workbook to be finalizedsheet- - the sheet to be finalizedsheetIndex- - the index of the sheetrow- - the first rowcell- - the cell in first row to finalizecellIndex- - the cell index- Since:
- 4.1
- See Also:
-
getAlign
public org.apache.poi.ss.usermodel.HorizontalAlignment getAlign(ExcelWriter writer, int row, int column, Object value) Returns the alignment to be used. This implementation returns null.- Parameters:
writer- writer that requires the informationrow- row indexcolumn- column indexvalue- value in cell- Returns:
- alignment index for Excel or null if no alignment is required
-
getFormat
Returns the display format. The format of the value. This implementation sets format for dates and numbers.- Parameters:
writer- writer that requires the informationrow- row indexcolumn- column indexvalue- value in cell- Returns:
- format index for Excel or null if no formatting is required
- See Also:
-
getDateFormat
Returns the ID of the format or creates a new one if required.- Parameters:
writer- writer that provides the workbookformat- format to be used- Returns:
- ID of format
-
getCurrencyFormat
Returns the ID of the format or creates a new one if required.- Parameters:
writer- writer that provides the workbookformat- format to be used- Returns:
- ID of format
-
getIntegerFormat
Returns the ID of the format or creates a new one if required.- Parameters:
writer- writer that provides the workbookformat- format to be used- Returns:
- ID of format
-
getRealFormat
Returns the ID of the format or creates a new one if required.- Parameters:
writer- writer that provides the workbookformat- format to be used- Returns:
- ID of format
-
getDateTimeFormat
Returns the default format for dates. This implementation returnsDEFAULT_DATETIME_FORMAT.- Parameters:
row- the row that this format will be used forcolumn- the column that this format will be used forvalue- the value that this format will be used for- Returns:
- date formats
- See Also:
-
getCurrencyFormat
Returns the default format for currencies. This implementation returns "#,##0.00\\ _" plus the symbol of the currency.- Parameters:
row- the row that this format will be used forcolumn- the column that this format will be used forvalue- the value that this format will be used for- Returns:
- currency formats
-
getDateFormat
Returns the default format for dates. This implementation returnsDEFAULT_DATE_FORMAT.- Parameters:
row- the row that this format will be used forcolumn- the column that this format will be used forvalue- the value that this format will be used for- Returns:
- date formats
- See Also:
-
getIntegerFormat
Returns the default format for shorts, integers and longs. This implementation returnsDEFAULT_INTEGER_FORMAT.- Parameters:
row- the row that this format will be used forcolumn- the column that this format will be used forvalue- the value that this format will be used for- Returns:
- date formats
- See Also:
-
getRealFormat
Returns the default format for real and float numbers. This implementation returnsDEFAULT_REAL_FORMAT.- Parameters:
row- the row that this format will be used forcolumn- the column that this format will be used forvalue- the value that this format will be used for- Returns:
- date formats
- See Also:
-
getBackgroundColor
Returns the background color for the specified cell. This implementation returns null (default background color). You can use IndexedColors.LIGHT_GREEN.getIndex() to return the color. Notice that background colors is somehow misleading as foreground and background color build up a cell's background (behind the text itself).- Parameters:
writer- writer that requires the informationrow- row indexcolumn- column indexvalue- value in cell- Returns:
- color index for Excel or null
-
getFillPattern
public org.apache.poi.ss.usermodel.FillPatternType getFillPattern(ExcelWriter writer, int row, int column, Object value) Returns the fill pattern for the background. This implementation returns CellStyle.SOLID_FOREGROUND if a foreground color was set.- Parameters:
writer- writer that requires the informationrow- row indexcolumn- column indexvalue- value in cell- Returns:
- the fill pattern or null if default pattern shall be applied.
-
getForegroundColor
Returns the foreground color for the specified cell. This implementation returns null (default foreground color). You can use IndexedColors.LIGHT_GREEN.getIndex() to return the color. This is the correct implementation if you want to set the cell's color.- Parameters:
writer- writer that requires the informationrow- row indexcolumn- column indexvalue- value in cell- Returns:
- color index for Excel or null
-
getFont
public org.apache.poi.ss.usermodel.Font getFont(ExcelWriter writer, int row, int column, Object value) Returns the correct font for the cell. This implementation will return bold font for the first row if required and hyperlink fonts for hyperlink cells. An overwritten implementation could look like this:font = writer.getWorkbook().createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setColor(IndexedColors.BLACK.getIndex()); font.setFontHeightInPoints((short)10); font.setFontName("Arial");- Parameters:
writer- writer that requires the informationrow- row indexcolumn- column indexvalue- value in cell- Returns:
- correct font or null if no special font is required.
- See Also:
-
getDefaultFontSize
public short getDefaultFontSize()Returns the font size to be used.- Returns:
- the default font size
- See Also:
-
getDefaultFontColor
public short getDefaultFontColor()Returns the font color to be used in non-hyperlink cells.- Returns:
- the font color
- See Also:
-
getDefaultHyperlinkColor
public short getDefaultHyperlinkColor()Returns the font color to be used for hyperlinks.- Returns:
- the hyperlink color
-
getDefaultFontName
Returns the font name to be used.- Returns:
- the font name
-
isEmphasizeFirstRow
public boolean isEmphasizeFirstRow()Returns the setting of emphasizing the header row.- Returns:
- the emphasizeFirstRow
-
setEmphasizeFirstRow
public void setEmphasizeFirstRow(boolean emphasizeFirstRow) Sets the property of emphasizing header rows.- Parameters:
emphasizeFirstRow- the emphasizeFirstRow to set
-
getBoldFont
public org.apache.poi.ss.usermodel.Font getBoldFont(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column, Object value) Returns the bold font used for header rows. This implementation returns the font returned bygetDefaultBoldFont(Workbook).- Parameters:
row- the row that this font will be used forcolumn- the column that this font will be used forvalue- the value that this font will be used forworkbook- the workbook for creation- Returns:
- the bold Font for this cell
- See Also:
-
getDefaultBoldFont
public org.apache.poi.ss.usermodel.Font getDefaultBoldFont(org.apache.poi.ss.usermodel.Workbook workbook) Returns the default bold font. This implementation returns the font defined bygetDefaultFontName(),getDefaultFontSize()andgetDefaultFontColor()with bold weight.- Parameters:
workbook- workbook object for creation- Returns:
- default bold font
-
getPlainFont
public org.apache.poi.ss.usermodel.Font getPlainFont(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column, Object value) Returns the default font used for normal cells. This implementation returns the font defined by ,getDefaultPlainFont(Workbook).- Parameters:
row- the row that this font will be used forcolumn- the column that this font will be used forvalue- the value that this font will be used forworkbook- the workbook for creation- Returns:
- the font for this cell
-
getDefaultPlainFont
public org.apache.poi.ss.usermodel.Font getDefaultPlainFont(org.apache.poi.ss.usermodel.Workbook workbook) Returns the default font used for normal cells. This implementation returns the font defined bygetDefaultFontName(),getDefaultFontSize()andgetDefaultFontColor()with normal weight.- Parameters:
workbook- the workbook for creation- Returns:
- the default plain font
-
getHyperlinkFont
public org.apache.poi.ss.usermodel.Font getHyperlinkFont(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column, Object value) Returns the font to be used for hyperlinks. This implementation returns the font defined bygetDefaultHyperlinkFont(Workbook).- Parameters:
workbook- the workbook for creationrow- the row that this font will be used forcolumn- the column that this font will be used forvalue- the value that this font will be used for- Returns:
- font object
-
getDefaultHyperlinkFont
public org.apache.poi.ss.usermodel.Font getDefaultHyperlinkFont(org.apache.poi.ss.usermodel.Workbook workbook) Returns the font to be used for hyperlinks. This implementation returns the font defined bygetDefaultFontName(),getDefaultFontSize()andgetDefaultHyperlinkColor(). This font will be underlined with normal weight.- Parameters:
workbook- the workbook for creation- Returns:
- font object for hyperlinks
-
getTopBorderColor
-
getLeftBorderColor
-
getRightBorderColor
-
getBottomBorderColor
-
getTopBorderThickness
public org.apache.poi.ss.usermodel.BorderStyle getTopBorderThickness(ExcelWriter writer, int row, int column, Object value) -
getLeftBorderThickness
public org.apache.poi.ss.usermodel.BorderStyle getLeftBorderThickness(ExcelWriter writer, int row, int column, Object value) -
getRightBorderThickness
public org.apache.poi.ss.usermodel.BorderStyle getRightBorderThickness(ExcelWriter writer, int row, int column, Object value) -
getBottomBorderThickness
public org.apache.poi.ss.usermodel.BorderStyle getBottomBorderThickness(ExcelWriter writer, int row, int column, Object value) -
setDefaultBorderColor
- Parameters:
borderColor- the borderColor to set
-
setDefaultBorderThickness
public void setDefaultBorderThickness(org.apache.poi.ss.usermodel.BorderStyle borderThickness) - Parameters:
borderThickness- the borderThickness to set
-
isTextWrap
-
finalize(ExcelWriter)instead.