I have ASP.net MVC 5 Application, I want to generate report in excel
currently I have specified a path, but what I want is that excel should be downloaded in client computer not on the path.
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();
}