From de927853081f37f936017f6b5b13f11be7958dc8 Mon Sep 17 00:00:00 2001 From: Michele Bastione Date: Wed, 9 Sep 2026 23:23:43 +0200 Subject: [PATCH] Fixed QueryAsync header handling in facade static class Inverted the hasHeader flag before forwarding it to the Excel and CSV importers from the MiniExcel facade class because their APIs use the opposite semantics. --- src/MiniExcel/MiniExcel.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/MiniExcel/MiniExcel.cs b/src/MiniExcel/MiniExcel.cs index 6fb631d1..d68437ae 100644 --- a/src/MiniExcel/MiniExcel.cs +++ b/src/MiniExcel/MiniExcel.cs @@ -113,10 +113,13 @@ public static async Task SaveAsAsync(this Stream stream, object value, bo public static IAsyncEnumerable QueryAsync(string path, string? sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration? configuration = null, bool hasHeader = true, CancellationToken cancellationToken = default) where T : class, new() { var type = path.GetExcelType(excelType); + + // the hasHeader parameter is inverted before being forwarded to the importers' methods + // as the treatHeaderAsData parameter because they have opposite semantics and defaults return type switch { - ExcelType.XLSX => ExcelImporter.QueryAsync(path, sheetName, startCell, hasHeader, configuration as NewOpenXmlConfiguration, cancellationToken), - ExcelType.CSV => CsvImporter.QueryAsync(path, hasHeader, configuration as Csv.CsvConfiguration, cancellationToken), + ExcelType.XLSX => ExcelImporter.QueryAsync(path, sheetName, startCell, !hasHeader, configuration as NewOpenXmlConfiguration, cancellationToken), + ExcelType.CSV => CsvImporter.QueryAsync(path, !hasHeader, configuration as Csv.CsvConfiguration, cancellationToken), _ => throw new InvalidDataException($"Type {type} is not a valid Excel type") }; } @@ -125,10 +128,13 @@ public static async Task SaveAsAsync(this Stream stream, object value, bo public static IAsyncEnumerable QueryAsync(this Stream stream, string? sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration? configuration = null, bool hasHeader = true, CancellationToken cancellationToken = default) where T : class, new() { var type = stream.GetExcelType(excelType); + + // the hasHeader parameter is inverted before being forwarded to the importers' methods + // as the treatHeaderAsData parameter because they have opposite semantics and defaults return type switch { - ExcelType.XLSX => ExcelImporter.QueryAsync(stream, sheetName, startCell, hasHeader, configuration as NewOpenXmlConfiguration, leaveOpen: true, cancellationToken), - ExcelType.CSV => CsvImporter.QueryAsync(stream, hasHeader, configuration as Csv.CsvConfiguration, leaveOpen: true, cancellationToken), + ExcelType.XLSX => ExcelImporter.QueryAsync(stream, sheetName, startCell, !hasHeader, configuration as NewOpenXmlConfiguration, leaveOpen: true, cancellationToken), + ExcelType.CSV => CsvImporter.QueryAsync(stream, !hasHeader, configuration as Csv.CsvConfiguration, leaveOpen: true, cancellationToken), _ => throw new InvalidDataException($"Type {type} is not a valid Excel type") }; }