如何导出到Excel?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何导出到Excel?相关的知识,希望对你有一定的参考价值。
我跟随this guide,这是在另一篇文章中推荐的,但我无法让它工作。尝试重定向到/ 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" />
}
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();
Response.End();
return RedirectToAction("~/LogModels/Index");
}
LogModel.cs
namespace ASDMVC.Models
{
[Table("Log")]
public class LogModel
{
[Key]
public long id { get; set; }
public string message { get; set; }
public DateTime timeStamp { get; set; }
public string level { get; set; }
public int customerId { get; set; }
}
public class LogDBContext:DbContext
{
public LogDBContext() : base("mysqlConnection")
{
}
public DbSet <LogModel> Log { get; set; }
}
}
TD; DR:如何将此表正确导出到Excel?
额外奖励:它也适用于PagedList吗?
编辑3 - 已解决
Index.cshtml:
@using (Html.BeginForm("ExportData", "LogModels"))
{
<input type="submit" name="Export" id="Export" value="Export" />
}
LogModelsController.cs
public ActionResult ExportData()
{
DataTable dataTable = GetData();
using (ExcelPackage package = new ExcelPackage())
{
var ws = package.Workbook.Worksheets.Add("My Sheet");
//true generates headers
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
var stream = new MemoryStream();
package.SaveAs(stream);
string fileName = "myfilename.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
stream.Position = 0;
return File(stream, contentType, fileName);
}
}
public DataTable GetData()
{
DataTable dt = new DataTable();
if (ModelState.IsValid)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString))
{
using (SqlCommand comm = conn.CreateCommand())
{
comm.Parameters.AddWithValue("@val1", Session["myID"]);
comm.CommandText = "SELECT * FROM dbo.Log WHERE CustomerId = @val1";
try
{
conn.Open();
dt.Load(comm.ExecuteReader());
}
catch (SqlException e)
{
throw new Exception(e.ToString());
}
}
}
}
return dt;
}
我在CommandText中犯了一个错误并输入“Id”而不是“CustomerId” - 它现在返回Excel文件中的整个表。
该指南未显示如何创建Excel文件,它显示了一个使用Table创建HTML文件的hack。 Excel将尝试使用默认设置导入带有表的csv或HTML文件,这些设置通常会失败。
使用像EPPlus这样的库创建一个合适的Excel文件要容易得多。我确信这已经得到了回答,但我找不到一个答案,显示如何create an Excel file from data和设置proper content type为xlsx
文件。
EPPlus提供LoadFromDataTable和LoadFromCollection方法,以使用从DataTable或通用集合中提取的数据填充Excel范围。列或属性名称分别用作标题。
public ActionResult ExportData()
{
//Somehow, load data to a DataTable
using (ExcelPackage package = new ExcelPackage())
{
var ws = package.Workbook.Worksheets.Add("My Sheet");
//true generates headers
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
var stream = new MemoryStream();
package.SaveAs(stream);
string fileName = "myfilename.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
stream.Position = 0;
return File(stream, contentType, fileName);
}
}
LoadFromDataTable或LoadFromCollection返回Excel单元格范围,可用于格式化表格:
var range=ws.Cells["A1"].LoadFromDataTable(table);
range.Style.Numberformat.Format = "#,##0.00";
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
您还可以将表格样式应用于范围,或将所需样式传递给LoadFromDataTable
或LoadFromCollection
,例如:
ws.Cells[1,1].LoadFromDataTable(table, true, TableStyles.Light1);
图书馆的Githup wiki展示了如何使用format the result,生成公式,数据透视表,处理表等。
如果要创建相对较大的工作表,则保存到MemoryStream可能会出现问题。将相同的数据写入两次,一次写入MemoryStream然后将其复制到输出,会浪费周期。直接写入MVC动作的输出流是一种不好的做法。诀窍是创建一个自定义FileResult来处理EPPlus包并返回它而不是File
返回的FileResult,例如:
public class EpplusResult:FileResult
{
public EpplusResult(ExcelPackage package)
: base("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
if (package == null)
{
throw new ArgumentNullException("package");
}
Package = package;
}
public ExcelPackage Package { get; private set; }
protected override void WriteFile(HttpResponseBase response)
{
// grab chunks of data and write to the output stream
Stream outputStream = response.OutputStream;
using (Package)
{
Package.SaveAs(outputStream);
}
}
}
这允许您编写以下操作:
public FileResult ExportData()
{
ExcelPackage package = new ExcelPackage();
var ws = package.Workbook.Worksheets.Add("My Sheet");
...
ws.Cells[1,1].LoadFromDataTable(table, true, TableStyles.Light1);
return new EpplusResult(package){FileDownloadName = "SomeFile.xlsx"};
}
不要创建基于HTML的Excel文件,它们有很多问题。
保证使用一个紧凑的库,如epplus。
public ActionResult ExportData()
{
using (ExcelPackage package = new ExcelPackage())
{
var ws = package.Workbook.Worksheets.Add("LogMessages");
//Headers
ws.Cells["A1"].Value = "Message";
ws.Cells["B1"].Value = "TimeStamp";
ws.Cells["C1"].Value = "Level";
var rowNumber=1;
foreach (var log in DbContext.Log)
{
ws.Cells[rowNumber, 1].Value = vehicle.message;
ws.Cells[rowNumber, 2].Value = vehicle.timeStamp;
ws.Cells[rowNumber, 3].Value = vehicle.level;
rowNumber++;
}
var stream = new MemoryStream();
package.SaveAs(stream);
string fileName = "logMessags.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
stream.Position = 0;
return File(stream, contentType, fileName);
}
}
我使用Panagiotis Kanavos代码结构来实现这个解决方案
看看这个,这个库包含一个ExcelResult,它将自动创建过程,并通过调用Excel()方法将Excel文件发送到客户端,就像Json()一样。
https://github.com/iabdelkareem/MVC-Excel-Extensions
以上是关于如何导出到Excel?的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET 导出EXCEL时如何不把一些不是EXCEL的符号一起导出到EXCEL?