如何将导出功能添加到Grid.Mvc中,以便将当前搜索结果导出到excel? [英] How do I add an export feature to Grid.Mvc so that I can export the current search results to excel?

查看:178
本文介绍了如何将导出功能添加到Grid.Mvc中,以便将当前搜索结果导出到excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此主题的几个问题 https:// stackoverflow。 com / questions / 23498689 / handle-a-pre-filtered-list-with-grid-mvc grid.mvc在控制器中使用过滤的结果,但没有回答。

There are few questions on this topic https://stackoverflow.com/questions/23498689/handle-a-pre-filtered-list-with-grid-mvc and grid.mvc use filtered Result in Controller but no answer.

我正在使用Grid.MVC的基本示例与MVC 4

I am using a basic example of the Grid.MVC with MVC 4

@using GridMvc.Html

@Html.Grid(Model,"_MyCustomGrid").Columns(columns =>
          {
                columns.Add(foo => foo.Title).Titled("Custom column title").SetWidth(110);
                columns.Add(foo => foo.Description).Sortable(true);
          }).WithPaging(20)

其中创建一个看起来像这样的网格 -

Which creates a grid that looks like this -

一旦用户过滤网格中的数据,我希望他们能够将其导出为ex​​cel。我已经阅读了源代码,但是看不到我可以插入的事件。

Once the user filters the data in the grid I would like them to be able to export this to excel. I have read the source but I can't see an event I can plug into.

推荐答案

我找到的解决方案是创建控制器和视图,返回具有XLS内容类型的表。不幸的是,当您在Excel中打开结果时,您会收到一条警告消息,因此它不完美,但它仍然可行。

The solution I have found was to create a controller and view that return a table with a XLS content type. Unfortunately you get an warning message when you open up the result in Excel so it's not perfect but it works.

例如

// Controllers/TaskController.cs
public virtual ActionResult Export () {
    var tasks = repository.GetAllTasks();

    Response.AddHeader("Content-Disposition", "attachment; filename=Tasks.xls");
    Response.ContentType = "application/ms-excel";

    return PartialView("Export", tasks);
}

// /Views/Task/Export.cshtml
@model IEnumerable<OpuzEleven.Models.Task>
@using GridMvc.Html;


@(Html.Grid(Model, "_GridExcel")
    .Named("TaskGrid")
    .AutoGenerateColumns()
    .Columns(columns => columns.Add(c => c.UserProfile.FullName).Titled("Created by"))
    .Sortable(true)
    .Filterable(true))

// /Shared/_GridExcel.cshtml
@using GridMvc.Columns
@model GridMvc.IGrid

@if (Model == null) { return; }
@if (Model.RenderOptions.RenderRowsOnly) {
  @RenderGridBody();
} else {
  <div class="grid-mvc" data-lang="@Model.Language" data-gridname="@Model.RenderOptions.GridName" data-selectable="@Model.RenderOptions.Selectable.ToString().ToLower()" data-multiplefilters="@Model.RenderOptions.AllowMultipleFilters.ToString().ToLower()">
    <div class="grid-wrap">
      <table class="table table-striped grid-table">
        @* Draw grid header *@
        <thead>
          @RenderGridHeader()
        </thead>
        <tbody>
          @RenderGridBody()
        </tbody>
      </table>
    </div>
  </div>
}

@helper RenderGridBody () {
  if (!Model.ItemsToDisplay.Any()) {
    <tr class="grid-empty-text">
      <td colspan="@Model.Columns.Count()">
        @Model.EmptyGridText
      </td>
    </tr>
  } else {
    foreach (object item in Model.ItemsToDisplay) {
      <tr class="grid-row @Model.GetRowCssClasses(item)">
        @foreach (GridMvc.Columns.IGridColumn column in Model.Columns) {
          @column.CellRenderer.Render(column, column.GetCell(item))
        }
      </tr>
    }
  }
}

@helper RenderGridHeader () {
  <tr>
    @foreach (GridMvc.Columns.IGridColumn column in Model.Columns) {
      <th>@column.Title</th>
    }
  </tr>
}

注意:链接传递用于文件管理器的参数,网格使用此 MVC ActionLink从当前网址添加所有(可选)参数

NB: So that links pass on the parameters used to filer the grid use this MVC ActionLink add all (optional) parameters from current url

这篇关于如何将导出功能添加到Grid.Mvc中,以便将当前搜索结果导出到excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆