我遵循了这个指南,这是在另一篇文章中推荐的,但我无法让它工作.尝试重定向到/ 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> …Run Code Online (Sandbox Code Playgroud) Excel告诉我有无法读取的数据,因此在我说尝试恢复信息后,它会显示正确的数据.但是当我打开xlsx的文本文件时,我得到整个页面的所有html,而不仅仅是gridview(这可能是Excel正在讨论的不可读的内容).
这是我的代码:
public void ExcelDownload(object sender, EventArgs e)
{
DataSet _MailingListUsers = db.GetMailingList();
DataTable mailTable = _MailingListUsers.Tables[0];
DumpExcel(mailTable);
}
private void DumpExcel(DataTable tbl)
{
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Mailing List");
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A2"].LoadFromDataTable(tbl, false);
//Header Titles
ws.Cells["A1"].Value = "Employee Name";
ws.Cells["B1"].Value = "Email Address";
ws.Cells["C1"].Value = "Phone";
ws.Cells["D1"].Value = "Business Unit";
ws.Cells["E1"].Value = "Site";
ws.Cells["A1"].AutoFitColumns();
//Format …Run Code Online (Sandbox Code Playgroud)