Class ExcelWriter


  • public class ExcelWriter
    extends java.lang.Object
    Utility class for creating and writing to Excel files, using Apache POI.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ExcelWriter.DataCell  
    • Constructor Summary

      Constructors 
      Constructor Description
      ExcelWriter​(int batch)
      Construct a new Excel file writer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addDataCell​(org.apache.poi.ss.usermodel.Row row, int index, boolean value)
      Add a data cell with a boolean value.
      void addDataCell​(org.apache.poi.ss.usermodel.Row row, int index, double value)
      Add a data cell with a numeric value.
      void addDataCell​(org.apache.poi.ss.usermodel.Row row, int index, long value)
      Add a data cell with a numeric value.
      void addDataCell​(org.apache.poi.ss.usermodel.Row row, int index, java.lang.String value)
      Add a data cell with a string value.
      void addHeader​(org.apache.poi.ss.usermodel.Sheet worksheet, java.util.Map<java.lang.String,​java.lang.String> headers)
      Create a header row for the worksheet.
      void addHeaderCell​(org.apache.poi.ss.usermodel.Row header, int index, java.lang.String name, java.lang.String description)
      Add a header cell to the worksheet.
      void appendRow​(org.apache.poi.ss.usermodel.Sheet worksheet, java.util.List<ExcelWriter.DataCell> data)
      Add a row of data to the end of a worksheet.
      java.io.ByteArrayOutputStream asByteArray()
      Flush out the contents of the workbook to a ByteArrayOutputStream.
      void create​(java.lang.String fileLocation)
      Flush out the contents of the workbook to a file, creating the XLSX file.
      org.apache.poi.ss.usermodel.Sheet createSheet​(java.lang.String name)
      Create a new worksheet within the workbook.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExcelWriter

        public ExcelWriter​(int batch)
                    throws java.io.IOException
        Construct a new Excel file writer.
        Parameters:
        batch - how many records to write in-memory before flushing to disk
        Throws:
        java.io.IOException - on any errors creating or accessing the file
    • Method Detail

      • createSheet

        public org.apache.poi.ss.usermodel.Sheet createSheet​(java.lang.String name)
        Create a new worksheet within the workbook.
        Parameters:
        name - of the sheet to create
        Returns:
        the worksheet
      • create

        public void create​(java.lang.String fileLocation)
                    throws java.io.IOException
        Flush out the contents of the workbook to a file, creating the XLSX file.
        Parameters:
        fileLocation - location of the Excel file to create
        Throws:
        java.io.IOException - on any errors creating or accessing the file
      • asByteArray

        public java.io.ByteArrayOutputStream asByteArray()
                                                  throws java.io.IOException
        Flush out the contents of the workbook to a ByteArrayOutputStream.
        Returns:
        the contents of the workbook as an output stream of bytes
        Throws:
        java.io.IOException - on any errors creating or writing to the byte array output stream
      • addHeader

        public void addHeader​(org.apache.poi.ss.usermodel.Sheet worksheet,
                              java.util.Map<java.lang.String,​java.lang.String> headers)
        Create a header row for the worksheet.
        Parameters:
        worksheet - in which to create the header row
        headers - ordered map of header names and descriptions
      • appendRow

        public void appendRow​(org.apache.poi.ss.usermodel.Sheet worksheet,
                              java.util.List<ExcelWriter.DataCell> data)
        Add a row of data to the end of a worksheet.
        Parameters:
        worksheet - the worksheet into which to add the row
        data - the row of data to add
      • addHeaderCell

        public void addHeaderCell​(org.apache.poi.ss.usermodel.Row header,
                                  int index,
                                  java.lang.String name,
                                  java.lang.String description)
        Add a header cell to the worksheet.
        Parameters:
        header - row for the header
        index - location of the cell (column index)
        name - of the header column
        description - of the header column (will be put into a comment)
      • addDataCell

        public void addDataCell​(org.apache.poi.ss.usermodel.Row row,
                                int index,
                                java.lang.String value)
        Add a data cell with a string value.
        Parameters:
        row - in which to add the cell
        index - column for the cell
        value - to set for the cell
      • addDataCell

        public void addDataCell​(org.apache.poi.ss.usermodel.Row row,
                                int index,
                                boolean value)
        Add a data cell with a boolean value.
        Parameters:
        row - in which to add the cell
        index - column for the cell
        value - to set for the cell
      • addDataCell

        public void addDataCell​(org.apache.poi.ss.usermodel.Row row,
                                int index,
                                long value)
        Add a data cell with a numeric value.
        Parameters:
        row - in which to add the cell
        index - column for the cell
        value - to set for the cell
      • addDataCell

        public void addDataCell​(org.apache.poi.ss.usermodel.Row row,
                                int index,
                                double value)
        Add a data cell with a numeric value.
        Parameters:
        row - in which to add the cell
        index - column for the cell
        value - to set for the cell