【问题标题】:Input string was not in correct format error in LINQ to Excel queryLINQ to Excel 查询中的输入字符串格式错误
【发布时间】:2015-04-08 20:38:49
【问题描述】:

使用 LINQ to Excel 解析 excel 文件时,输入字符串的格式不正确:

var excel = new ExcelQueryFactory(ExcelFileURI);
excel.DatabaseEngine = DatabaseEngine.Ace;

var items = excel.Worksheet<StatusPedido>(sheetName);

List<StatusPedido> list = items.ToList(); //Error points here

StatusPedido 是我自己的类,它有 5 个 int 属性,每个属性对应于 excel 文件中的一列,这可能会导致此错误,但我找不到错误来自哪个或来自何处。

查询在被枚举之前不会执行,并且使用 .ToList() 方法不会显示哪个项目(1600 行 excel 行)甚至导致了错误,所以我不确定在哪里查看。

有没有办法逐个遍历“项目”IQueryable 以查找哪个项目未能转换为 StatusPedido - 从而确定 excel 中的哪个行/单元格引发此错误?

【问题讨论】:

  • 在没有ToList() 的情况下使用foreach 进行迭代应该让您逐步完成每一个。

标签: c# excel linq linq-to-excel


【解决方案1】:

因为ListaItemsStatusPedido 的列表

试试:

List<StatusPedido> ListaItems;

        Console.WriteLine("Getting articles from excel file");
        var excel = new ExcelQueryFactory(ExcelFileURI);
        excel.DatabaseEngine = DatabaseEngine.Ace;

        //var Items = from c in excel.Worksheet<StatusPedido>(sheetName)
        var Items = from c in excel.Worksheet<StatusPedido>(sheetName)
                    select c;
foreach(var x in items)
{
StatusPedido objData=new StatusPedido();
objData.val1=x.val1;
.
.
.
ListaItems.Add(objData);

}
return ListaItems;

【讨论】:

  • 在你的 foreach 中发生了同样的错误(var x in items)。调用 foreach 会使程序将整个 IQueryable 作为 StatusPedido 项加载到内存中,这反过来又会在某处触发异常,但没有指定在哪一行或哪一行或 value/s...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多