Search code examples
c#asp.net-mvcexport-to-excel

Export to excel does not work on server


I have an app that runs on two servers, one is QA and the other one is Production. The problem is, when you want to open the excel that it is downloaded by the app in QA or PRD it opens Excel but nothing happens, just blank.

There was a time where the app could export information to an Excel file, not anymore since yesterday the method is as follows:

[Autorizacion]
public ActionResult ExportToExcelReports()
{
    IList<DTOReport> reports = null;

    try
    {
        reports = BusinessLogic.Report.GetReports(SessionHelper.Site.IdSite); 

        var title = "Reports";

        var report = new System.Data.DataTable(title);

        report.Columns.Add("Blah1", typeof(string));
        report.Columns.Add("Blah2", typeof(string));
        report.Columns.Add("Blah3", typeof(string));
        report.Columns.Add("Blah4", typeof(string));
        report.Columns.Add("Blah5", typeof(string));
        report.Columns.Add("Blah6", typeof(string));
        report.Columns.Add("Blah7", typeof(string));
        report.Columns.Add("Blah8", typeof(string));
        report.Columns.Add("Blah9", typeof(string));
        report.Columns.Add("Blah10", typeof(string));

        foreach (var item in reports)
        {
            var brandText = "";

            foreach (var brand in item.Brands)
            {
                brandText = brandText + (brandText != "" ? "," : "") + brand.Name;
            }

            report.Rows.Add(item.Name, item.Description, item.DateCreated, item.Type, item.Category, item.Latitude, item.Longitude, item.Locality, item.Province, brandText);
        }

        var grid = new GridView();
        grid.DataSource = report;
        grid.DataBind();

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=" + title + ".xls");
        Response.ContentType = "application/ms-excel";

        Response.Charset = string.Empty;
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        grid.RenderControl(htw);

        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();    
    }
    catch (Exception ex)
    {
        LogError(ex);
    }

    return View("Index", reports);
}

The thing is, This works on my local machine, I open the project I compile it, I run it and it works flawlesly with the same information or Data from Production, the same happens with QA, because they share the same server-database.

The GetReports is working perfectly, it doesnt enter in a try catch, it just works in my development environment. Something related to excel in the servers maybe?

Could it be something related to IIS or the servers where the app is deployed?.


Solution

  • Ok I have found my answer in this link: Opening excel files from the internet opens a blank excel window

    The problem arose when Windows Installed this update: Windows Update KB3115130 (Excel 2010) - https://www.microsoft.com/en-us/download/details.aspx?id=52809

    "A security vulnerability exists in Microsoft Excel 2010 32-Bit Edition that could allow arbitrary code to run when a maliciously modified file is opened. This update resolves that vulnerability."

    The solution is either (not recommended) uninstall that update or: Go into the properties of the file (R click - properties) Click 'Unblock' Click 'Apply' (Answered by Josh Bucklen)