在演示之后:https://demos.telerik.com/aspnet-mvc/treelist/excel-export
我们正尝试在我们的kendo treelist中实现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);
}我们该如何解决这个问题呢?
发布于 2017-07-19 03:34:40
我刚刚遇到了这个。您需要截获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就很漂亮了。
https://stackoverflow.com/questions/35311795
复制相似问题