Skip to content
Open
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 @@ -26,6 +26,8 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

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
Loading