I've been trying to Export my GridView to Excel using itextSharp as per the example given by Mudassar Khan in aspsnippets.com. When I run the code
DataSet dt = Logins.Fill_Ds(str);
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt.Tables[0];
GridView1.DataBind();
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);
//for (int i = 0; i < GridView1.Rows.Count; i++)
//{
// //Apply text style to each Row
// GridView1.Rows[i].Attributes.Add("class", "textmode");
//}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
But, its not completing and throwing an error while is not able to trace due to this "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack" So I downloaded the demo from aspsnippets and its working fine,
but in my solution its not working. May be I am use ajaxToolKit and with updatepanel. Its all not clear to me. Please help me out with this, if anyone is using iTextSharp with ajaxToolkit and Asp.net 4.0.
But I am also using these things so nothing gonna help me, I debug it more and found that actual problem is LoginView where my GridView is place inside LoginView and when I render the gridview Like this
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Label lblheader = new Label();
lblheader.Text = @"<b style=""font-size:small"">" + Header + "</b>" + "<br/>";
lblheader.RenderControl(htw);
// Create a form to contain the grid
Table table = new Table();
table.GridLines = gv.GridLines;
if (!(string.IsNullOrEmpty(Header)))
{
TableRow tr = new TableRow();
TableCell cl = new TableCell();
TableCell a = new TableCell();
//a.Text = Header;
// a.Attributes.Add("font-weight", "bold");
tr.Cells.Add(a);
table.Rows.Add(tr);
}
// add the header row to the table
if (gv.HeaderRow != null)
{
ExportControl_Grid(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
ExportControl_Grid(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
ExportControl_Grid(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
private static void ExportControl_Grid(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is Image)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl(" "));
}
else if (current is Panel)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl(" "));
}
if (current.HasControls())
{
Reports.ExportControl_Grid(current);
}
}
}
Here gv is nothing but your Gridview.. Find the Gridview first and pass it to this function..
Its quite frustrating, I put the Ideal Page (From Aspsnippets.com) inside update panel, it works, I put Toolkit Script manager it worked and even I put it inside LoginView and used it with FindControl it worked. I really dont understand whats wrong in my
page then.
Hi, I just put my Export button outside of Update Panel and it started working, but that was hit & try only, I still don't have clue what happened. Why it need a triggering out of Updatepanel.
And What If I like to keep my Button inside update panel, with postback option.
Member
48 Points
158 Posts
problem Using itextsharp to export Excel, PDF etc.
Feb 13, 2012 03:32 AM|jaykhatri|LINK
Hi,
I've been trying to Export my GridView to Excel using itextSharp as per the example given by Mudassar Khan in aspsnippets.com. When I run the code
But, its not completing and throwing an error while is not able to trace due to this "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack" So I downloaded the demo from aspsnippets and its working fine, but in my solution its not working. May be I am use ajaxToolKit and with updatepanel. Its all not clear to me. Please help me out with this, if anyone is using iTextSharp with ajaxToolkit and Asp.net 4.0.
Thanks
Jay
All-Star
48393 Points
12161 Posts
Re: problem Using itextsharp to export Excel, PDF etc.
Feb 13, 2012 10:27 PM|chetan.sarode|LINK
Hi,
http://csharpdotnetfreak.blogspot.com/2008/12/export-gridview-to-pdf-using-itextsharp.html
also check...
http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx
http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx
http://forums.asp.net/t/1412788.aspx/1
Team Lead, Product Development
Approva Systems Pvt Ltd, Pune, India.
Member
48 Points
158 Posts
Re: problem Using itextsharp to export Excel, PDF etc.
Feb 14, 2012 04:30 AM|jaykhatri|LINK
Thanks for your reply,
But I am also using these things so nothing gonna help me, I debug it more and found that actual problem is LoginView where my GridView is place inside LoginView and when I render the gridview Like this
GridView gw = (GridView)LoginView1.FindControl("GridView1");
gw.RenderControl(hw);
it gives an error, Control 'LoginView1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
If this will be solved, I think my problem will also be sorted out. I just dot to try other example, I like to clear the code that I given.
I already replaced GridView1 with in my question.
GridView gw = (GridView)LoginView1.FindControl("GridView1");
Thanks & Regards
Jaidev Khatri
Contributor
3980 Points
1144 Posts
Re: problem Using itextsharp to export Excel, PDF etc.
Feb 14, 2012 11:47 AM|sushanth009|LINK
Try using this after the Response.Content line..
Here gv is nothing but your Gridview.. Find the Gridview first and pass it to this function..
Member
48 Points
158 Posts
Re: problem Using itextsharp to export Excel, PDF etc.
Feb 14, 2012 08:49 PM|jaykhatri|LINK
Same error.
Member
48 Points
158 Posts
Re: problem Using itextsharp to export Excel, PDF etc.
Feb 14, 2012 09:37 PM|jaykhatri|LINK
Its quite frustrating, I put the Ideal Page (From Aspsnippets.com) inside update panel, it works, I put Toolkit Script manager it worked and even I put it inside LoginView and used it with FindControl it worked. I really dont understand whats wrong in my page then.
Member
48 Points
158 Posts
Re: problem Using itextsharp to export Excel, PDF etc.
Feb 15, 2012 02:09 AM|jaykhatri|LINK
Hi, I just put my Export button outside of Update Panel and it started working, but that was hit & try only, I still don't have clue what happened. Why it need a triggering out of Updatepanel.
And What If I like to keep my Button inside update panel, with postback option.