donnytian / Npoi.Mapper

Use this tool to import or export data with Excel file. The tool is a convention based mapper between strong typed object and Excel data via NPOI.
MIT License
589 stars 114 forks source link

Header name containing underscore #111

Closed acsel87 closed 1 year ago

acsel87 commented 2 years ago

Hi

I have an excel with header names containing underscores: ex. Column_Name

I map it using dynamic type (needs to be dynamic in my case):

var rows = mapper.Take<dynamic>("Sheet").ToList();

foreach(var row in rows) {
    var value = row.Value;

    somObj.ColumnName = value.Column_Name;
}

and it results in the following error: _"does not contain a definition for 'ColumnName' "

If I remove the underscore, it works perfectly.

As a workaround I can look for another naming convention for our headers, but I would like to know if this is a bug or I'm doing something wrong.

----------------------- EDIT ---------------------------------

I looked at the row.Value and it seems that even though in excel the header is Column_Name, when imported, it comes as row.Value.ColumnName (so the underscore, and possibly other characters as well, is removed)

This means there isn't any real issue anymore, but it's still confusing/unintuitive.

Thank you

donnytian commented 1 year ago

below chars will be removed from the name to ensure it's a valid property name. source code:

// Default chars that will be removed when mapping by column header name.
private static readonly char[] DefaultIgnoredChars =
{'`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '-', '_', '+', '=', '|', ',', '.', '/', '?'};

if you want to keep the underscore, just write this

// remove underscore
mapper.IgnoredNameChars = {'`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '-', '+', '=', '|', ',', '.', '/', '?'};

but yes, underscore is valid in a property name, I will consider removing it from the default array.