Class AbstractExcelSheetRowIterator<V>

java.lang.Object
io.github.connellite.excel.AbstractExcelSheetRowIterator<V>
Type Parameters:
V - map value type produced by subclasses
All Implemented Interfaces:
AutoCloseable, Iterator<Map<String,V>>
Direct Known Subclasses:
ExcelRowIterator, ExcelRowStringIterator

public abstract class AbstractExcelSheetRowIterator<V> extends Object implements Iterator<Map<String,V>>, AutoCloseable
Forward-only iterator over data rows of an Apache POI Sheet. The first row is treated as a header; column names are derived from that row (blank headers become Column_0, Column_1, …). This type implements only Iterator (not Iterable) so APIs with overloads for both (for example org.jooq.lambda.Seq.seq) remain unambiguous when passed this instance. For enhanced for-loops use asIterable(); it is single-pass and delegates to this iterator.

close() closes the Workbook passed to the constructor. Do not close the same workbook again afterward (for example avoid wrapping the workbook in a second try-with-resources if the iterator already owns shutdown).

See Also:
  • Field Details

    • columnNames

      protected final List<String> columnNames
    • rowIterator

      protected final Iterator<org.apache.poi.ss.usermodel.Row> rowIterator
    • formatter

      protected final org.apache.poi.ss.usermodel.DataFormatter formatter
  • Constructor Details

    • AbstractExcelSheetRowIterator

      protected AbstractExcelSheetRowIterator(org.apache.poi.ss.usermodel.Workbook workbook, String sheetName)
      Parameters:
      workbook - workbook that contains the sheet; closed when close() is called
      sheetName - name of the sheet to read
      Throws:
      IllegalArgumentException - if no sheet exists with the given name
  • Method Details

    • getCellObjectValue

      protected Object getCellObjectValue(org.apache.poi.ss.usermodel.Cell cell)
      Cell value for data rows: strings, Double for plain numbers, Boolean, formatted string for dates, cached formula result (not the formula text).
    • asIterable

      public Iterable<Map<String,V>> asIterable()
      Single-pass Iterable view for enhanced for-loops. Iterable.iterator() returns
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<V>
    • next

      public abstract Map<String,V> next()
      Specified by:
      next in interface Iterator<V>
    • getColumnNames

      public List<String> getColumnNames()
      Column names from the first row of the sheet, in order.
    • close

      public void close() throws Exception
      Closes the workbook from construction.
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception