繁体   English   中英

剑道树列表导出问题

[英]Kendo Treelist Export Issue

演示之后: 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);
    }

我们该如何解决呢?

我刚遇到这个。 您需要拦截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就很漂亮。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM