Class AbstractExcelReader<W extends org.apache.poi.ss.usermodel.Workbook,​T>

java.lang.Object
com.github.javaxcel.in.AbstractExcelReader<W,​T>
All Implemented Interfaces:
ExcelReader<W,​T>
Direct Known Subclasses:
MapReader, ModelReader

public abstract class AbstractExcelReader<W extends org.apache.poi.ss.usermodel.Workbook,​T>
extends Object
implements ExcelReader<W,​T>
  • Field Details

    • dataFormatter

      protected static final org.apache.poi.ss.usermodel.DataFormatter dataFormatter
      Formatter that stringifies the value in a cell with FormulaEvaluator.
      See Also:
      readRow(Row)
    • workbook

      protected final W extends org.apache.poi.ss.usermodel.Workbook workbook
      Apache POI workbook.
      See Also:
      HSSFWorkbook, XSSFWorkbook
    • formulaEvaluator

      protected final org.apache.poi.ss.usermodel.FormulaEvaluator formulaEvaluator
      Evaluator that evaluates the formula in a cell.
      See Also:
      Workbook.getCreationHelper(), CreationHelper.createFormulaEvaluator(), readRow(Row)
    • limit

      protected int limit
      Limitation of reading rows.
      See Also:
      limit(int)
    • numOfModelsRead

      protected int numOfModelsRead
      Total number of models read by AbstractExcelReader.
      See Also:
      limit(int)
  • Constructor Details

  • Method Details

    • limit

      public AbstractExcelReader<W,​T> limit​(int limit)
      Limits the number of models.
      Specified by:
      limit in interface ExcelReader<W extends org.apache.poi.ss.usermodel.Workbook,​T>
      Parameters:
      limit - limit for the number of models
      Returns:
      AbstractExcelReader
    • read

      public final List<T> read()
      Returns a list after this reads the excel file.

      When an empty row is in the middle of body, computation of the number of models by ExcelUtils.getNumOfModels(Sheet) is missed. Models are missing as many empty rows.

      Specified by:
      read in interface ExcelReader<W extends org.apache.poi.ss.usermodel.Workbook,​T>
      Returns:
      list
    • getNumOfModels

      protected final int getNumOfModels​(org.apache.poi.ss.usermodel.Sheet sheet)
    • readSheetAsMaps

      protected final List<Map<String,​Object>> readSheetAsMaps​(org.apache.poi.ss.usermodel.Sheet sheet)
      Gets models read as map from a sheet.
      Parameters:
      sheet - excel sheet
      Returns:
      models read as map
    • readRow

      protected final Map<String,​Object> readRow​(org.apache.poi.ss.usermodel.Row row)
      Converts a row to a imitated model.

      Reads rows to get data. this creates Map as a imitated model and puts the key(Field.getName()) and the value (DataFormatter.formatCellValue(Cell, FormulaEvaluator)) to the model. The result is the same as the following code.

      
           +------+--------+--------+----------+
           | name | height | weight | eyesight |
           +------+--------+--------+----------+
           | John | 180.5  | 79.2   |          |
           +------+--------+--------+----------+
      
           This row will be converted to
      
           { "name": "John", "height": "180.5", "weight": "79.2", "eyesight": null }
       
      Parameters:
      row - row in sheet
      Returns:
      imitated model
    • beforeRead

      protected void beforeRead​(List<T> list)
    • afterRead

      protected void afterRead​(List<T> list)
    • readSheet

      protected abstract List<T> readSheet​(org.apache.poi.ss.usermodel.Sheet sheet)
    • beforeReadModels

      protected void beforeReadModels​(org.apache.poi.ss.usermodel.Sheet sheet)
    • getNumOfColumns

      protected abstract int getNumOfColumns​(org.apache.poi.ss.usermodel.Row row)
    • getColumnName

      protected abstract String getColumnName​(org.apache.poi.ss.usermodel.Cell cell, int columnIndex)