在Ken网格中隐藏标题组模板导出到excel

时间:2018-07-26 02:34:56

标签: javascript kendo-grid

大家好,我正在使用Kendo网格显示详细信息列表。

<div style="text-align:left;position:relative;width:auto; width:99%; padding:4px; padding-top:0px; color:#286006" id="centerPane-IFrame">
@(Html.Kendo().Grid<SDByBatchPublisherReportVM>()
.Name("ReportGrid")
.EnableCustomBinding(true)
.HtmlAttributes(new { style = "width:100%" })
.Columns(columns =>
{
 columns.Bound(x => x.PublisherName)
.Title("Publisher")
.Visible(true)
.Width("50%")
.HeaderHtmlAttributes(new { style = "font-weight: bold;" })
.ClientGroupHeaderTemplate("Publisher: #=value# </td><td style='text- 
align:center;'>Product Count: #=aggregates.ProductName.count# </td> 
<td>Discrepancy Count") //Discrepancy Count: #=aggregates.SDCount.sum#
.HtmlAttributes(new { style = "text-align:left" })
.Filterable(ftb => ftb.Multi(true).Search(true));
columns.Bound(x => x.ProductName)
.Title("Product")
.Width("60%")
.HeaderHtmlAttributes(new { style = "font-weight: bold;" })
 .Filterable(ftb => ftb.Multi(true).Search(true));
 columns.Bound(x => x.SDCount)
.Title("Discrepancy Count")
.Width("30%")
 .HeaderHtmlAttributes(new { style = "font-weight: bold;" })
 .Filterable(ftb => ftb.Multi(true).Search(true));
 columns.Template(@<text></text>).Title("").Width(1);                                                       
  })
 .Pageable(m => m.PageSizes(new int[] { 50, 100, 150, 500 }).Refresh(true))
 .Selectable(selectable => 
  selectable.Enabled(true).
  Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
 .Scrollable().Sortable()
 .Filterable()
 .DataSource(dataSource => dataSource
 .Ajax()
 .Batch(false)
 .ServerOperation(false)
  .PageSize(100)
 .Read(read => read.Action("ByBatchPublisherGrid_Read", 
  "SoftwareDiscrepancyMetrics").Data("buildReportData"))
 .Aggregates(agg =>
  {
  agg.Add(x => x.SDCount).Sum();
  agg.Add(x => x.ProductName).Count();
  })
 .Group(group =>
  {
  group.Add(x => x.PublisherName);
  })
  )
 .Events(evt => evt.DataBound("OnDataBound"))
  .Excel(excel => excel.AllPages(true).Filterable(true))
  )
 </div>

通过使用上面的代码在grid中显示详细信息列表。现在,我正在尝试将所有传入并显示在excel中的数据导出到excel。

我的需求是需要在Export to excel中删除GroupHeaderTemplate数据(无需在excel中显示该组头模板数据)。

 function ExportButtonClick() {
    debugger
    var grid = $("#ReportGrid").data("kendoGrid");
    if (grid.dataSource.total() == 0) {
        alert("There are no records available in the Grid to Export!!!");
    }
    else {
        var firstGroup = grid.dataSource.group()[0].field;
        $('#grid tbody .k-grouping-row:contains("")').hide();
        grid.bind("excelExport", function (e) {
            var batchId = $("#txtBatchId").data("kendoDropDownList").text();
            e.workbook.fileName = "By_BatchPublisher_Report_" + batchId + ".xlsx";
            debugger

            var sheet = e.workbook.sheets[0];
            var rows = e.workbook.sheets[0].rows;
            var gridData = "";
            //This is for Remvoing HTML Tags from Cell text // added on 07-05-2018
            for (var ri = 0; ri < rows.length; ri++) {
                var row = rows[ri];

                for (var ci = 0; ci < row.cells.length; ci++) {
                    row.cells[ci].hidden = true;
                    var cell = row.cells[ci];

                        if (row.type == "group-header") {
                            cell.value = "";
                        }
                        if (cell.value) {
                            gridData = cell.value;
                            if (!$.isNumeric(cell.value)) {
                                cell.value = gridData.replace(/(<([^>]+)>)/ig, "");
                            } else {
                                cell.value = gridData;
                            }
                    }
                }
            }

            //End Here
            debugger
            for (var i = 0; i < sheet.columns.length; i++) {
                    sheet.columns[i].autoWidth = false;
                    sheet.columns[i].width = 300;
            }
        });
        grid.saveAsExcel();
    }
}

我用于导出到excel的上述功能可以帮助我如何从excel中隐藏组标题模板。

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

function (e) {
            var rows = e.workbook.sheets[0].rows;
            var newRows = []
            for (var ri = 0; ri < rows.length; ri++) {
                var row = rows[ri];

                if (row.type !== "group-header") {
                    newRows.push(row)
                }
            }
            e.workbook.sheets[0].rows = newRows
            return e;
        }

上一篇文章中有不错的类似示例:https://www.telerik.com/forums/export-to-excel-also-exports-the-group-footer-template