Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Excel工作表不显示导出的图表图像-ASP.NET_C#_Asp.net_Excel_Export To Excel_Bar Chart - Fatal编程技术网

C# Excel工作表不显示导出的图表图像-ASP.NET

C# Excel工作表不显示导出的图表图像-ASP.NET,c#,asp.net,excel,export-to-excel,bar-chart,C#,Asp.net,Excel,Export To Excel,Bar Chart,我的网站中有一个网页,允许我从该网页下载图表并将其添加到excel工作表中。然而,一个红色的x按钮显示在图表的位置,上面写着:“此图像当前无法显示”。我在网上尝试了很多解决方案,但都显示出相同的结果 这是我的密码: protected void ExcelDl_Click(object sender, EventArgs e) { string tmpChartName = "test2.jpg"; string imgPath = HttpContext

我的网站中有一个网页,允许我从该网页下载图表并将其添加到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();
    }
protectedvoidexceldl\u单击(对象发送方,事件参数e){
字符串tmpChartName=“test2.jpg”;
字符串imgPath=HttpContext.Current.Request.PhysicalApplicationPath+tmpChartName;
图表1.保存图像(imgPath);
字符串imgPath2=Request.Url.GetLeftPart(UriPartial.Authority)+virtualPath.ToAbsolute(“~/”+tmpChartName);
Response.Clear();
Response.ContentType=“application/vnd.ms excel”;
AddHeader(“内容处置”、“附件;文件名=test.xls;”);
StringWriter sw=新的StringWriter();
HtmlTextWriter hw=新的HtmlTextWriter(sw);
字符串headerTable=@“”;
响应。写入(headerTable);
Response.Write(sw.ToString());
Response.End();
}

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

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

不知道你那边发生了什么。问题可能是错误的图像路径引用将导致此问题。打印图像URL并确保其是否正确

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

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

封闭XML方法的参考链接:


请检查并让我知道您的意见

嗨@Mayil,很抱歉回复晚了。图像URL可以很好地打开,但是当它在Excel中打开时,问题就出现了。我会试试这个,然后告诉你结果。谢谢你的更新。试着让我知道你的评论。
      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();
           }

}