This is a migrated thread and some comments may be shown as answers.

Exporting to Excel Error

5 Answers 261 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jim
Top achievements
Rank 1
jim asked on 18 Feb 2015, 11:25 PM
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);
}

5 Answers, 1 is accepted

Sort by
0
jim
Top achievements
Rank 1
answered on 19 Feb 2015, 03:41 PM
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?
0
Accepted
Ryan
Top achievements
Rank 1
answered on 19 Feb 2015, 03:59 PM
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;

0
Ryan
Top achievements
Rank 1
answered on 19 Feb 2015, 04:01 PM
forgot

var cols = new Collection<GridColumnSettings>();
0
jim
Top achievements
Rank 1
answered on 21 Feb 2015, 02:56 AM
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
0
jim
Top achievements
Rank 1
answered on 23 Feb 2015, 03:08 PM
Thanks Ryan. That is exactly what I was looking for.
Tags
Grid
Asked by
jim
Top achievements
Rank 1
Answers by
jim
Top achievements
Rank 1
Ryan
Top achievements
Rank 1
Share this question
or