Embed an image in response to be opened by Excel
I created an HTML table with an image spanning a few cells (first portion) then attempt to export that image to excel (second portion (Ex开发者_运维知识库portReport Method)). I currently have two lines which show the image, both work fine in the HTML table. The commented out portion shows as a red X when exported to excel, the non-commented out line shows the image in Excel however the image is floating and not cemented into an Excel cell.
I would like to know what I'm missing in order to get the image to show in the Excel export while having that image cemented in a cell (not being able to drag it all over the report.)
//Create Table
Table tbl = new Table();
TableRow tr;
TableCell tc;
tc = new TableCell();
tr = new TableRow();
//tc.Text = "<center><imj src=\"images/logo_PROD.gif\" alt=\"Logo\" /></center>";
tc.Text = "<center><imj src=\"http://chwf1/portalsite/images/logo_PROD.gif\" alt=\"Logo\" /></center>";
tc.ColumnSpan = 3;
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
//Export Table
public static void ExportReport(string fileName, Table table)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
table.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
you can do this with Excel Automation [1], or you can give it a try 3rd party components like Aspose ( they handle it in managed code without using Excel, i suppose) or you can get rid of images from yout html code.
[1] http://www.eggheadcafe.com/community/aspnet/2/8506/how-to-insert-a-picture-into-excel-using-c.aspx
and have you tried to search SO? cause there are lots of questions like this, just look at related questions
精彩评论