Rendering Excel from a browser

I have content that I have to display as an excel file in the browser. I was able to display the contents of the grid to outperform the code below.

  Response.Clear();
  Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
  Response.Charset = "";
  Response.ContentType = "application/vnd.xls";
  System.IO.StringWriter stringWrite = new System.IO.StringWriter();
  System.Web.UI.HtmlTextWriter htmlWrite =
  new HtmlTextWriter(stringWrite);
  GridView1.RenderControl(htmlWrite);
  htmlWrite.Write(stringWrite);
  Response.Write(stringWrite.ToString());
  Response.End();

      

Now I have to do a similar thing for the image. That is, I want some code that can make the image look great.

I mean when a user opens excel he should be able to see the image in it.

Is it possible to convert an image to base64sting format and put it in excel?

Please let me know if you have an idea like this.

Thanks to Vinod T.

+1


source to share


5 answers


I am using the same hacks to get the grid data (html tagged) in excel.

If it's HTML data you write there, just add <IMG src = "MYFILENAME.JPG> to the HTML text where you want the images.

What happens is that Excel will ask your server (asp.net application) for this file.

You may need to add the full path to the image <IMG src = "http: //myserver/subpath/MYFILENAME.JPG>



Using a batch sniffer, I have verified that Excel will actually ask for an IMG file, so just make sure you put the image ready to upload. (either as an asp.net stream) or physically.

My test HTML.
<html>
<table>
& L; Td> test </ td> <td> test </ & td l; td> Test </ td> </TR>
& L; TR> <td> Test </td> <td> Test </td> <td> Test </td> </TR>
& L; TR> <td> Test </ td> <td> Test </ td> <td> <IMG src = "HTTP: //myserver/LALLAALLAA.jpg"> </ td> </TR>
</ table>
< / Html>

If ONLY an image, then this is the same as without table tags. Just HTML and IMG tag.

+2


source


A. Another tricky way would be to create a .MHT file (webarchive). They contain both HTML and images embedded in the same file. This way you only have one thread. But I don't know if Excel will be able to recognize MHT content and show it.



0


source


Office 2007 documents (Word, PowerPoint, and Excel) are based on the OpenXML formats. They are just zip files with a bunch of XML and binary parts (like images) inside. You can create them with any of the XML classes in the Framework and then put the snippets along with the packaging API (System.IO.Packaging in WindowsBase.dll).

Check out OpenXMLDeveloper.org for more details.

0


source


Take a look at http://www.codeplex.com/OpenXMLViewer . Maybe this can help you.

0


source


I am using this library for xls-output: CarlogAG Excel Writer

0


source







All Articles