Reputation: 576
I was trying to download list of data as excel file.From various posts in stackoverflow I found some help, now I m getting the data in browser as response.Now How to save(download) it in my disk as Excel file?
Note: my server does not have any MS Office installed.
string attachment = "attachment; filename=city.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
//string tab = "";
//Response.WriteFile("hello World");
foreach (TimesAndMovementsModel item in searchParam)
{
Response.Write("\t" + item.CustomerName + "\t" + item.DurationInMinutes + "\t" + item.StartDate);
Response.Write("\n");
}
}
Response.End();
Thanks.
Upvotes: 0
Views: 1949
Reputation: 498
You can do something like this:
public FileResult Download()
{
byte[] fileBytes = System.IO.File.ReadAllBytes("c:\folder\myfile.ext");
string fileName = "myfile.ext";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
You can also read this article as a reference. Hope it helps!
Upvotes: 1