Kendo Treelist出口问题

时间:2016-02-10 09:52:39

标签: c# asp.net-mvc kendo-ui telerik export-to-excel

演示之后:https://demos.telerik.com/aspnet-mvc/treelist/excel-export

我们正试图在我们的剑道树干中实现excel导出。 问题是,下载时,excel文件没有数据,只有列的标题。

        <%: 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);
    }

我们如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我刚刚遇到这个。您需要拦截Excel导出事件并编写JavaScript以将值按到您想要的表中。

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

听起来你已经知道你需要为ExcelExportSave()方法做些什么,所以我不会介绍它。

声明你的JavaScript方法:

// 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;
            }
        }
    }
}

就是这样。然后你导出的Excel很漂亮。