0

I return HTML content and display in excel file in ASP.NET MVC.

My question is about img source.Img source does not work.

My Code:

public ActionResult ExportData()
{
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");
    var stringWrite = new StringWriter();
    var htmlWrite = new HtmlTextWriter(stringWrite);

    var headerTable = @"<Table><tr><td><img src=""‪C:\Users\xx\Desktop\Export\Export\Images\orderedList0.png"" \></td></tr></Table>";

    Response.Write(headerTable);
    Response.Write(stringWrite.ToString());
    Response.End();

    return RedirectToAction("StudentDetails");
}

Above code exports excell file without problem. I only have problem about displaying image. Image can not appear exactly. There is a problem about img source.

What is the correct way for img src for my code?

1
  • Always use relative url as much as possible as it saves you the hassle of changing lines while deploying.Use something like Fiddler to check the correct url. Commented Apr 29, 2014 at 8:53

1 Answer 1

0

Please keep in mind the path to the image you are embedding into Excel is an absolute path which MUST EXIST on the client's machine at that exact path.

I assume you get an image placeholder within the workbook? If so, it's a path problem. You will have to make sure the path points correctly to a file which exists at that exact path on that exact machine.

A much better idea would be to alter the src tag to be relative as Ash suggested and host the image from your webserver

var headerTable = @"<Table><tr><td><img src=""fullyqualifiedurltoimagefileonserver.png"" \></td></tr></Table>";

If you are 100% certain on the image path you may be facing an Excel security limitation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.