XlsxReader implements DataReader<XlsxReadOptions> { @Override public Table read(XlsxReadOptions options) throws IOException { List<Table> tables = readMultiple(options, true); if (options.sheetIndex() != null) { int index = options.sheetIndex(); if (index < 0 || index >= tables.size()) { throw new IndexOutOfBoundsException( String.format("Sheet index %d outside bounds. %d sheets found.", index, tables.size())); } Table table = tables.get(index); if (table == null) { throw new IllegalArgumentException( String.format("No table found at sheet index %d.", index)); } return table; } return tables.stream() .filter(t -> t != null) .findFirst() .orElseThrow(() -> new IllegalArgumentException("No tables found.")); }  static void register(ReaderRegistry registry); @Override Table read(XlsxReadOptions options); List<Table> readMultiple(XlsxReadOptions options); @Override Table read(Source source);  }
@Test public void testSheetIndex() throws IOException { Table table = new XlsxReader() .read(XlsxReadOptions.builder("../data/multiplesheets.xlsx").sheetIndex(1).build()); assertNotNull(table, "No table read from multiplesheets.xlsx"); assertColumnValues(table.stringColumn("stringcol"), "John", "Doe"); assertEquals("multiplesheets.xlsx#Sheet2", table.name(), "table name is different"); Table tableImplicit = new XlsxReader().read(XlsxReadOptions.builder("../data/multiplesheets.xlsx").build()); assertNotNull(tableImplicit, "No table read from multiplesheets.xlsx"); try { new XlsxReader() .read(XlsxReadOptions.builder("../data/multiplesheets.xlsx").sheetIndex(0).build()); fail("First sheet is empty, no table should be found"); } catch (IllegalArgumentException iae) { } try { new XlsxReader() .read(XlsxReadOptions.builder("../data/multiplesheets.xlsx").sheetIndex(5).build()); fail("Only 2 sheets exist, no sheet 5"); } catch (IndexOutOfBoundsException iobe) { } }