Package csv.impl
Class DefaultExcelFormatter
java.lang.Object
csv.impl.DefaultExcelFormatter
- All Implemented Interfaces:
ExcelFormatter
public class DefaultExcelFormatter extends java.lang.Object implements 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 Classes Modifier and Type Class Description protected static classDefaultExcelFormatter.StyleDescriptionDescribes a style for a cell. -
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_DATE_FORMATdate format "dd.mm.yyyy hh:mm"static shortDEFAULT_FONT_COLORColor Blackstatic java.lang.StringDEFAULT_FONT_NAMEArial font namestatic shortDEFAULT_FONT_SIZEFont Size 10static java.lang.StringDEFAULT_INTEGER_FORMATinteger format "0"static java.lang.StringDEFAULT_REAL_FORMATreal format "0.00"static shortHYPERLINK_FONT_COLORColor Blue -
Constructor Summary
Constructors Constructor Description DefaultExcelFormatter()Default constructor.DefaultExcelFormatter(boolean emphasizeFirstRow)Constructor for defining the emphasizing of header rows.DefaultExcelFormatter(boolean emphasizeFirstRow, java.lang.String defaultFontName, java.lang.Short defaultFontSize, java.lang.Short defaultFontColor, java.lang.Short defaultHyperlinkColor)Constructor for defining the various properties. -
Method Summary
Modifier and Type Method Description voidfinalize(ExcelWriter writer, int rowCount, int columnCount)Finalizes the workbook.org.apache.poi.ss.usermodel.HorizontalAlignmentgetAlign(ExcelWriter writer, int row, int column, java.lang.Object value)Returns the alignment to be used.java.lang.ShortgetBackgroundColor(ExcelWriter writer, int row, int column, java.lang.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, java.lang.Object value)Returns the bold font used for header rows.java.lang.ShortgetBottomBorderColor(ExcelWriter writer, int row, int column, java.lang.Object value)org.apache.poi.ss.usermodel.BorderStylegetBottomBorderThickness(ExcelWriter writer, int row, int column, java.lang.Object value)java.lang.StringgetDateFormat(int row, int column, java.lang.Object value)Returns the default format for dates.protected java.lang.ShortgetDateFormat(ExcelWriter writer, java.lang.String format)Returns the ID of the format or creates a new one if required.org.apache.poi.ss.usermodel.FontgetDefaultBoldFont(org.apache.poi.ss.usermodel.Workbook workbook)Returns the default bold font.shortgetDefaultFontColor()Returns the font color to be used in non-hyperlink cells.java.lang.StringgetDefaultFontName()Returns the font name to be used.shortgetDefaultFontSize()Returns the font size to be used.shortgetDefaultHyperlinkColor()Returns 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, java.lang.Object value)Returns the fill pattern for the background.org.apache.poi.ss.usermodel.FontgetFont(ExcelWriter writer, int row, int column, java.lang.Object value)Returns the correct font for the cell.java.lang.ShortgetForegroundColor(ExcelWriter writer, int row, int column, java.lang.Object value)Returns the foreground color for the specified cell.java.lang.ShortgetFormat(ExcelWriter writer, int row, int column, java.lang.Object value)Returns the display format.org.apache.poi.ss.usermodel.HyperlinkgetHyperlink(ExcelWriter writer, int row, int column, java.lang.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, java.lang.Object value)Returns the font to be used for hyperlinks.java.lang.StringgetIntegerFormat(int row, int column, java.lang.Object value)Returns the default format for shorts, integers and longs.protected java.lang.ShortgetIntegerFormat(ExcelWriter writer, java.lang.String format)Returns the ID of the format or creates a new one if required.java.lang.ShortgetLeftBorderColor(ExcelWriter writer, int row, int column, java.lang.Object value)org.apache.poi.ss.usermodel.BorderStylegetLeftBorderThickness(ExcelWriter writer, int row, int column, java.lang.Object value)org.apache.poi.ss.usermodel.FontgetPlainFont(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column, java.lang.Object value)Returns the default font used for normal cells.java.lang.StringgetRealFormat(int row, int column, java.lang.Object value)Returns the default format for real and float numbers.protected java.lang.ShortgetRealFormat(ExcelWriter writer, java.lang.String format)Returns the ID of the format or creates a new one if required.java.lang.ShortgetRightBorderColor(ExcelWriter writer, int row, int column, java.lang.Object value)org.apache.poi.ss.usermodel.BorderStylegetRightBorderThickness(ExcelWriter writer, int row, int column, java.lang.Object value)java.lang.ShortgetTopBorderColor(ExcelWriter writer, int row, int column, java.lang.Object value)org.apache.poi.ss.usermodel.BorderStylegetTopBorderThickness(ExcelWriter writer, int row, int column, java.lang.Object value)voidinit()booleanisEmphasizeFirstRow()Returns the setting of emphasizing the header row.booleanisTextWrap(ExcelWriter writer, int row, int column, java.lang.Object value)voidsetDefaultBorderColor(java.lang.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, java.lang.Object value)Sets the cell style.
-
Field Details
-
DEFAULT_FONT_NAME
public static final java.lang.String DEFAULT_FONT_NAMEArial font name- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
DEFAULT_DATE_FORMAT
public static final java.lang.String DEFAULT_DATE_FORMATdate format "dd.mm.yyyy hh:mm"- See Also:
- Constant Field Values
-
DEFAULT_INTEGER_FORMAT
public static final java.lang.String DEFAULT_INTEGER_FORMATinteger format "0"- See Also:
- Constant Field Values
-
DEFAULT_REAL_FORMAT
public static final java.lang.String DEFAULT_REAL_FORMATreal format "0.00"- See Also:
- Constant Field Values
-
-
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:
getFont(ExcelWriter, int, int, Object)
-
DefaultExcelFormatter
public DefaultExcelFormatter(boolean emphasizeFirstRow, java.lang.String defaultFontName, java.lang.Short defaultFontSize, java.lang.Short defaultFontColor, java.lang.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:
getFont(ExcelWriter, int, int, Object)
-
-
Method Details
-
init
public void init() -
setStyle
public void setStyle(ExcelWriter writer, org.apache.poi.ss.usermodel.Cell cell, java.lang.Object value)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, java.lang.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
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
-
getAlign
public org.apache.poi.ss.usermodel.HorizontalAlignment getAlign(ExcelWriter writer, int row, int column, java.lang.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(int, int, Object),getIntegerFormat(int, int, Object),getRealFormat(int, int, Object)
-
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
-
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
-
getDateFormat
public java.lang.String getDateFormat(int row, int column, java.lang.Object value)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:
DEFAULT_DATE_FORMAT
-
getIntegerFormat
public java.lang.String getIntegerFormat(int row, int column, java.lang.Object value)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:
DEFAULT_INTEGER_FORMAT
-
getRealFormat
public java.lang.String getRealFormat(int row, int column, java.lang.Object value)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:
DEFAULT_REAL_FORMAT
-
getBackgroundColor
public java.lang.Short getBackgroundColor(ExcelWriter writer, int row, int column, java.lang.Object value)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, java.lang.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
public java.lang.Short getForegroundColor(ExcelWriter writer, int row, int column, java.lang.Object value)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, java.lang.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:
getBoldFont(Workbook, int, int, Object),getPlainFont(Workbook, int, int, Object),getHyperlinkFont(Workbook, int, int, Object)
-
getDefaultFontSize
public short getDefaultFontSize()Returns the font size to be used.- Returns:
- the default font size
- See Also:
DEFAULT_FONT_SIZE
-
getDefaultFontColor
public short getDefaultFontColor()Returns the font color to be used in non-hyperlink cells.- Returns:
- the font color
- See Also:
DEFAULT_FONT_COLOR
-
getDefaultHyperlinkColor
public short getDefaultHyperlinkColor()Returns the font color to be used for hyperlinks.- Returns:
- the hyperlink color
-
getDefaultFontName
public java.lang.String 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, java.lang.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(Workbook)
-
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, java.lang.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, java.lang.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
public java.lang.Short getTopBorderColor(ExcelWriter writer, int row, int column, java.lang.Object value) -
getLeftBorderColor
public java.lang.Short getLeftBorderColor(ExcelWriter writer, int row, int column, java.lang.Object value) -
getRightBorderColor
public java.lang.Short getRightBorderColor(ExcelWriter writer, int row, int column, java.lang.Object value) -
getBottomBorderColor
public java.lang.Short getBottomBorderColor(ExcelWriter writer, int row, int column, java.lang.Object value) -
getTopBorderThickness
public org.apache.poi.ss.usermodel.BorderStyle getTopBorderThickness(ExcelWriter writer, int row, int column, java.lang.Object value) -
getLeftBorderThickness
public org.apache.poi.ss.usermodel.BorderStyle getLeftBorderThickness(ExcelWriter writer, int row, int column, java.lang.Object value) -
getRightBorderThickness
public org.apache.poi.ss.usermodel.BorderStyle getRightBorderThickness(ExcelWriter writer, int row, int column, java.lang.Object value) -
getBottomBorderThickness
public org.apache.poi.ss.usermodel.BorderStyle getBottomBorderThickness(ExcelWriter writer, int row, int column, java.lang.Object value) -
setDefaultBorderColor
public void setDefaultBorderColor(java.lang.Short borderColor)- Parameters:
borderColor- the borderColor to set
-
setDefaultBorderThickness
public void setDefaultBorderThickness(org.apache.poi.ss.usermodel.BorderStyle borderThickness)- Parameters:
borderThickness- the borderThickness to set
-
isTextWrap
-