我有ASP.net MVC 5应用程序,我想在excel中生成报告
目前我已经指定了一个路径,但我想要的是excel应该下载到不在路径上的客户端计算机上。
public void GenerateExcel()
{
Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Add(System.Reflection.Missing.Value);
Excel.Worksheet worksheet = workbook.ActiveSheet;
worksheet.Cells[1, 1] = "ID";
worksheet.Cells[1, 2] = "Full Name";
worksheet.Cells[1, 3] = "Position Title";
worksheet.Cells[1, 4] = "Unit";
worksheet.Cells[1, 5] = "Mobile";
worksheet.Cells[1, 6] = "Email";
worksheet.Cells[1, 7] = "Supervisor Email";
worksheet.Cells[1, 8] = "Date and Time of Travel";
worksheet.Cells[1, 9] = "Type of Trip";
worksheet.Cells[1, 10] = "Distination";
var query = from v in db.MyContextVR
join t in db.MyTripTypeContext on v.TypeOfTripId equals t.Id
select new VRsVM
{
Id = v.Id,
FullName = v.FullName,
PostitionTitle = v.PostitionTitle,
Unit = v.Unit,
Mobile = v.Mobile,
Email = v.Email,
SupervisorEmail = v.SupervisorEmail,
DateAndTimeOfTravel = v.DateAndTimeOfTravel,
TripName = t.TripName,
Distination = v.Distination
};
int row = 2;
foreach (var item in query.ToList())
{
worksheet.Cells[row, 1] = item.Id;
worksheet.Cells[row, 2] = item.FullName;
worksheet.Cells[row, 3] = item.PostitionTitle;
worksheet.Cells[row, 4] = item.Unit;
worksheet.Cells[row, 5] = item.Mobile;
worksheet.Cells[row, 6] = item.Email;
worksheet.Cells[row, 7] = item.SupervisorEmail;
worksheet.Cells[row, 8] = item.DateAndTimeOfTravel;
worksheet.Cells[row, 9] = item.TripName;
worksheet.Cells[row, 10] = item.Distination;
row++;
}
workbook.SaveAs("D:\\tempex/myreport.xlsx");
workbook.Close();
}
答案 0 :(得分:1)
假设您已经制作了Excel电子表格:
public HttpResponseMessage GetExcel()
{
var workbook = yourExcel; //Whatever you do to create one.
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "Excel download");
MemoryStream myStream = new MemoryStream();
workbook.Write(myStream); // This is for NPOI, you'll have to do the right one for your tool (which is unspecified currently).
response.Content = new ByteArrayContent(myStream.ToArray());
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileName
};
myStream.Close();
}
不要忘记包含自定义try-catch逻辑!
答案 1 :(得分:1)
将excel文件保存到服务器中,然后将其转换为字节数组,然后通过File()辅助方法将其返回到客户端,作为来自asp.net mvc操作方法的ActionResult / FileResult
public ActionResult ExportToExcel(...) {
your codes....
string tempPath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond + "_temp";
workbook.SaveAs(tempPath, workbook.FileFormat);
tempPath = workbook.FullName;
workbook.Close();
byte[] result = File.ReadAllBytes(tempPath);
File.Delete(tempPath);
this.Response.AddHeader("Content-Disposition", "Employees.xls");
this.Response.ContentType = "application/vnd.ms-excel";
return File(result, "application/vnd.ms-excel");
}
答案 2 :(得分:0)
您可以使用EPPlus之类的内容在内存流中轻松创建Excel文档。之后,您可以使用content-disposition将其提供给客户端(作为下载)。
按照Corey Adler的这篇精彩教程:
Easy Excel Interaction with EPPlus
答案 3 :(得分:0)
在生成Excel文件时,你有几个选项,我个人使用免费的ClosedXml库得到了很好的结果,这个库也可以通过NuGet获得。我不会这么做,因为我觉得这不是问题的重点!
如果您想将Excel文件作为Mvc中的下载文件发送回客户端,则无论您选择生成库,都需要先将文件保存到流中以获取文件的数据
using (System.IO.MemoryStream outputStream = new System.IO.MemoryStream())
{
workbook.SaveAs(outputStream);
outputStream.Seek(0, SeekOrigin.Begin);
data = new byte[outputStream.Length];
outputStream.Read(data, 0, (int)outputStream.Length);
}
然后,一旦获得模板中的数据,您就可以从控制器操作中将其发送回用户:
public ActionResult Download() {
byte[] data = GetExcel();
return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Filename.xlsx")
}
修改强>
还有一个File方法的重载,它接受一个Stream,因此如果你已经有一个文件流,你可以跳过将它转换为字节数组