【问题标题】:Export filtered results to excel in MVC 4 (no web form)将过滤结果导出到 MVC 4 中的 excel(无 Web 表单)
【发布时间】:2015-08-01 15:40:21
【问题描述】:

我无法将过滤后的结果导出到 Excel 文件。我只是在学习 ASP.Net 和 MVC。

我已经查看了建议 here,但我无法让它发挥作用。我不太清楚如何使用另一个link中提到的EditorTemplate。

目前,当我导出时,无论过滤器如何,所有数据都会被导出。如何在不使用 Web 表单的情况下将视图上显示的内容导出到 Excel 文件?

谢谢你..

这是我的观点,Index.cshtml:

@model IEnumerable<ExportToExcel.Models.Student>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
    @using (Html.BeginForm("Index","Student",FormMethod.Get))
    {
        <p>
            Name: @Html.TextBox("NameSearch")
            <input type="submit" value="Search" />
            @Html.ActionLink("Export to Excel","ExportToExcel")
        </p>
    }
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Marks)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Marks)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>
    </tr>
}

</table>

在我的控制器中,我实现了以下功能:

public ActionResult Index(string nameSearch)
{
    var students = from m in db.Students
                     select m;

    if (!String.IsNullOrEmpty(nameSearch))
    {
        students = students.Where(n => n.Name.Contains(nameSearch));
    }

    return View(students);
}

public ActionResult ExportToExcel()
{
    GridView gv = new GridView();

    //if (!String.IsNullOrEmpty(nameSearch))
    //{
    //    gv.DataSource = db.Students.Where(n => n.Name.Contains(nameSearch)).ToList();
    //}
    //else
    //{
    //    gv.DataSource = db.Students.ToList();
    //}
    gv.DataSource = db.Students.ToList();
    gv.DataBind();

    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/ms-excel";
    Response.AddHeader("content-disposition", "attachment;filename=StudentList.xls");
    Response.Charset = "";

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    gv.RenderControl(htw);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();

    return RedirectToAction("Index");
}

【问题讨论】:

    标签: asp.net-mvc excel asp.net-mvc-4 export-to-excel


    【解决方案1】:

    好的,找到答案了。原创link

    基本上,将搜索结果存储在会话中。然后,在导出期间从会话中检索列表。

    【讨论】:

      【解决方案2】:
           public ActionResult ExportExcel()
      
          {
      
              var EmployeeList = (List<Employee>)Session["EmployeeList"];
              //var EmployeeList = Session["EmployeeList"] as List<Product>;
              //var EmployeeList = Session["EmployeeList"];
      
              GridView grid = new GridView();
              grid.DataSource = EmployeeList;
      
              grid.DataBind();
      
              Response.ClearContent();
              Response.Buffer = true;
              Response.AddHeader("content-disposition", "attachment; filename=Employees.xls");
              Response.ContentType = "application/ms-excel";
      
              Response.Charset = "";
              StringWriter sw = new StringWriter();
              HtmlTextWriter htw = new HtmlTextWriter(sw);
      
              grid.RenderControl(htw);
      
              Response.Output.Write(sw.ToString());
              Response.Flush();
              Response.End();
      
              return RedirectToAction("Index");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-18
        • 2018-09-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多