导出到 excel 在服务器上不起作用

Export to excel does not work on server

我有一个应用 运行 在两台服务器上运行,一台是 QA,另一台是生产。 问题是,当您想要打开由 QA 或 PRD 中的应用程序下载的 excel 时,它会打开 Excel 但没有任何反应,只是一片空白。

曾经有一段时间应用程序可以将信息导出到Excel文件,从昨天开始就没有了方法如下:

[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);
}

事情是,这在我的本地机器上工作,我打开项目我编译它,我 运行 它和来自生产的相同信息或数据完美地工作,质量检查也是如此,因为他们共享相同的服务器数据库。

GetReports 运行良好,它没有进入 try catch,它只在我的开发环境中运行。可能与服务器中的 excel 有关?

是否与 IIS 或部署应用程序的服务器有关?

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

你可以试试上面的link。由于它曾经有效,也许 Excel 的设置已更改。 基本上在 link 中可以尝试三件事:

  1. 取消选中 Excel 中的 "Ignore DDE" 选项,以便外部应用程序 调用它不会被忽略。
  2. 检查文件关联 各种 Excel 文件类型(.xls、.xlsx、.xlsm 等)的扩展名
  3. 维修办公室excel以防它不能正常工作

好的,我在 link 中找到了答案:

Windows 安装此更新时出现问题: Windows 更新 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."

解决方案是(不推荐)卸载该更新或: 进入文件的属性(R 单击 - 属性) 点击 'Unblock' 单击 'Apply' (由乔什·巴克伦回答)