搜索结果导出到MVC 4练成 [英] Export search results to excel in MVC 4

查看:133
本文介绍了搜索结果导出到MVC 4练成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器。当我搜索一个特定的等级,我得到一个列表。我想要的是导出这个结果到Excel。我有我的导出按钮正常工作。但唯一的一点是,它会尝试一切导出数据库到Excel,当我点击它。我要的是仅导出的结果。任何想法?

I have following controller. When i search for a specific grade i get a list. What i want is to export this result to excel. I have my export button works fine. But the only thing is that it tries to export everything in db to excel when i click on it. What i want is to export only the results. Any idea?

public ActionResult Index(string searchBy, string search)
    {
        if (!String.IsNullOrEmpty(searchBy) && !String.IsNullOrEmpty(search))
        {
            if (searchBy == "ID")
            {
                return View(db.students.Where(x => x.id==search).ToList());
            }
            else if (searchBy == "grade")
            {
                return View(db.students.Where(x => x.grade == search).ToList());
            }
        else
        {
            return View(db.students.Take(0));
        }
    }

     public ActionResult ExportData()
    {
        GridView gv = new GridView();
        gv.DataSource = db.students.ToList();
        gv.DataBind();
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=students.xls");
        Response.ContentType = "application/ms-excel";
        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("Home");
    }

这是在我的索引视图部分:

And this is the part in my index view:

  @using (Html.BeginForm("ExportData", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <table>
                    <tr>
                        <td></td>
                        <td>
                            <input type="submit" name="Export" id="Export" value="Export" />
                        </td>
                    </tr>

                </table>
            }

推荐答案

ExportData()动作就是从检索 db.students每个结果,它不知道什么是在你的看法。结果
假设你的学生名单有你想要的所有数据,您既可以在整个列表张贴到你的行动,例如

Your ExportData() action is just retrieving every result from db.students, it doesn't know what is in your view.
Assuming your list of students has all the data you want, you could either post the entire list to your action, e.g.

public ActionResult ExportData(List<Student> students)
{
    GridView gv = new GridView();
    gv.DataSource = students;
    gv.DataBind();
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=students.xls");
    Response.ContentType = "application/ms-excel";
    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("Home");
}  

或者,你可以通过你的搜索参数,并相应地筛选数据库,例如。

Or you could pass your search parameters and filter the database accordingly, e.g.

public ActionResult ExportData(string searchBy, string search)
{

    GridView gv = new GridView();
    if (!String.IsNullOrEmpty(searchBy) && !String.IsNullOrEmpty(search))
    {
        if (searchBy == "ID")
        {
            gv.DataSource = db.students.Where(x => x.id==search).ToList();
        }
        else if (searchBy == "grade")
        {
            gv.DataSource = db.students.Where(x => x.grade == search).ToList();
        }
    }
    gv.DataBind();
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=students.xls");
    Response.ContentType = "application/ms-excel";
    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("Home");
}

和不要忘记从你的观点张贴必要的数据。

And don't forget to post the necessary data from your view.

这篇关于搜索结果导出到MVC 4练成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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