Telerik MVC网格PDF导出无法正常工作

时间:2016-02-22 18:26:05

标签: c# asp.net-mvc pdf telerik

我正在测试网格,我试图将其导出为PDF和XLS。使用Excel几乎是直截了当,但当我尝试按照demo上提供的步骤时,它不起作用。当我点击导出按钮时,进度条会加载,当它完成任何事情时,操作永远不会被执行。

这是我的观点:

@model IEnumerable<ViewModels.TestViewModel>

@{
    ViewBag.Title = "Tests KendoUI";
}

<script src="~/Scripts/lib/jszip.min.js" type="text/javascript"></script>
<script src="~/Scripts/lib/pako.min.js" type="text/javascript"></script>

<style>
    /*
                Use the DejaVu Sans font for display and embedding in the PDF file.
                The standard PDF fonts have no support for Unicode characters.
            */
    .k-grid {
        font-family: "DejaVu Sans", "Arial", sans-serif;
    }

    /* Hide the Grid header and pager during export */
    .k-pdf-export .k-grid-toolbar,
    .k-pdf-export .k-pager-wrap,
    .k-pdf-export a.k-button.k-button-icontext,
    .k-pdf-export .k-filter-row,
    .k-pdf-export .k-grouping-header,
    .k-pdf-export .k-grid tr td:last-child {
        display: none !important;
    }
</style>

@(Html.Kendo().Grid(Model)
      .Name("grid")
      .DataSource(dataSource => dataSource
          .Ajax()
          .Read(read => read.Action("Products_Read", "Home"))
          .Aggregates(aggregates => aggregates.Add(p => p.oper_monto).Sum())
          )
      .Columns(columns =>
      {
        columns.Bound(p => p.oper_numero).Hidden(true);
        columns.Bound(p => p.oper_monto).ClientFooterTemplate("Total: #=sum#");
        columns.Bound(p => p.cpto_codigo);
      })
      .Excel(excel => excel
        .FileName("Reporte.xlsx")
        .Filterable(true)
        .AllPages(true)
        .ProxyURL(Url.Action("Excel_Export_Save", "Home"))
      )
      .Pdf(pdf => pdf
            .AllPages()
            .FileName("Reporte.pdf")
            .ProxyURL(Url.Action("Pdf_Export_Save", "Home"))
        )
      .ColumnMenu()
      .ToolBar(t => t.Excel().Text("Exportar a Excel"))
      .ToolBar(t => t.Pdf().Text("Exportar a PDF"))
      .ToolBar(toolBar => 
                    toolBar.Custom()
                        .Text("Guardar Preferencias")
                        .HtmlAttributes(new { id = "save" })
      )
      .ToolBar(toolBar =>
                    toolBar.Custom()
                        .Text("Cargar Preferencias")
                        .HtmlAttributes(new { id = "load" })
      )                      
      .Filterable(ftb => ftb.Mode(GridFilterMode.Menu))
)

<script>

    $(function () {
        var grid = $("#grid").data("kendoGrid");

        $("#save").click(function (e) {
            e.preventDefault();
            localStorage["kendo-grid-options"] = kendo.stringify(grid.getOptions());
        });

        $("#load").click(function (e) {
            e.preventDefault();
            var options = localStorage["kendo-grid-options"];
            if (options) {
                grid.setOptions(JSON.parse(options));
            }
        });
    });
</script>

这是我的控制器动作:

[HttpPost]
        public ActionResult Pdf_Export_Save(string contentType, string base64, string fileName)
        {
            var fileContents = Convert.FromBase64String(base64);

            return File(fileContents, contentType, fileName);
        }

提前致谢。

1 个答案:

答案 0 :(得分:0)

好吧,导出函数使用了一个名为pako的js,你可以找到它here,但是如果你右键单击并下载文件,它将会失败并且无论如何都要下载它,但是它里面的代码赢了&#39;是必要的JavaScript。所以,我必须输入文件,复制所有文本并将其粘贴到我的本地文件中。它就像一个魅力。