0

I'm getting a input string was not in correct format when parsing an excel file with LINQ to 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 is my own class and it has 5 int attributes, each corresponding to a column in the excel file, that could be causing this error but I can't find which or where the error is coming from.

the query doesn't execute until it's enumerated and using the .ToList() method doesn't show which item (1600 excel lines) is even causing the error so I'm not sure where to look.

Is there a way to Iterate through the 'Items' IQueryable one by one to find which one is failing to cast into a StatusPedido - and hence identify which row/cell in the excel is throwing this error?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
ConnorU
  • 1,379
  • 2
  • 15
  • 27
  • 1
    iterating with a `foreach` without `ToList()` should let you step through each one. – DLeh Apr 08 '15 at 20:42

1 Answers1

0

as ListaItems is a list of StatusPedido

Try:

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;
A_Sk
  • 4,532
  • 3
  • 27
  • 51
  • Same error happens in your foreach(var x in items). Calling the foreach makes the program load the entire IQueryable into memory as StatusPedido items which in turn triggers the exception somewhere but does not specify on which line or value/s... – ConnorU Apr 09 '15 at 01:08