2

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?.

ekad
  • 14,436
  • 26
  • 44
  • 46
user3442470
  • 409
  • 2
  • 6
  • 19
  • 1
    There any error code when it does not return the excel? – juanvan Aug 26 '16 at 12:32
  • Sorry I put the info in bold text, forgot to put what happens when download the file in excel. – user3442470 Aug 26 '16 at 12:35
  • since you're essentially exporting HTML into this file (which excel can read), can you open the file in a text editor and see if there's any content in it? Maybe it's corrupted in some way – ADyson Aug 26 '16 at 12:42
  • Yes, I opened it with Notepad++ and I can see the html, all tags are there and is correctly formatted. – user3442470 Aug 26 '16 at 12:46
  • Possibly add MIME Types to your web.config and/or IIS - Maybe your local cpu is using just `.xls` and not `xlsx`. **Also note that the MIME Type for `.xls` is `application/vnd.ms-excel`, not `application/ms-excel` ** http://stackoverflow.com/questions/4212861/what-is-a-correct-mime-type-for-docx-pptx-etc – Rob Scott Aug 26 '16 at 12:56
  • Im testing application/vnd.ms-excel right now. – user3442470 Aug 26 '16 at 13:12
  • Ok, I tried application/vnd.ms.excel but it is the same. Now... one of my coworker opened both excel, the one created by QA/PRD and the DEV one. We both have the same Excel and the same version (2010 V14.0.7172.5000 (32 bits)) Now I grabbed all the content of the one that does not open and paste it on the one that works (opened it in Notepad++ and grab the html and paste it) and now it opens... – user3442470 Aug 26 '16 at 13:28

2 Answers2

0

http://www.computerhope.com/issues/ch001123.htm

You could try the above link. Since it used to work, perhaps Excel's settings have been changed. Essentially in the link there are three things to try:

  1. Uncheck "Ignore DDE" option in Excel so that External applications that call it are not ignored.
  2. Check file associations for the extensions for various Excel file types (.xls, .xlsx, .xlsm, etc.)
  3. Repair office excel in case it is not functioning properly
elsausmc
  • 111
  • 6
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – ekad Aug 26 '16 at 12:54
0

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)

Community
  • 1
  • 1
user3442470
  • 409
  • 2
  • 6
  • 19