Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down
30 changes: 30 additions & 0 deletions website/docs/sheet/read/simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer>)`. 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<Integer, String>`. 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<Integer> targetColumns = Arrays.asList(0, 2);

List<Map<Integer, String>> list = FesodSheet.read(fileName)
.sheet()
.includeColumnIndexes(targetColumns)
.doReadSync();

for (Map<Integer, String> 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);
}
}
Loading