2014-06-17 114 views
0

我從Kendo網格導出數據。控制器中的代碼在foreach循環中發生錯誤。無法將劍道網格數據導出到Excel電子表格

ERROR MESSAGE

-InvalidCastException- 


{"Unable to cast object of type '<>f__AnonymousType2`6[System.DateTime,System.String,System.Int32,System.String,System.String,System.String]' to type 'ZoomAudits.DAL.TypedViewClasses.ReportPhoneSupportResultRow'."} 

堆棧跟蹤:

在 UtilityWebSite.Controllers.ReportsController.Export(DataSourceRequest 請求,ReportsPhoneSupportSearchVM模型)在 C:\ Users \用戶texas_000 \桌面\ UtilityWebSite \ UtilityWebSite \ Controllers \ ReportsController.cs:line 130 at lambda_method(Closure,ControllerBase,Object [])at System.Web.Mvc.Action MethodDispatcher.Execute(ControllerBase 控制器,對象[]參數)在 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary的參數)在 System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod () 在 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase 1.End(System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult的 asyncResult,ActionInvocation innerInvokeState)) 在System.Web.Mvc .Async.AsyncResultWrapper.End [TResult](IAsyncResult asyncResult,Object tag)at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult的 asyncResult)在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3c() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。 <> c__DisplayClass45.b__3e()

我已經搜索了可能的答案,但我無法弄清楚需要更改什麼?該項目設置LLBL,我不熟悉它。任何幫助都會很棒。如果您需要更多信息,請告訴我。謝謝

public FileResult Export([DataSourceRequest]DataSourceRequest request, ReportsPhoneSupportSearchVM model) 
    { 
     ReportPhoneSupportResultTypedView results = new ReportPhoneSupportResultTypedView(); 
     string[] userIds = model.UserId.Split(','); 
     foreach (string userId in userIds) 
     { 
      int iUserId = 0; 
      if (Int32.TryParse(userId, out iUserId)) 
      { 

       RetrievalProcedures.FetchReportPhoneSupportResultTypedView(results, model.FromDate, model.ToDate, iUserId); 
      } 
     } 

     var Results = from Reslt in results 
         select new 
         { 
          ActivityDate = Reslt.ActivityDate, 
          Action = Reslt.Action, 
          Assignment = Reslt.Assignment, 
          Description = Reslt.Description, 
          Result = Reslt.Result, 
          ToFrom = Reslt.ToFrom 
         }; 


     //Get the data representing the current grid state - page, sort and filter 
     IEnumerable ExcelResults = Results.ToDataSourceResult(request).Data; 

     //Create new Excel workbook 
     var workbook = new HSSFWorkbook(); 

     //Create new Excel sheet 
     var sheet = workbook.CreateSheet(); 

     //(Optional) set the width of the columns 
     sheet.SetColumnWidth(0, 10 * 256); 
     sheet.SetColumnWidth(1, 50 * 256); 
     sheet.SetColumnWidth(2, 50 * 256); 
     sheet.SetColumnWidth(3, 50 * 256); 

     //Create a header row 
     var headerRow = sheet.CreateRow(0); 

     //Set the column names in the header row 
     headerRow.CreateCell(0).SetCellValue("ActivityDate"); 
     headerRow.CreateCell(1).SetCellValue("Assignment"); 
     headerRow.CreateCell(2).SetCellValue("Action"); 
     headerRow.CreateCell(3).SetCellValue("ToFrom"); 
     headerRow.CreateCell(2).SetCellValue("Result"); 
     headerRow.CreateCell(3).SetCellValue("Description"); 

     //(Optional) freeze the header row so it is not scrolled 
     sheet.CreateFreezePane(0, 1, 0, 1); 

     int rowNumber = 1; 

     //Populate the sheet with values from the grid data 
     foreach (ReportPhoneSupportResultRow ER in ExcelResults) 
     { 
      //Create a new row 
      var row = sheet.CreateRow(rowNumber++); 

      //Set values for the cells 
      row.CreateCell(0).SetCellValue(ER.ActivityDate); 
      row.CreateCell(1).SetCellValue(ER.Assignment); 
      row.CreateCell(2).SetCellValue(ER.Action); 
      row.CreateCell(3).SetCellValue(ER.ToFrom); 
      row.CreateCell(2).SetCellValue(ER.Result); 
      row.CreateCell(3).SetCellValue(ER.Description); 
     } 

     //Write the workbook to a memory stream 
     MemoryStream output = new MemoryStream(); 
     workbook.Write(output); 

     //Return the result to the end user 

     return File(output.ToArray(), //The binary data of the XLS file 
      "application/vnd.ms-excel", //MIME type of Excel files 
      "GridExcelExport.xls");  //Suggested file name in the "Save as" dialog which will be displayed to the end user 

    } 
+1

請錯誤添加到您的問題 –

+0

我把它排在首位。謝謝 – user3750212

+0

任何想法?我是否需要發佈更多信息? – user3750212

回答

0

得到了解決辦法。使用此代碼在FileExport行動

ReportPhoneSupportResultTypedView results = new ReportPhoneSupportResultTypedView(); 
    string[] userIds = model.UserId.Split(','); 
    foreach (string userId in userIds) 
    { 
     int iUserId = 0; 
     if (Int32.TryParse(userId, out iUserId)) 
     { 

      RetrievalProcedures.FetchReportPhoneSupportResultTypedView(results, model.FromDate, model.ToDate, iUserId); 
     } 
    } 


    //Get the data representing the current grid state - page, sort and filter 
    IEnumerable ExcelResults = ((IEnumerable)results).ToDataSourceResult(request).Data;