【问题标题】:Excel sheet does not display exported chart image - ASP.NETExcel 工作表不显示导出的图表图像 - ASP.NET
【发布时间】:2016-01-29 04:13:26
【问题描述】:

我的网站上有一个网页,允许我从该页面下载图表并将其添加到 Excel 工作表中。但是,在图表的位置显示了一个带有“当前无法显示此图像”字样的红色 x 按钮。我在网上尝试了很多解决方案,但都显示了相同的结果。

这是我的代码:

    protected void ExcelDl_Click(object sender, EventArgs e) {
        string tmpChartName = "test2.jpg";
        string imgPath = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName;
        Chart1.SaveImage(imgPath);
        string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName);

        Response.Clear();
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        string headerTable = @"<Table><tr><td><img src='" + imgPath2 + @"' \></td></tr></Table>";
        Response.Write(headerTable);
        Response.Write(sw.ToString());
        Response.End();
    }

我们将不胜感激任何形式的帮助。另外,请注意我已经在我的 Web.config 中添加了所需的代码。

【问题讨论】:

    标签: c# asp.net excel export-to-excel bar-chart


    【解决方案1】:

    我们已经尝试过您的程序,它运行良好。随图像一起下载的 Excel。我已经在 iis 中安装并尝试了许多客户端机器,它工作正常。

    不知道你到底发生了什么。该问题可能是错误的图像路径引用会导致此问题。打印图片网址并确认是否正确。

    替代方法:在 excel 单元格内不使用 html 标签,而是使用 XML 关闭方法在 excel 单元格内呈现图像。

    在这里,我添加了用于在 excel 中呈现图像的封闭 XML 方法的示例代码。要使用此代码需要参考以下 DLL 1. ClosedXML.dll 2.WindowsCore.dll 3. DocumentFormat.OpenXML.dll 4. PresentationCore.dll

    封闭式 XML 方法的参考链接:https://closedxml.codeplex.com/

          using (XLWorkbook wb = new XLWorkbook())
            {
        IXLWorksheet WorkSheet = new IXLWorksheet();
          string LogoPath = Server.MapPath("~/App_Themes/Images/logonew.png");
          System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(LogoPath);
          if (bitmap != null)
             {
                var stream = new System.IO.MemoryStream();
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
                if (stream != null)
                {
                    stream.Position = 0;
    
                    XLPicture pic = new XLPicture
                    {
                         NoChangeAspect = true,
                         NoMove = true,
                         NoResize = true,
                         ImageStream = stream,
                         Name = "Logo",
                         EMUOffsetX = 4,
                         EMUOffsetY = 6
                    };
    
                    XLMarker fMark = new XLMarker
                    {
                        ColumnId = 1,
                        RowId = 1
                     };
    
                     pic.AddMarker(fMark);
    
                    WorkSheet.AddPicture(pic);
                 }
              }
    
               Response.Clear();
               Response.Buffer = true;
               Response.Charset = "";
               Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
               Response.AddHeader("content-disposition", "attachment;filename=test.xls");
    
               using (MemoryStream MyMemoryStream = new MemoryStream())
               {
                wb.SaveAs(MyMemoryStream);
                MyMemoryStream.WriteTo(Response.OutputStream);
                Response.Flush();
                Response.End();
               }
    
    }
    

    请检查并告诉我您的 cmets。

    【讨论】:

    • 嗨@Mayil,很抱歉回复晚了。图片 URL 可以正常打开,但是在 Excel 中打开时,问题出现了。我会试试这个,让你知道结果。
    • 感谢您的更新。试着让我知道你的 cmets。
    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多