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

ExcelMapper always generate extra columns #298

Open LJN-hzleaper opened 7 months ago

LJN-hzleaper commented 7 months ago

In my demo app, ExcelMapper always output extra columns that I dont't want. How to prevent it?

        ExcelMapper mapper = new();
        mapper.AddMapping(typeof(Dto), "Identity", nameof(Dto.Id));
        mapper.AddMapping(typeof(Dto), "User Name", nameof(Dto.Name))
            .ToExcelOnly();
        mapper.Save("E:/Test.xlsx", GetData(), "Hi");

public class Dto
{
    public int Id { get; set; }

    public string? Name { get; set; }
}

image

LJN-hzleaper commented 7 months ago

I can remove these columns one by one from output by Ignore, but is there a batch way?

        ExcelMapper mapper = new();
        mapper.Ignore(typeof(Dto), nameof(Dto.Id));
        mapper.Ignore(typeof(Dto), nameof(Dto.Name));
        mapper.AddMapping(typeof(Dto), "Identity", nameof(Dto.Id));
        mapper.AddMapping(typeof(Dto), "User Name", nameof(Dto.Name))
            .ToExcelOnly();
        mapper.Save("E:/Test.xlsx", GetData(), "Hi");
mganss commented 7 months ago

You can remove all mappings like so:

var typeMapper = mapper.TypeMapperFactory.Create(typeof(Dto));
typeMapper.ColumnsByName.Clear();