MVC linq查询导出到Excel

时间:2015-06-30 09:14:15

标签: asp.net-mvc excel linq export

我有一个控制器,我在其中使用linq查询并将其输入保存到列表中并显示在视图中,现在我希望还可以选择将查询输入导出到excel中。 到目前为止,我只看到了可以将表的内容导出到excel的教程,但是我的查询没有保存在表中,所以我想导出查询内容。 这就是我的控制器的样子:

public ActionResult ControllingTagestouren(string sortorder, string tName, string sName)
    {
var query1 = from q in query
                     where q.Tagestourname == tName || tName == null || tName == ""
                     where q.Standardtourname == sName || sName == null || sName == ""
                     select q;

        List<Tagestourauswertung> TagesAuswertung = new List<Tagestourauswertung>();
        TagesAuswertung.AddRange(query1);
        return View(TagesAuswertung);

这是我到目前为止所发现的但它不起作用...... 它在同一个控制器类中

public ActionResult ExportData()
    {
        ControllingController cc = new ControllingController();
        GridView gv = new GridView();
        gv.DataSource = cc.ControllingTagestouren(null, null, null);
        gv.DataBind();
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=Marklist.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("ControllingTagestouren");
    }

0 个答案:

没有答案