如何导出到Excel?

我遵循了本指南,该指南是另一篇关于此指南的建议,但我无法完全使它起作用。我尝试重定向到/ LogModelsController / ExportData时收到404错误,据我所知,这正是我应该做的。


_LogPartialLayout.cshtml


@using (Html.BeginForm("ExportData", "LogModelsController", FormMethod.Post, new { enctype = "multipart/form-data" }))

{

    <table class="table">

        <tr>

            <th>

                @Html.ActionLink("message", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })


            </th>

            <th>

                @Html.ActionLink("timestamp", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })

            </th>

            <th>

                @Html.ActionLink("level", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })

            </th>


        </tr>


        @foreach (var item in Model)

        {

            <tr>

                <td>

                    @Html.DisplayFor(modelItem => item.message)

                </td>

                <td>

                    @Html.DisplayFor(modelItem => item.timeStamp)

                </td>

                <td>

                    @Html.DisplayFor(modelItem => item.level)

                </td>

            </tr>

        }

    </table>

    <input type="submit" name="Export" id="Export" value="Export" />

}

http://img.mukewang.com/5daf00c90001a8b907760653.jpg

LogModelsController.cs


public ActionResult ExportData()

        {

            GridView gv = new GridView();

            gv.DataSource = db.Log.ToList();

            gv.DataBind();

            Response.ClearContent();

            Response.Buffer = true;

            Response.AddHeader("content-disposition", "attachment; filename=Loglist.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();


TD; DR:如何正确将此表导出到Excel?


奖励:它也可以与PagedList一起使用吗?


ITMISS
浏览 437回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP