导出到excel不能在服务器上工作

本文关键字:服务器 工作 不能 excel | 更新日期: 2023-09-27 18:04:03

我有一个在两个服务器上运行的应用程序,一个是QA,另一个是生产。问题是,当你想打开excel时,它是由QA或PRD的应用程序下载的,它打开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);
}

事情是,这在我的本地机器上工作,我打开项目,我编译它,我运行它,它与来自生产的相同信息或数据完美地工作,同样发生在QA上,因为它们共享相同的服务器数据库。

GetReports工作得很完美,它没有进入try catch,它只是在我的开发环境中工作。也许是与服务器中的excel相关的东西?

导出到excel不能在服务器上工作

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

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

你可以试试上面的链接。因为它以前可以工作,也许Excel的设置已经改变了。从本质上讲,在链接中有三件事要尝试:

  1. 在Excel中取消选中"忽略DDE"选项,以便外部应用程序那就叫它不要忽视。
  2. 检查文件关联各种Excel文件类型(.xls, .xlsx, .xlsm等)的扩展名
  3. 修复office excel,以防它不能正常工作

好的,我在这个链接中找到了我的答案:从因特网打开excel文件会打开一个空白的excel窗口

当Windows安装此更新时出现问题:Windows Update KB3115130 (Excel 2010) - https://www.microsoft.com/en-us/download/details.aspx?id=52809

" Microsoft Excel 2010 32位版存在安全漏洞,当恶意修改的文件被打开时,可能允许任意代码运行。此更新解决了该漏洞。"

解决方案是(不推荐)卸载该更新或:进入文件的属性(点击R键- properties)点击"开启"点击"应用"