Situation
I have encountered an issue while generating an Excel file using OfficeOpenXml in C#. The generated file is not being returned to the browser.
Any thoughts on why this might be happening?
Code Snippets
[C#] :
public ActionResult ExportToExcel(string[] emails)
{
using (var ep = new ExcelPackage())
{
var ws = ep.Workbook.Worksheets.Add("Contacts");
for (var i = 0; i < emails.Length; i++)
{
ws.Cells[i + 1, 1].Value = emails[i];
}
Byte[] bytes = ep.GetAsByteArray();
return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "Contacts.xls" };
}
}
[JavaScript] :
$('#contacts-excel-btn').click(function () {
var emails = [],
uniqueEmails = [];
$('.email-td').each(function () {
var text = $(this).text();
if (text) {
emails.push(text);
}
});
$.each(emails, function (i, email) {
if ($.inArray(email, uniqueEmails) === -1) {
uniqueEmails.push(email);
}
});
if (uniqueEmails[0]) {
$.ajax({
type: 'POST',
url: '/Contact/ExportToExcel',
dataType: 'json',
traditional: true,
data: { emails: uniqueEmails }
});
}
});