user3289773
user3289773

Reputation: 77

ClosedXML: Export To Excel not downloading the File MVC 4

I have a List<T>, which I convert to a Datatable and then export that Datatable to Excel via Kingsoft-Spreadsheets using ClosedXML library.

However, for some reason the code is not working and the sheet is not getting downloaded. I am getting strange symbolic screen on my MVC view. I have attached the image for reference. Any help much appreciated.

enter image description here

Upvotes: 4

Views: 4864

Answers (3)

SANJAY SHARMA
SANJAY SHARMA

Reputation: 1

You should avoid to call ajax :

function ExportSPReport() {
    window.location = '@Url.Action("ExportSPReport", "Report")';
    @*$.ajax({
            url: '@Url.Action("ExportSPReport", "Report")',
            type: "GET",
            success: function (data) {
                //alert(data);
                //$("#downloadFile").attr("href", data);
                //document.getElementById('downloadFile').click();
            },
            error: function (reponse) {
            }
        });
    }*@
}

Upvotes: -1

user3289773
user3289773

Reputation: 77

My bad, I was using ajax post back on Export Excel button click,there was no page postback.Acutally full page post back is required. I just added window.location.href with respective controller and action method and voila..its working fine.

Upvotes: 1

Raidri
Raidri

Reputation: 17560

It looks like the Excel file is created correctly but your browser tries to open it like a plain text file. Set the content type and the content disposition header for the response correctly like this:

Response.AddHeader("content-disposition", "attachment; filename=" + myName);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // or "application/vnd.ms-excel"

For a full working code example see this question and its answers.

Upvotes: 1

Related Questions