Kendo saveasexcel dynamically header of excel shee

2019-06-06 01:55发布

问题:

I have a kendo grid, like this:

@(Html.Kendo().Grid<SDB.Models.NettoVergelijking.Dienstverband>()
        .Name("overzicht-grid")
        .AutoBind(false)
        .Columns(columns =>
        {
            columns.Bound(d => d.Naam).Title("Medewerker").ClientTemplate("<span title='${Naam}'>${Naam}</span>").Width(300)
                .Filterable(f =>
                {
                    f.Extra(false);
                    f.Operators(op =>
                    {
                        op.ForString(str =>
                        {
                            str.Clear().Contains("Bevat");
                        });
                    });
                });
            columns.Bound(d => d.Contractvorm).Title("Contractvorm").ClientTemplate("<span title='${Contractvorm}'>${Contractvorm}</span>").Width(200).Filterable(ftb => ftb.Multi(true)); ;
            columns.Bound(d => d.Run1.Netto).Title("Periode 1").HeaderTemplate("<div class ='periode1jaar'></div >").Filterable(true).HtmlAttributes(new { style = "text-align:right;" }).Width(220).ClientTemplate("# if (Run1.Netto != 0) { #  #= kendo.toString(Run1.Netto, 'n2') #  # } #").HeaderHtmlAttributes(new { style = "text-align:right;" });
            columns.Bound(d => d.Run2.Netto).Title("Periode 2").HeaderTemplate("<div class ='periode2jaar'></div >").Filterable(true).HtmlAttributes(new { style = "text-align:right;" }).Width(220).ClientTemplate("# if (Run2.Netto != 0) { #  #= kendo.toString(Run2.Netto, 'n2') #  # } #").HeaderHtmlAttributes(new { style = "text-align:right;" });

            columns.Bound(d => d.VerschilPercentage).Filterable(false).HtmlAttributes(new { style = "text-align:right;", @class = "NettoVergelijkingVerschil" }).ClientTemplate("# if (VerschilPercentage != 0) { # %   #= kendo.toString(VerschilPercentage, 'n2') #  # } else { # % 0  # } #").Width(165).Format("{0:P}").HeaderHtmlAttributes(new { style = "text-align:right;" }).Hidden();

            columns.Bound(d => d.Verschil).Filterable(false).HtmlAttributes(new { style = "text-align:right;", @class = "NettoVergelijkingVerschil" }).ClientTemplate("# if (Verschil != 0) { #  € #= kendo.toString(Verschil, 'n2') #    # } else { # €  0,00  # } #").Width(165).Format("{0: #.00}").HeaderHtmlAttributes(new { style = "text-align:right;" });
        })
    .Filterable(f => f.Mode(GridFilterMode.Menu))
    .Sortable()
    .Events(e => e.DataBound("OngridDatabound"))
    .Pageable(pager => pager.PageSizes(new List<object>
        { 25, 50, 100, 200, 500 }))
        .ClientDetailTemplateId("overzicht-grid-details")
        .Excel(e => e.AllPages(true))
        .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(50)
        .ServerOperation(false)
        .Model(model =>
        {
            model.Id(d => d.Id);
            model.Field(f => f.Naam);
            model.Field(f => f.Contractvorm);
            model.Field(f => f.Run1.Netto);
            model.Field(f => f.Run2.Netto);
        })
        .Read(r => r.Action("GetOverzicht", "NettoVergelijking").Data("getData"))
        .Sort(d => d.Add(a => a.Verschil).Descending()
        )
        )
)

so a user can select a periode(periode1) and that is the header.

and this is the javascript for it:

function OngridDatabound(e) {
        $('.periode1jaar').html($('#Periode1').data('kendoDropDownList').text());
        $('.periode2jaar').html($('#Periode2').data('kendoDropDownList').text());
    }

and this is the javascript for export naar excel

:

$("#export-excel").click(function () {

       $("#overzicht-grid").data("kendoGrid").saveAsExcel();
    })

But if I look in the excel sheet there is still standing in the header periode1 and not for example: januari 2017.

so what to change that the headers of the excel sheet take the correct headers?

Thank you.

I changed like this:

 $("#export-excel").click(function () {

        var grid = $("#overzicht-grid").getKendoGrid();
        var rows = [{
            cells: [
              { value: "Periode 1" }

            ]
        }];
        var trs = $("#overzicht-grid").find('td');
        for (var i = 0; i < trs.length; i++) {           
        }
        excelExport(rows)


        function excelExport(rows) {
            var workbook = new kendo.ooxml.Workbook({
                sheets: [
                  {
                      columns: [
                        { autoWidth: true },
                        { autoWidth: true }
                      ],
                      title: "Orders",
                      rows: rows
                  }
                ]
            });
            kendo.saveAs({ dataURI: workbook.toDataURL(), fileName: "Test.xlsx" });
        }
    })

But now I see again just Periode 1