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 class  DefaultExcelFormatter.StyleDescription
    Describes a style for a cell.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String DEFAULT_DATE_FORMAT
    date format "dd.mm.yyyy hh:mm"
    static short DEFAULT_FONT_COLOR
    Color Black
    static java.lang.String DEFAULT_FONT_NAME
    Arial font name
    static short DEFAULT_FONT_SIZE
    Font Size 10
    static java.lang.String DEFAULT_INTEGER_FORMAT
    integer format "0"
    static java.lang.String DEFAULT_REAL_FORMAT
    real format "0.00"
    static short HYPERLINK_FONT_COLOR
    Color 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
    void finalize​(ExcelWriter writer, int rowCount, int columnCount)
    Finalizes the workbook.
    org.apache.poi.ss.usermodel.HorizontalAlignment getAlign​(ExcelWriter writer, int row, int column, java.lang.Object value)
    Returns the alignment to be used.
    java.lang.Short getBackgroundColor​(ExcelWriter writer, int row, int column, java.lang.Object value)
    Returns the background color for the specified cell.
    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.
    java.lang.Short getBottomBorderColor​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    org.apache.poi.ss.usermodel.BorderStyle getBottomBorderThickness​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    java.lang.String getDateFormat​(int row, int column, java.lang.Object value)
    Returns the default format for dates.
    protected java.lang.Short getDateFormat​(ExcelWriter writer, java.lang.String format)
    Returns the ID of the format or creates a new one if required.
    org.apache.poi.ss.usermodel.Font getDefaultBoldFont​(org.apache.poi.ss.usermodel.Workbook workbook)
    Returns the default bold font.
    short getDefaultFontColor()
    Returns the font color to be used in non-hyperlink cells.
    java.lang.String getDefaultFontName()
    Returns the font name to be used.
    short getDefaultFontSize()
    Returns the font size to be used.
    short getDefaultHyperlinkColor()
    Returns the font color to be used for hyperlinks.
    org.apache.poi.ss.usermodel.Font getDefaultHyperlinkFont​(org.apache.poi.ss.usermodel.Workbook workbook)
    Returns the font to be used for hyperlinks.
    org.apache.poi.ss.usermodel.Font getDefaultPlainFont​(org.apache.poi.ss.usermodel.Workbook workbook)
    Returns the default font used for normal cells.
    org.apache.poi.ss.usermodel.FillPatternType getFillPattern​(ExcelWriter writer, int row, int column, java.lang.Object value)
    Returns the fill pattern for the background.
    org.apache.poi.ss.usermodel.Font getFont​(ExcelWriter writer, int row, int column, java.lang.Object value)
    Returns the correct font for the cell.
    java.lang.Short getForegroundColor​(ExcelWriter writer, int row, int column, java.lang.Object value)
    Returns the foreground color for the specified cell.
    java.lang.Short getFormat​(ExcelWriter writer, int row, int column, java.lang.Object value)
    Returns the display format.
    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.
    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.
    java.lang.String getIntegerFormat​(int row, int column, java.lang.Object value)
    Returns the default format for shorts, integers and longs.
    protected java.lang.Short getIntegerFormat​(ExcelWriter writer, java.lang.String format)
    Returns the ID of the format or creates a new one if required.
    java.lang.Short getLeftBorderColor​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    org.apache.poi.ss.usermodel.BorderStyle getLeftBorderThickness​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    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.
    java.lang.String getRealFormat​(int row, int column, java.lang.Object value)
    Returns the default format for real and float numbers.
    protected java.lang.Short getRealFormat​(ExcelWriter writer, java.lang.String format)
    Returns the ID of the format or creates a new one if required.
    java.lang.Short getRightBorderColor​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    org.apache.poi.ss.usermodel.BorderStyle getRightBorderThickness​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    java.lang.Short getTopBorderColor​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    org.apache.poi.ss.usermodel.BorderStyle getTopBorderThickness​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    void init()  
    boolean isEmphasizeFirstRow()
    Returns the setting of emphasizing the header row.
    boolean isTextWrap​(ExcelWriter writer, int row, int column, java.lang.Object value)  
    void setDefaultBorderColor​(java.lang.Short borderColor)  
    void setDefaultBorderThickness​(org.apache.poi.ss.usermodel.BorderStyle borderThickness)  
    void setEmphasizeFirstRow​(boolean emphasizeFirstRow)
    Sets the property of emphasizing header rows.
    void setStyle​(ExcelWriter writer, org.apache.poi.ss.usermodel.Cell cell, java.lang.Object value)
    Sets the cell style.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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 font
      defaultFontName - font name of default font
      defaultFontSize - font size to be used
      defaultFontColor - color to be used for font
      defaultHyperlinkColor - 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:
      setStyle in interface ExcelFormatter
      Parameters:
      writer - writer that requires the information
      cell - cell to be formatted
      value - 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 in getFont(ExcelWriter, int, int, Object) when you return a hyperlink here.
      Parameters:
      writer - the calling writer
      row - row index
      column - column index
      value - value in cell
      Returns:
      hyperlink object for the cell
    • finalize

      public void finalize​(ExcelWriter writer, int rowCount, int columnCount)
      Finalizes the workbook. This method is called immediately before the ExcelWriter writes the complete workbook to the underlying output stream. This implementation just sets all columns to auto fit.
      Specified by:
      finalize in interface ExcelFormatter
      Parameters:
      writer - the calling writer
      rowCount - the number of rows in the selected sheet
      columnCount - 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 information
      row - row index
      column - column index
      value - value in cell
      Returns:
      alignment index for Excel or null if no alignment is required
    • getFormat

      public java.lang.Short getFormat​(ExcelWriter writer, int row, int column, java.lang.Object value)
      Returns the display format. The format of the value. This implementation sets format for dates and numbers.
      Parameters:
      writer - writer that requires the information
      row - row index
      column - column index
      value - 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

      protected java.lang.Short getDateFormat​(ExcelWriter writer, java.lang.String format)
      Returns the ID of the format or creates a new one if required.
      Parameters:
      writer - writer that provides the workbook
      format - format to be used
      Returns:
      ID of format
    • getIntegerFormat

      protected java.lang.Short getIntegerFormat​(ExcelWriter writer, java.lang.String format)
      Returns the ID of the format or creates a new one if required.
      Parameters:
      writer - writer that provides the workbook
      format - format to be used
      Returns:
      ID of format
    • getRealFormat

      protected java.lang.Short getRealFormat​(ExcelWriter writer, java.lang.String format)
      Returns the ID of the format or creates a new one if required.
      Parameters:
      writer - writer that provides the workbook
      format - 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 returns DEFAULT_DATE_FORMAT.
      Parameters:
      row - the row that this format will be used for
      column - the column that this format will be used for
      value - 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 returns DEFAULT_INTEGER_FORMAT.
      Parameters:
      row - the row that this format will be used for
      column - the column that this format will be used for
      value - 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 returns DEFAULT_REAL_FORMAT.
      Parameters:
      row - the row that this format will be used for
      column - the column that this format will be used for
      value - 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 information
      row - row index
      column - column index
      value - 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 information
      row - row index
      column - column index
      value - 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 information
      row - row index
      column - column index
      value - 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 information
      row - row index
      column - column index
      value - 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 by getDefaultBoldFont(Workbook).
      Parameters:
      row - the row that this font will be used for
      column - the column that this font will be used for
      value - the value that this font will be used for
      workbook - 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 by getDefaultFontName(), getDefaultFontSize() and getDefaultFontColor() 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 for
      column - the column that this font will be used for
      value - the value that this font will be used for
      workbook - 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 by getDefaultFontName(), getDefaultFontSize() and getDefaultFontColor() 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 by getDefaultHyperlinkFont(Workbook).
      Parameters:
      workbook - the workbook for creation
      row - the row that this font will be used for
      column - the column that this font will be used for
      value - 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 by getDefaultFontName(), getDefaultFontSize() and getDefaultHyperlinkColor(). 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

      public boolean isTextWrap​(ExcelWriter writer, int row, int column, java.lang.Object value)