无法将剑道网格数据导出到Excel电子表格

本文关键字:Excel 电子表格 数据 网格 | 更新日期: 2023-09-27 18:09:07

我正在从剑道网格导出数据。控制器中的代码在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'."}
异常堆栈

:

在UtilityWebSite.Controllers.ReportsController.Export (DataSourceRequest请求,ReportsPhoneSupportSearchVM模型)c:'Users'texas_000'Desktop' UtilityWebSite ' UtilityWebSite '控制器' ReportsController.cs:行lambda_method(Closure, ControllerBase, Object[]System.Web.Mvc.ActionMethodDispatcher.Execute (ControllerBase控制器,对象[]参数)System.Web.Mvc.ReflectedActionDescriptor.Execute (ControllerContext控制器上下文,字典2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary参数)System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod ()在System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36 (IAsyncResultasyncResult, ActionInvocation, innerInvokeState)System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase . end ()在System.Web.Mvc.Async.AsyncResultWrapper.End TResult (IAsyncResultasyncResult,对象标签)System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod (IAsyncResultasyncResult)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
    }

无法将剑道网格数据导出到Excel电子表格

找到解决方案了。在FileExport Action

中使用此代码
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;