Mihir Chittora
Mihir Chittora

Reputation: 21

Kendo Grid Excel Export with Columns with DropDown Template

I want to export the kendo grid data into excel. I am using kendo dropdownlist as a template for some columns.

When i export it exports but it not show the text value for those columns but rather shows value for that fields.

Upvotes: 2

Views: 1430

Answers (1)

JEGardner
JEGardner

Reputation: 41

This may not be the "best" solution but it worked for me. Basically I created a function that loops through and processes the row with the dropdown, using a switch statement to set the text based on the value (the data that comes through unfortunately only has the value, rather than the object).

Here is the markup for my grid:

<div id="grid" data-role="grid"
data-sortable=" {mode: 'single' , allowunsort:true }"
data-toolbar="[{template: this.model.getToolbar}]"
data-excel="{ fileName: 'export.xlsx', allPages:'true' }"
data-excel-export="model.curateExcelData"
data-columns="@string.Format(@"[
        {{ title:'{0}', field: 'Column1', width:80 }},
        {{ title:'{1}', field: 'Column2', width:80 }},
        {{ title:'{12}', field: 'DropDownColumn',template: kendo.template($('#dropdown-template').html()),'width':80, sortable: false }},
]", CommonResource.Column1, CommonResource.Column2, CommonResource.DropDownColumn)"
data-bind="source: items"
data-editable="inline">
</div>

And the template:

<script id="dropdown-template" type="text/x-kendo-template">    
    <input data-role="dropdownlist"
           data-auto-bind="false"
           data-value-primitive="true"
           data-text-field="Text"
           data-value-field="Value"
           data-bind="value: dropDownValue, source: dropDownValues"
           style="width:65px" />
</script>

And here is the function that updates the values for the export (in my case I had a dropdown that was "Days" and "Weeks", which was the 12th column):

curateExcelData: function (data) {
            var sheet = data.workbook.sheets[0];
            $.each(sheet.rows, function (index, row) {
                if (row.type == "data") {
                    switch (row.cells[12].value) {
                        case "D":
                            row.cells[12].value = "Days";
                            break;
                        case "W":
                            row.cells[12].value = "Weeks";
                        default:
                            break;
                    }
                }
            });
        },

Upvotes: 0

Related Questions