Package csv.impl

Class DefaultExcelFormatter

java.lang.Object
csv.impl.DefaultExcelFormatter
All Implemented Interfaces:
ExcelFormatter

public class DefaultExcelFormatter extends 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
  • Field Details

    • DEFAULT_FONT_NAME

      public static final String DEFAULT_FONT_NAME
      Arial font name
      See Also:
    • DEFAULT_FONT_COLOR

      public static final short DEFAULT_FONT_COLOR
      Color Black
    • DEFAULT_FONT_SIZE

      public static final short DEFAULT_FONT_SIZE
      Font Size 10
      See Also:
    • DEFAULT_DATE_FORMAT

      public static final String DEFAULT_DATE_FORMAT
      datetime format "dd.mm.yyyy hh:mm"
      See Also:
    • DEFAULT_DATETIME_FORMAT

      public static final String DEFAULT_DATETIME_FORMAT
      datetime format "dd.mm.yyyy hh:mm"
      See Also:
    • DEFAULT_INTEGER_FORMAT

      public static final String DEFAULT_INTEGER_FORMAT
      integer format "0"
      See Also:
    • DEFAULT_REAL_FORMAT

      public static final String 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 font
      autofilter - 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 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:
    • 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 font
      autofilter - whether row 0 shall have autofilter activated
      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:
  • 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

      public void setStyle(ExcelWriter writer, org.apache.poi.ss.usermodel.Cell cell, 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:
    • 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 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

      @Deprecated public void finalize(ExcelWriter writer, int rowCount, int columnCount)
      Deprecated.
      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
    • finalize

      public void finalize(ExcelWriter writer)
      Finalizes the workbook. This method is called immediately before the ExcelWriter writes the complete workbook to the underlying output stream.
      Specified by:
      finalize in interface ExcelFormatter
      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 by finalize(ExcelWriter) and takes care of finalizing the first row in a sheet by calling finalizeFirstRow(ExcelWriter, Workbook, Sheet, int, Row, Cell, int).
      Parameters:
      writer - - writer the calling writer
      workbook - - the workbook to be finalized
      sheet - - the sheet to be finalized
      sheetIndex - - 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 finalized
      sheet - - the sheet to be finalized
      row - - 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 writer
      workbook - - the workbook to be finalized
      sheet - - the sheet to be finalized
      sheetIndex - - the index of the sheet
      row - - the first row
      cell - - the cell in first row to finalize
      cellIndex - - 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 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 Short getFormat(ExcelWriter writer, int row, int column, 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

      protected Short getDateFormat(ExcelWriter writer, 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
    • getCurrencyFormat

      protected Short getCurrencyFormat(ExcelWriter writer, 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 Short getIntegerFormat(ExcelWriter writer, 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 Short getRealFormat(ExcelWriter writer, 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
    • getDateTimeFormat

      public String getDateTimeFormat(int row, int column, Object value)
      Returns the default format for dates. This implementation returns DEFAULT_DATETIME_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:
    • getCurrencyFormat

      public String getCurrencyFormat(int row, int column, Object value)
      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 for
      column - the column that this format will be used for
      value - the value that this format will be used for
      Returns:
      currency formats
    • getDateFormat

      public String getDateFormat(int row, int column, 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:
    • getIntegerFormat

      public String getIntegerFormat(int row, int column, 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:
    • getRealFormat

      public String getRealFormat(int row, int column, 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:
    • getBackgroundColor

      public Short getBackgroundColor(ExcelWriter writer, int row, int column, 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, 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 Short getForegroundColor(ExcelWriter writer, int row, int column, 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, 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:
    • 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

      public 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, 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

      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, 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, 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 Short getTopBorderColor(ExcelWriter writer, int row, int column, Object value)
    • getLeftBorderColor

      public Short getLeftBorderColor(ExcelWriter writer, int row, int column, Object value)
    • getRightBorderColor

      public Short getRightBorderColor(ExcelWriter writer, int row, int column, Object value)
    • getBottomBorderColor

      public Short getBottomBorderColor(ExcelWriter writer, int row, int column, Object value)
    • 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

      public void setDefaultBorderColor(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, Object value)