2014年7月13日 星期日

[RESOLVED] Exporting Images from Gridview to Excel


Hi All,


             My requirement is i want to export images in the gridview to excel sheet. For this iam using the following code:



Response.Clear();

Response.Buffer = true;

Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");

Response.Charset = "";

Response.ContentType = "application/vnd.ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);


GridView1.AllowPaging = false;




for (int i = 0; i < GridView1.Rows.Count; i++)

{

GridViewRow row = GridView1.Rows[i];

//Apply text style to each Row

row.Attributes.Add("class", "textmode");

row.Attributes.Add("class", ".img");


}

//Apply some style settimgs

GridView1.Caption = "Your caption";

GridView1.Style.Add("width", "400px");

GridView1.HeaderRow.Style.Add("font-size", "12px");

GridView1.HeaderRow.Style.Add("font-weight", "bold");

GridView1.Style.Add("border", "1px solid black");

GridView1.Style.Add("text-decoration", "none");

GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");

GridView1.Style.Add("class", "img");

GridView1.RenderControl(hw);


//style to format numbers to string

string style = @"";

Response.Write(style);

Response.Write(style);

Response.Output.Write(sw.ToString());

Response.Flush();

Response.End();



Its working fine but iam facing 2 problems with this code:


1. Iam unable to set the image size in the excel.It export with original size which i dont want.


2. If i copy the excel sheet and paste it in another machine images are not displayed.


Am i missing any thing? or need to follow any other approach. Please help me to solve this issue ASAP.


                



hi  M.Mayuri,


  From what I understand you are facing 2 problems with Exporting Images from Gridview to Excel.


1.If you want to set the image size in the excel, you can try to use css style.     


StreamReader sr = new StreamReader(Server.MapPath("../Styles/grid.css"));

string s = sr.ReadToEnd();

Response.Write("");

2. Do you run it in localhost? If it is, I would suggest you place the project in a server.


When you do View Source of the page in Browser you will notice that the relative URL have been converted to absolute. You need to convert the complete URL of the image which is needed.


Please read the reference below for more information:


http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx


If you need more assistance, please let me know.


沒有留言:

張貼留言