locked
problem Using itextsharp to export Excel, PDF etc. RRS feed

  • Question

  • User17438264 posted

    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 

                    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.

    Thanks

    Jay

    Monday, February 13, 2012 3:32 AM

Answers

  • User17438264 posted

    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. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 15, 2012 2:09 AM

All replies

  • User17438264 posted

    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

    Tuesday, February 14, 2012 4:30 AM
  • User-533242366 posted

    Try using this after the Response.Content line..

    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..  

    Tuesday, February 14, 2012 11:47 AM
  • User17438264 posted

    Same error.

    Tuesday, February 14, 2012 8:49 PM
  • User17438264 posted

    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.

    Tuesday, February 14, 2012 9:37 PM
  • User17438264 posted

    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. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 15, 2012 2:09 AM