mganss / ExcelMapper

An Excel to object mapper. Maps POCOs to and from Excel. Configuration via convention, attributes, or fluent methods.
MIT License
793 stars 122 forks source link

How can addMapping read index when headerRow is true #265

Closed Ahmad-F123 closed 1 year ago

Ahmad-F123 commented 1 year ago

I try with headerRow false, working fine but when headerRow is true, data will get empty

mganss commented 1 year ago

Can you provide example code that shows the issue?

Ahmad-F123 commented 1 year ago

var mapData = getMapData(); // return obj with indexvalue for Name, Title and Author also return HeaderRow var excel = new ExcelMapper(@"......\products.xlsx"); excel.HeaderRow = mapData.HeaderRow; // issue happen if headerRow is true excel.AddMapping(mapData.Name, p => p.Name); excel.AddMapping(mapData.Title, p => p.Title); excel.AddMapping(mapData.Author, p => p.Author);

var books = excel.Fetch<Book>().ToList();        
mganss commented 1 year ago

Can you show the class declarations for Book and the class of the mapData object?

Ahmad-F123 commented 1 year ago

public class Book { public string Name { get; set; } public string Title { get; set; } public string Author { get; set; }

}

public class mapData { public int Name { get; set; } public int Title { get; set; } public int Author { get; set; } public bool HeaderRow { get; set; }

}

mganss commented 1 year ago

If you're working with column indexes (not column names), then HeaderRow should always be false. If you still want to skip the first row when reading data, set MinRowNumber to 1.