1
votes

Following the demo: https://demos.telerik.com/aspnet-mvc/treelist/excel-export

We're trying to implement the excel export in our kendo treelist. The issue is that when downloaded, the excel file has no data, just the header of the column.

        <%: Html.Kendo().TreeList<A.Models.ExportModel>()
            .Name("treelist")
            .Columns(columns =>
            {
                columns.Add().Field(e => e.ACTIVITY).Title("Activity").Width(400);      
            })
            .Toolbar(tools => tools.Excel())
            .Excel(excel => excel.FileName("TreeListExport.xlsx").ProxyURL(Url.Action("Excel_Export_Save")))          
            .DataSource(dataSource => dataSource   
                .Read(read => read.Action("getActivityData", "ExportActivity"))                    
                .ServerOperation(false)                    
                .Model(m => {
                    m.Id(f => f.PK);
                    m.ParentId(f => f.PA);
                    m.Expanded(true);
                    m.Field(f => f.ACTIVITY);
                })
            )
            .Height(540) 
        %>



    [HttpPost]
    public ActionResult Excel_Export_Save(string contentType, string base64, string fileName)
    {
        var fileContents = Convert.FromBase64String(base64);

        return File(fileContents, contentType, fileName);
    }

How can we solve this?

1
What's the value of base64 before returning the file? - diiN__________
It doesn't enter into the ActionResult @diiN_ - ArDevTeam
Check the network activity in developer tools if there is an error when clicking on the export button. - diiN__________
There are no errors @diiN_ - ArDevTeam
However, it's not mandatory the ActionResult, it should work without it too! @diiN_ - ArDevTeam

1 Answers

0
votes

I just encountered this. You need to intercept the Excel export event and write JavaScript to massage the values into what you want in your table.

.Toolbar(tools => tools.Excel())
.Excel(excel => excel.FileName("LocationHierarchy.xlsx").ProxyURL(Url.Action("ExcelExportSave")))
.Events(events => events.ExcelExport("onExcelExport"))

It sounds like you already know what you need to do for the ExcelExportSave() method, so I won't cover it.

Declare your JavaScript method thus:

// groom the exported values
function onExcelExport(e) {

    var workbook = e.workbook;

    var i = 0;

    // loop through all the worksheets
    for (i = 0; i < workbook.sheets.length; ++i) {

        // iterate over all the rows, skipping the first since it is just the column headings
        var j = 0;
        for (j = 1; j < workbook.sheets[i].rows.length; ++j) {

            var thisRow = workbook.sheets[i].rows[j];

            // iterate over all the cells
            for (k = 0; k < thisRow.cells.length; ++k) {
                var cellData = thisRow.cells[k];
                var cellValue = cellData.value;

                // do your special stuff so your values show up like you want
                // this often just requires digging into an object and getting the correct member value
                // slap the value in cellData.value to make them appear in the Excel spreadsheet
                cellData.value = myCoolNewValue;
            }
        }
    }
}

That's it. Then your exported Excel is nice and beautiful.