Skip to content

Saving a list of dictionary items creates empty XL file #191

Open

Description

@mkgn

I am using XL Mapper version 5.2.352 with .Net core 5.

Since my data structure can contain varying number of columns I have those data arranged in a collection of dictionaries. A dictionary has data similar to below;

image

I assume the Keys of the dictionary will be taken as column headings and the values of each dictionary will be written as rows in the XL.

var mylist = new List<IDictionary<string, string>>();
//fill data to a new dictionary instance
var itemdict = new Dictionary.....
mylist.Add(itemdict);

....
xlMapper.Save(fileName, mylist); 

When I do this as above it throws below exception;

System.Reflection.TargetParameterCountException
Parameter count mismatch.

System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at Ganss.Excel.ColumnInfo.GetProperty(Object o)
at Ganss.Excel.ExcelMapper.SetCell[T](Func3 valueConverter, T objInstance, ICell cell, ColumnInfo ci) at Ganss.Excel.ExcelMapper.SetCells(TypeMapper typeMapper, Dictionary2 columnsByIndex, Object o, IRow row, Func3 valueConverter) at Ganss.Excel.ExcelMapper.Save[T](Stream stream, ISheet sheet, IEnumerable1 objects, Func3 valueConverter) at Ganss.Excel.ExcelMapper.Save[T](Stream stream, IEnumerable1 objects, Int32 sheetIndex, Boolean xlsx, Func3 valueConverter) at Ganss.Excel.ExcelMapper.Save[T](String file, IEnumerable1 objects, Int32 sheetIndex, Boolean xlsx, Func`3 valueConverter)

However if I change my code to;

var mylist = new List<dynamic>(); //earlier it was of type IDictionary<string, string>
//fill data to a new dictionary instance
var itemdict = new Dictionary.....
mylist.Add(itemdict);

....
xlMapper.Save(fileName, mylist); 

Now it doesn't throw an exception, but nothing gets written to the file.

How can I get this done properly?

Activity

  1. mganss commented on May 13, 2022

    @mganss
    Owner

    This use case isn't currently supported. The closest is support for dynamic objects so you could pass a list of ExpandoObject (which implements IDictionary<string, object>) to Save(). Still this necessitates that each object in the list has all keys, possibly with null values for the unused columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions