Class XlsxTableWriter

All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class XlsxTableWriter extends TableWriter
This is a simple wrapper for the TableWriter class to make it create an Excel workbook instead of a CSV file. There are some things to consider: The tablePrintHeaders(Object...) and TableWriter.tablePrintData(Object...) (including overloaded variants) methods can be used to write a single row at a time. The various print() and write() methods write to a single cell and will advance one column at a time. The println() variants ends the current row and starts over on the first column. Writing data to Excel is by default handled by the AutoFormatter implementation which will try to infer the correct Excel data type and format string from the class and value of the data. It is possible to assign a different formatter implementation to a given column by calling TableWriter.setColumnFormatter(int, Formatter), but note that the formatter is only used by this class if it also implements the ExcelFormatter interface. Call saveTo(OutputStream) to write the Excel workbook to an output location.
Since:
3.15
  • Field Details

    • name

      private final String name
    • workbook

      private final org.apache.poi.ss.usermodel.Workbook workbook
    • sheet

      private final org.apache.poi.ss.usermodel.Sheet sheet
    • externalWorkbook

      private final boolean externalWorkbook
    • styleCreator

      private final CellStyleCreator styleCreator
    • autoFormatter

      private final AutoFormatter autoFormatter
    • rowNo

      private int rowNo
    • currentRow

      private org.apache.poi.ss.usermodel.Row currentRow
    • colNo

      private int colNo
  • Constructor Details

    • XlsxTableWriter

      public XlsxTableWriter(String name)
      Create a new workbook with a single worksheet.
      Parameters:
      name - The name of the worksheet
    • XlsxTableWriter

      public XlsxTableWriter(org.apache.poi.ss.usermodel.Workbook workbook, String name)
      Add a sheet to the given Excel workbook.
      Parameters:
      name - The name of the worksheet
  • Method Details

    • getWorkbook

      public org.apache.poi.ss.usermodel.Workbook getWorkbook()
      Get the workbook.
    • getSheet

      public org.apache.poi.ss.usermodel.Sheet getSheet()
      Get the current sheet in the workbook.
    • getCurrentRow

      public org.apache.poi.ss.usermodel.Row getCurrentRow()
      Get row that was the last one to be printed.
    • getAutoFormatter

      public AutoFormatter getAutoFormatter()
      Get the auto-formatter instance to use for columns that has no specified formatter via TableWriter.setColumnFormatter(int, Formatter).
    • getCellStyleCreator

      public CellStyleCreator getCellStyleCreator()
      Get the style creator that can be used for custom styling of cells.
    • tablePrintHeaders

      public void tablePrintHeaders(Object... headers)
      Description copied from class: TableWriter
      Print a header line to the parent writer. The only difference between this method and the TableWriter.tablePrintData(Object...) method is that no column formatters are used (since they operate on data and the headers are typically strings).
      Overrides:
      tablePrintHeaders in class TableWriter
    • tablePrintData

      public void tablePrintData(EncoderDecoder encoder, Map<Integer,Formatter<?>> formatters, Object... data)
      Description copied from class: TableWriter
      Print a data line to the parent writer. Each value is separated by the data separator string ('tab' by default). Null values are replaced with the null value string (empty string by default). Non-null objects are converted to strings values by calling either their toString() method or using the formatter assigned for each column. If an encoder has been specified, the value is finally encoded with EncoderDecoder.encode(String).
      Overrides:
      tablePrintData in class TableWriter
      Parameters:
      encoder - The encoder to use, or null to not use any encoder
      formatters - Column formatters to use (null to always use the toString() method)
      data - The values to write
    • close

      public void close()
      Closes the workbook unless it was supplied in the constructor from an external source.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class PrintWriter
    • saveTo

      public void saveTo(OutputStream out) throws IOException
      Save the workbook to the given stream.
      Throws:
      IOException
    • write

      public void write(int c)
      Overrides:
      write in class PrintWriter
    • write

      public void write(char[] buf, int off, int len)
      Overrides:
      write in class PrintWriter
    • write

      public void write(char[] buf)
      Overrides:
      write in class PrintWriter
    • write

      public void write(String s, int off, int len)
      Overrides:
      write in class PrintWriter
    • write

      public void write(String s)
      Overrides:
      write in class PrintWriter
    • print

      public void print(boolean b)
      Overrides:
      print in class PrintWriter
    • print

      public void print(char c)
      Overrides:
      print in class PrintWriter
    • print

      public void print(int i)
      Overrides:
      print in class PrintWriter
    • print

      public void print(long l)
      Overrides:
      print in class PrintWriter
    • print

      public void print(float f)
      Overrides:
      print in class PrintWriter
    • print

      public void print(double d)
      Overrides:
      print in class PrintWriter
    • print

      public void print(char[] s)
      Overrides:
      print in class PrintWriter
    • print

      public void print(String s)
      Overrides:
      print in class PrintWriter
    • print

      public void print(Object obj)
      Overrides:
      print in class PrintWriter
    • println

      public void println()
      Overrides:
      println in class PrintWriter
    • println

      public void println(boolean b)
      Overrides:
      println in class PrintWriter
    • println

      public void println(char c)
      Overrides:
      println in class PrintWriter
    • println

      public void println(int i)
      Overrides:
      println in class PrintWriter
    • println

      public void println(long l)
      Overrides:
      println in class PrintWriter
    • println

      public void println(float f)
      Overrides:
      println in class PrintWriter
    • println

      public void println(double d)
      Overrides:
      println in class PrintWriter
    • println

      public void println(char[] s)
      Overrides:
      println in class PrintWriter
    • println

      public void println(String s)
      Overrides:
      println in class PrintWriter
    • println

      public void println(Object o)
      Overrides:
      println in class PrintWriter
    • writeData

      private void writeData(Object data, boolean endOfRow)
      Write the data to the next cell. If colNo=0 a new row is created, otherwise the next cell in the current row is used. If 'endOfRow' the current row is ended (by setting colNo to 0) so that a new row is created the next time this method is called.