diff --git a/fesod-examples/fesod-sheet-examples/src/test/java/org/apache/fesod/sheet/examples/ExampleTestBase.java b/fesod-examples/fesod-sheet-examples/src/test/java/org/apache/fesod/sheet/examples/ExampleTestBase.java index 479ebe7e2..fb46a1259 100644 --- a/fesod-examples/fesod-sheet-examples/src/test/java/org/apache/fesod/sheet/examples/ExampleTestBase.java +++ b/fesod-examples/fesod-sheet-examples/src/test/java/org/apache/fesod/sheet/examples/ExampleTestBase.java @@ -28,6 +28,8 @@ import java.nio.file.Path; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Row; /** * Base class for Fesod example integration tests. @@ -87,6 +89,41 @@ protected static void assertValidExcelFile(File file, int minDataRows) { } } + /** + * Assert that the given file is a valid Excel workbook with at least the specified number of + * columns, scanning for the first non-empty row to determine the column count. + * + * @param file the Excel file to validate + * @param minColumns the minimum number of columns expected + */ + protected static void assertValidExcelFileColumns(File file, int minColumns) { + int totalColumns = 0; + assertValidExcelFile(file); + + try (FileInputStream fis = new FileInputStream(file); + Workbook workbook = WorkbookFactory.create(fis)) { + + Sheet sheet = workbook.getSheetAt(0); + + // find which row is not empty + for (int i = 0; i <= sheet.getLastRowNum(); i++) { + Row row = sheet.getRow(i); + + if (row != null && row.getPhysicalNumberOfCells() > 0) { + totalColumns = row.getLastCellNum(); + break; + } + } + + assertTrue( + totalColumns >= minColumns, + "Expected at least " + minColumns + " columns, but found " + totalColumns + + " total columns in the first data row of: " + file.getAbsolutePath()); + } catch (IOException e) { + fail("Failed to read workbook for column count verification: " + e.getMessage()); + } + } + /** * Generate a temp output file path within the given directory. * diff --git a/website/docs/sheet/read/simple.md b/website/docs/sheet/read/simple.md index 7e684b92e..c0ab151d5 100644 --- a/website/docs/sheet/read/simple.md +++ b/website/docs/sheet/read/simple.md @@ -246,3 +246,33 @@ public void synchronousReadToMapList() { } } ``` +## Reading Specific Columns (Column Filtering) + +By default, Fesod reads all columns from a sheet. If you only want to process specific columns, you can use `.includeColumnIndexes(List)`. This selects specific columns out of the entire sheet column set + +### Reading Specific Columns to Map List (Synchronous) + +When reading without a POJO template, the filtered columns are packed sequentially into the resulting `Map`. The first targeted column is mapped to key `0`, the second to key `1`, and so on. + +```java +@Test +public void readSpecificColumnsToMapList() { + String fileName = "path/to/demo.xlsx"; + + // We only want to read Column A (Index 0) and Column C (Index 2) + List targetColumns = Arrays.asList(0, 2); + + List> list = FesodSheet.read(fileName) + .sheet() + .includeColumnIndexes(targetColumns) + .doReadSync(); + + for (Map data : list) { + // Column Index 0 is mapped to key 0 + String id = data.get(0); + // Column Index 2 is mapped sequentially to key 1 + String age = data.get(1); + + log.info("ID: {}, Age: {}", id, age); + } +}