Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Developer,

I want to export data from Asp.net datagrid to excel.
the grid cell contains images(png image),
i cannot set the image in excel cell.
plz anyone help to me...
Thanks
Posted
Comments
Maciej Los 26-Sep-13 5:04am    
What have you done tille now? Where are you stuck? What kind of method do you use to export data into Excel?

The Shapes.AddPicture[^] method is used to insert a picture.

The Range[^] Top and Left properties are used to define the top left corner.

Width and Height should be in points instead of pixels. Use the 3/4 multiplier.

You may scale your pictures also.

Here is a short example in VBA:

VB
Dim ws As Worksheet
Dim r As Range
Dim fileName As String
Dim pictureWidth As Integer
Dim pictureHeight As Integer
Dim shape As shape

Set ws = Worksheets(1)

Set r = ws.Cells(5, 2)

fileName = "d:\picture.png"

pictureWidth = 195
pictureHeight = 102

Set shape = ws.Shapes.AddPicture(fileName, msoTrue, msoTrue, r.Left, r.Top, pictureWidth * 3 / 4, pictureHeight * 3 / 4)

shape.LockAspectRatio = msoTrue
shape.ScaleHeight 2, msoTrue, msoScaleFromTopLeft
 
Share this answer
 
Comments
sankmahesh 26-Sep-13 8:28am    
plz give me the C# code format..
sankmahesh 26-Sep-13 8:30am    
msoTrue , msoScaleFromTopLeft are returning error. so please give the above code in C# format
Sergey Vaselenko 26-Sep-13 9:24am    
msoTrue = -1
msoScaleFromTopLeft = 0
See Shape members at http://msdn.microsoft.com/en-us/library/ff193908(v=office.14).aspx.
sankmahesh 26-Sep-13 10:02am    
msotristate savewithDocument value = ?
Sergey Vaselenko 26-Sep-13 11:29am    
See MsoTriState Enumeration at http://msdn.microsoft.com/en-us/library/ff860737(v=office.14).aspx.
msoTrue = -1
msoFalse = 0
I don't found the corresponding VSTO enumeration.
i added just the below code,


public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}

result came...
 
Share this answer
 
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
OleDbConnection con = new OleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:/Guna Sankari Documents/WebSite2/db1.accdb") ;
string Query2 = "select * from tblfiles" ;
OleDbDataAdapter da;
DataSet ds=new DataSet();
DataTable dtb;
con.Open() ;
da = new OleDbDataAdapter(Query2, con) ;
da.Fill(ds);
con.Close() ;
dtb = ds.Tables[0] ;
GridView1.DataSource = dtb ;
GridView1.DataBind();
}
}


protected string GetUrl(string imagepath)
{

string[] splits = Request.Url.AbsoluteUri.Split('/');

if (splits.Length >= 2)
{

string url = splits[0] + "//";

for (int i = 2; i < splits.Length - 1; i++)
{

url += splits[i];

url += "/";

}

return url + imagepath;

}

return imagepath;

}

private void Excel_Export()
{

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;

GridView1.DataBind();

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

GridViewRow row = GridView1.Rows[i];
row.Attributes.Add("class", "textmode");

}

GridView1.RenderControl(hw);
string style = @" .textmode { mso-number-format:\@; } ";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

}



static DataTable GetData()
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(string));
table.Columns.Add("FileName", typeof(string));
//table.Columns.Add("Location", typeof(string));
table.Columns.Add("Image");

table.Rows.Add("JP", "XXX", "http://localhost/images/fir.png");
table.Rows.Add("HP", "TTT", "http://localhost/images/sec.png");
table.Rows.Add("SQ", "YYY", "http://localhost/images/thr.png");
table.Rows.Add("XS", "EEE", "http://localhost/images/fur.png");
return table;
}

protected void Button1_Click(object sender, EventArgs e)
{
Excel_Export();

}
}
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "False"
Font-Names = "Arial" >

<Columns>

    <asp:BoundField DataField = "ID" HeaderText = "ID"

     ItemStyle-Height = "150" />

    <asp:BoundField DataField = "FileName" HeaderText = "Image Name"

     ItemStyle-Height = "150" />

  <asp:TemplateField  ItemStyle-Height = "150" ItemStyle-Width = "170">

        <ItemTemplate>

            <asp:Image ID="Image1" runat="server"

             ImageUrl = '<%# Eval("FilePath", GetUrl("{0}")) %>' />

        </ItemTemplate>

    </asp:TemplateField>

</Columns>

</asp:GridView>



ID1 ID FileName FilePath
1 1 Image images/fir.png
2 2 Img2 images/sec.png
 
Share this answer
 
Comments
Maciej Los 26-Sep-13 16:13pm    
What it is? Is it an answer?
sankmahesh 27-Sep-13 0:02am    
this is my program.
sankmahesh 27-Sep-13 0:23am    
HttpException was unhandled by usercode
------------------------------------------

"Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server." error returns the below line


GridView1.RenderControl(hw);
sankmahesh 27-Sep-13 1:05am    
Guys i found the solution,

i added just the below code,


public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}

result came...


thank for your cooperaion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900