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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to filter empty rows in Mapper.Map method

SAPHemlock opened this issue · comments

I want to import four columns from excel sheet but it contains empty rows as well.
I could not find a way if I can ignore all empty rows somehow during import.
Because I need to validate four columns if they have empty cells or not?

Sample ExcelWithEmptyRows.xlsx

I want to import four columns from excel sheet but it contains empty rows as well.
I could not find a way if I can ignore all empty rows somehow during import.
Because I need to validate four columns if they have empty cells or not?

Sample ExcelWithEmptyRows.xlsx

My code is as below which gives me all rows including empty rows as per sample excel mentioned in the issue.
C# Entity :

class Inventory
    {
        [Column(7)]
        public string SignTrackingNum { get; set; }
        [Column(14)]
        public string Qty { get; set; }
        [Column(15)]
        public string Size { get; set; }
        [Column(16)]
        public string NetDistribution { get; set; }
    }

Mapper.Map method

importer.Map(
                    column => // column filter : Custom logic to determine whether or not to map and include an unmapped column.
                    { // Read inventory record only if there is valid non-empty "SignTracking#" 
                        if (column.Attribute.Index == 0 && (column.CurrentValue == null
                            || string.IsNullOrEmpty(column.CurrentValue.ToString())))
                        { //Common.Logger.Info($"Empty Sign Tracking# cell (importer.map(columns)): {column.CurrentValue}");
                            Common.Logger.Error($"Empty Sign Tracking# cell detected");
                            return false;}
                        else{ //Common.Logger.Info(column.CurrentValue);
                            return true;}},
                    (column, target) => // tryTake resolver : Custom logic to take cell value into target object.
                    {// Note: return false to indicate a failure; and that will increase error count.
                        if (column.Attribute.Index == 0 && (column.CurrentValue == null ||
                                string.IsNullOrEmpty(column.CurrentValue.ToString()))){
                            //Common.Logger.Info($"Empty Sign Tracking# cell (importer.map(column,target)): {column.CurrentValue}");
                            Common.Logger.Error($"Empty Sign Tracking# cell detected");
                            return false;}
                        else{ //Common.Logger.Info(column.CurrentValue);
                            return true;
                        }});

If somehow I can check from first column, if first column is empty then I can ignore such rows.
So, I put Index == 0 above but still it gives me all records including empty rows.

Hi, do not use the default column resolver, it's only for unmapped column. You already specified mapping by Column attribute.

You can remove those attributes and use fluent method to do the mapping.
Check those overloads.
https://github.com/donnytian/Npoi.Mapper/blob/master/Npoi.Mapper/src/Npoi.Mapper/Extensions/MapExtensions.cs#L50