ExcelMapper icon indicating copy to clipboard operation
ExcelMapper copied to clipboard

ExcelMapper always generate extra columns

Open LJN-hzleaper opened this issue 1 year ago • 2 comments

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 avatar Mar 25 '24 11:03 LJN-hzleaper

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");

LJN-hzleaper avatar Mar 25 '24 12:03 LJN-hzleaper

You can remove all mappings like so:

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

mganss avatar Mar 25 '24 14:03 mganss