【问题标题】:ClosedXml InsertTable with IEnumerable property具有 IEnumerable 属性的 ClosedXml InsertTable
【发布时间】:2020-04-27 15:28:03
【问题描述】:

已经接管了一个使用 ClosedXML 的遗留项目。创建错误的方法需要一个

IEnumerable<MainObject> rows 

并创建一个工作表。但是存在另一个属性

IEnumerable<Object1>

在主对象中。代码通过

执行
                worksheet.Cell(startRow, 1).InsertTable(rows);

当我这样做时,我得到了

An unhandled exception has occurred: Unable to set cell value to System.Collections.Generic.List`1[Object1]
System.ArgumentException: Unable to set cell value to System.Collections.Generic.List`1[Object1]

我的猜测是我必须创建另一个(被覆盖?)InsertTable,它使用反射来调用正确的类型,然后甚至在 IEnumerable 属性上使用 InsertTable。 但是有可能在重写的函数中使用某种递归调用并跳过所有自定义映射吗?

任何想法或建议都会很好。

【问题讨论】:

    标签: c# generic-list closedxml


    【解决方案1】:

    ClosedXML 不知道如何处理每一行的内部Object1。如果您创建自己的 POCO 对象的 IEnumerable&lt;&gt; 并在其上调用 InsertTable 会更容易,例如

    var pocos = rows.Select(m => new { m.Property1, m.Property2, m.Object1.OtherProperty });
    worksheet.Cell(startRow, 1).InsertTable(pocos);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      相关资源
      最近更新 更多