c# - Excel sheet does not display exported chart image - ASP.NET -


i have web page in website allows me download chart page , add excel sheet. however, red x button words: "this image cannot displayed" shown in place of chart. have tried many solutions online of them show same outcome.

here codes:

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

any form of appreciated. also, note have added required codes in web.config.

we have tried program , here it’s working fine. excel downloaded along image. have installed in iis , tried many client machine , working fine.

not sure what’s going on end. problem might wrong image path reference cause problem. print image url , make sure whether correct or not.

alternate method: instead of using html tags inside excel cell use xml closing method render image inside excel cell.

here have added sample code closed xml methods render image in excel. use code need refer following dlls 1. closedxml.dll 2. windowscore.dll 3. documentformat.openxml.dll 4. presentationcore.dll

reference link closed xml methods: 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();            }  } 

please check , let me know comments.


Comments