簡體   English   中英

無法將Kendo網格數據導出到Excel電子表格

[英]Cannot export kendo grid data to Excel spreadsheet

我正在從劍道網格導出數據。 控制器中的代碼在foreach循環中因錯誤而中斷。

錯誤信息

  -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'."}

堆棧跟蹤:

在c:\\ Users \\ texas_000 \\ Desktop \\ UtilityWebSite \\ UtilityWebSite \\ Controllers \\ ReportsController.cs:第130行at lambda_method(Closure,ControllerBase,Object [])在UtilityWebSite.Controllers.ReportsController.Export(DataSourceRequest request,ReportsPhoneSupportSearchVM模型) .Web.Mvc.ActionMethodDispatcher.Execute(System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()處的2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2參數)System.Web.Mvc.Async處的System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult,ActionInvocation innerInvokeState) 2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase System.Web.Mvc.Async.AsyncResultWrapper.End [TResult]的2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase 1.End()(System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)的System.Web.Mvc.Async.AsyncControllerActionInvoker.End System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters中的.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3c()。<> 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

    }

得到了解決方案。 在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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM