Я работаю над добавлением функции экспорта на существующий сайт.Я создал файл Excel и могу вывести его в локальный каталог, но когда я пытаюсь отправить файл пользователю с помощью Response, ничего не происходит.Я просмотрел несколько статей о разных сетях и вопросы о стеке, и ни одна из них, похоже, не отличается от того, что у меня есть, или работы, когда я их применяю.
Вот код, который у меня сейчас есть:
public void Export () {
//Create instance of the database
ExportContext eDB = new ExportContext();
var query = (from x in eDB.EMPLOYEES
select x);
IEnumerable<EMPLOYEES> employees = query.ToList();
//Set filename to Table + date/time
string fileName = "EMPLOYEES" + DateTime.Now.ToShortTimeString() + ".xlsx";
//Create excel package
ExcelPackage excel = new ExcelPackage();
var worksheet = excel.Workbook.Worksheets.Add("Employees");
worksheet.Cells[1, 1].LoadFromCollection(employees, true);
using (var memoryStream = new MemoryStream()) {
//Output the file to the user via download
Response.Buffer = false;
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
excel.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
Response.End();
}
}
Любая помощь будет оценена.Спасибо.