Class SpreadsheetReader


  • public class SpreadsheetReader
    extends Object
    Very simple utility to help read a POI sheet within an Excel (.xlsx) file. Does not work with .xls files. The getValue and getStringValue methods return the TRIMMED content of the cell, and return an empty String if the cell doesn't exist or is empty. The getNumericValue method returns 0 if the cell doesn't exist or is empty. It throws an exception if the value cannot be parsed to a number. The getDateValue method returns null if the cell doesn't exist or is empty, and throws an exception if the value is not a date.
    • Constructor Detail

      • SpreadsheetReader

        public SpreadsheetReader​(org.apache.poi.ss.usermodel.Sheet sheet)
    • Method Detail

      • getColumnIndex

        public static int getColumnIndex​(String cellId)
        Gets the 0-based index (used by POI) of the column given a cellId in Excel notation, which must be one or more letters (case-insensitive) followed by an integer. i.e. B7 (which returns 1) or AR1677 (which returns 43). Throws an IllegalArgumentException if the cellId is malformed
      • getRowIndex

        public static int getRowIndex​(String cellId)
        Gets the 0-based index (used by POI) of the row given a cellId in Excel notation, which must be one or more letters (case-insensitive) followed by an integer. i.e. B7 (which returns 6) or BR1677 (which returns 1676) Throws an IllegalArgumentException if the cellId is malformed
      • getCellAddress

        public static String getCellAddress​(int col,
                                            int row)
      • getPoiSheet

        public org.apache.poi.ss.usermodel.Sheet getPoiSheet()
      • getNumRows

        public int getNumRows()
      • getNumCols

        public int getNumCols​(int rowNum)
      • getValue

        public String getValue​(String cellId)
        Attempts to convert all values to a string. Returns the trimmed content of the cell, or an empty String if the cell doesn't exist or is empty.
      • getValue

        public String getValue​(int colIndex,
                               int rowIndex)
        Attempts to convert all values to a string. Returns the trimmed content of the cell, or an empty String if the cell doesn't exist or is empty.
      • getValue

        public String getValue​(org.apache.poi.ss.usermodel.Cell cell)
        Attempts to convert all values to a string. Returns the trimmed content of the cell, or an empty String if the cell doesn't exist or is empty.
      • getStringValue

        public String getStringValue​(String cellId)
        Returns the trimmed content of the cell as a String, or an empty String if the cell doesn't exist or is empty.
      • getStringValue

        public String getStringValue​(int columnIndex,
                                     int rowIndex)
        Returns the trimmed content of the cell as a String, or an empty String if the cell doesn't exist or is empty.
      • getStringValue

        public String getStringValue​(org.apache.poi.ss.usermodel.Cell cell)
        Returns the trimmed content of the cell as a String, or an empty String if the cell doesn't exist or is empty.
      • getNumericValue

        public Double getNumericValue​(String cellId)
        Returns the numeric content of the cell, or 0 if the cell doesn't exist or is empty.
      • getNumericValue

        public Double getNumericValue​(int columnIndex,
                                      int rowIndex)
        Returns the numeric content of the cell, or 0 if the cell doesn't exist or is empty.
      • getNumericValue

        public Double getNumericValue​(org.apache.poi.ss.usermodel.Cell cell)
      • getDateValue

        public Date getDateValue​(int columnIndex,
                                 int rowIndex)
        Returns the Date content of the cell, or null if the cell doesn't exist or is empty.
      • getDateValue

        public Date getDateValue​(String cellId)
      • getDateValue

        public Date getDateValue​(org.apache.poi.ss.usermodel.Cell cell)
      • getCell

        public org.apache.poi.ss.usermodel.Cell getCell​(String cellId)
      • getCell

        public org.apache.poi.ss.usermodel.Cell getCell​(int columnIndex,
                                                        int rowIndex)
      • getCellType

        public org.apache.poi.ss.usermodel.CellType getCellType​(int col,
                                                                int row)
      • isString

        public boolean isString​(int col,
                                int row)
      • isNumeric

        public boolean isNumeric​(int col,
                                 int row)
      • readDownUntilBlank

        public List<String> readDownUntilBlank​(String startingCell)
      • readDown

        public String[] readDown​(String startingCell,
                                 int num)
      • readDownNumeric

        public double[] readDownNumeric​(String startingCell,
                                        int num)
      • readAcrossUntilBlank

        public List<String> readAcrossUntilBlank​(String startingCell)
      • readAcross

        public String[] readAcross​(String startingCell,
                                   int num)
      • readAcrossNumeric

        public double[] readAcrossNumeric​(String startingCell,
                                          int num)
      • readSheet

        public String[][] readSheet()
      • getSheetName

        public String getSheetName()
      • rowHasData

        public boolean rowHasData​(int rowIndex)