如何添加一个导出功能Grid

如何添加一个导出功能Grid

本文介绍了如何添加一个导出功能Grid.Mvc,这样我可以导出当前搜索结果中脱颖而出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关于这一主题和grid.mvc使用过滤的结果在控制器,但无人接听。

There are few questions on this topic Handle a pre-filtered list with grid.mvc and grid.mvc use filtered Result in Controller but no answer.

我使用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 -

一旦用户在筛选网格中的数据我希望他们能够出口这出类拔萃。我已阅读源代码,但我看不到一个事件,我可以插入。

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的从当前URL添加所有(可选)参数

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

08-15 19:09