Exporting to Excel Error

6 posts, 1 answers
  1. jim
    jim avatar
    11 posts
    Member since:
    Jun 2012

    Posted 18 Feb 2015 Link to this post

    I copied the excel demo and got the following message.  I am not sure if it is b.c i am dynamically binding that data.

    Excel export is not supported in server binding mode.

    View
    @(Html.Kendo().Grid(Model)
        .Name("grid")
        .Columns(columns =>
        {
            foreach (System.Data.DataColumn column in Model.Columns)
            {
                var c = columns.Bound(column.ColumnName);
     
            }
        })
        .ToolBar(tools => tools.Excel())
        .Pageable()
        .Sortable()
        .Scrollable()
        .Excel(excel => excel
            .FileName("Export.xlsx")
            .Filterable(true)
            .ProxyURL(Url.Action("Excel_Export_Save", "Reports"))
        )
        .HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
        .Server()
        .Model(model =>
        {
            foreach (System.Data.DataColumn column in Model.Columns)
            {
                var field = model.Field(column.ColumnName, column.DataType);
            }
        })
        )
     
    )

    controller
    [HttpPost]
    public ActionResult Excel_Export_Save(string contentType, string base64, string fileName)
    {
        var fileContents = Convert.FromBase64String(base64);
     
        return File(fileContents, contentType, fileName);
    }
  2. jim
    jim avatar
    11 posts
    Member since:
    Jun 2012

    Posted 19 Feb 2015 in reply to jim Link to this post

    I see the error is pretty obvious.  That I need to change it to ajax

    my problem is that I am using a datatable since the results are dynmamic. As you cansee I am creating the columns based off of the results.

    how would I do this using ajax?
  3. Answer
    Ryan
    Ryan avatar
    2 posts
    Member since:
    Oct 2013

    Posted 19 Feb 2015 in reply to jim Link to this post

    Could you try something like

    @(Html.Kendo().Grid<DataRow>()
        .Name("grid")
            .Columns(columns =>
                columns.LoadSettings((IEnumerable<GridColumnSettings>)ViewData["Columns"])
            )

    in the controller before you display the view

    foreach (DataColumn column in dt.Columns)
                        {
                            var gcs = new GridColumnSettings()
                                {
                                    Member = column.ColumnName,
                                    Width = "225px",
                                };
     
                            //don't show
                            if (column.ColumnName == "CurrencyExponent")
                            {
                                gcs.Visible = false;
                            }
     
                            if (column.DataType == typeof(Decimal))
                            {
                                gcs.ClientTemplate = ("#= kendo.toString(" + column.ColumnName + ",'n'.concat(CurrencyExponent)) #");
                            }
     
                            if (column.DataType == typeof(DateTime))
                            {
                                gcs.Member = gcs.Member.Replace("UTC", "");
                                gcs.ClientTemplate = ("#= kendo.toString(calcLocal(kendo.parseDate(" + column.ColumnName + ")),\"dd/MMM/yyyy HH:mm:ss\") #");
                            }
     
                            cols.Add(gcs);
     
     
                        }
     
                        ViewData["Columns"] = cols;

  4. Ryan
    Ryan avatar
    2 posts
    Member since:
    Oct 2013

    Posted 19 Feb 2015 in reply to Ryan Link to this post

    forgot

    var cols = new Collection<GridColumnSettings>();
  5. jim
    jim avatar
    11 posts
    Member since:
    Jun 2012

    Posted 20 Feb 2015 in reply to Ryan Link to this post

    Thanks Ryan.  I will try it out monday and let you know.  I like the fact that you can format the columns based on the data
  6. jim
    jim avatar
    11 posts
    Member since:
    Jun 2012

    Posted 23 Feb 2015 Link to this post

    Thanks Ryan. That is exactly what I was looking for.
Back to Top