Export to excel

Last post 12-07-2009 5:41 PM by bartekm. 9 replies.

Sort Posts:

  • Export to excel

    10-24-2007, 2:48 AM
    • Member
      3 point Member
    • sarajuma
    • Member since 10-11-2007, 9:08 AM
    • Posts 21

    i have one gridview which I need to export it to excel ,

    is there any way using VB 

    I 'm using the following code in the click event of a button on the page

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

    ' Remove the charset from the Content-Type header.

    Response.Charset = ""

    ' Turn off the view state.

    Me.EnableViewState = False

    Dim tw As New System.IO.StringWriter()

    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    ' Get the HTML for the control.

    GridView1.RenderControl(hw)

    ' Write the HTML back to the browser.

    Response.Write(tw.ToString())

    ' End the response.

    Response.End()

     

    but I'm getting the following error message

    "  Control 'ctl00_MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server. "

    anything I can do .

    thanks

    Filed under:
  • Re: Export to excel

    10-24-2007, 4:59 AM
    • Star
      12,429 point Star
    • ca8msm
    • Member since 11-29-2005, 3:57 PM
    • http://mdssolutions.co.uk
    • Posts 2,152

    Have a read of the following article (especially the last paragraph):

    http://aspnetlibrary.com/articledetails.aspx?article=How-to-export-a-GridView-to-Excel
     

    Website Design Darlington - http://mdssolutions.co.uk
    http://lessthandot.com - Experts, Information, Ideas & Knowledge
    http://aspnetlibrary.com - An online resource for professional ASP.NET developers


    Please remember to click "Mark as Answer" on this post if it helped you

  • Re: Export to excel

    10-24-2007, 5:32 AM
    • Contributor
      7,324 point Contributor
    • M A A Mehedi Hasan
    • Member since 02-05-2007, 7:40 AM
    • Dhaka, Bangladesh
    • Posts 939

    Hi,

    Put the gridview inside the form tag i.e.

     

       <form id="Form1" method="post" runat="server">

            <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>

    </form>

    To Export to Excel

     

    http://codebetter.com/blogs/darrell.norton/archive/2004/02/12/7154.aspx
    http://www.c-sharpcorner.com/Blogs/BlogDetail.aspx?BlogId=283

     

    Mehedi Hasan


    Mark as answer if the post meets your requirement!
  • Re: Export to excel

    10-24-2007, 7:01 AM
    • Member
      3 point Member
    • sarajuma
    • Member since 10-11-2007, 9:08 AM
    • Posts 21

    thanks for the reply , but I'm getting this error now

    RegisterForEventValidation can only be called during Render();

     

    any ideas  ???

     

     

  • Re: Export to excel

    10-28-2007, 9:59 PM
    Answer

    Hi,

    Based on my understanding, you want to export the GridView into Excel in your asp.net VB application. When you try to run it, you get the error message above.  If I have misunderstood you, please feel free to let me know.

    Sometimes, we can confirm that an HtmlForm control is rendered for the specified ASP.NET server control at run time using VerifyRenderingInServerForm method.

    Please try to add the following code in your .aspx.vb file:

        Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)


        End Sub


    I hope this helps.

     

    Thomas Sun
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: Export to excel

    10-28-2007, 10:11 PM

    sarajuma:

    RegisterForEventValidation can only be called during Render();

     

    Hi,

    For the this error message, please try to set the EnableEventValidation to false. For example:

    <%@ Page Language="VB"  EnableEventValidation="false" CodeFile="Default8.aspx.vb" Inherits="Default8" %>
     
    I hope this helps.
     
    Thomas Sun
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: Export to excel

    05-07-2008, 2:41 PM
    • Contributor
      2,382 point Contributor
    • allanhorwitz
    • Member since 04-10-2008, 1:45 PM
    • Philadelphia, PA
    • Posts 517
  • Re: Export to excel

    05-07-2008, 3:59 PM
    • Member
      49 point Member
    • eforr
    • Member since 08-08-2007, 8:40 PM
    • North Carolina
    • Posts 16

    try this

    Dim stringWrite As IO.StringWriter

    Dim htmlWrite As HtmlTextWriter

    Dim frm As HtmlForm = New HtmlForm()

    Response.Clear()

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

    Response.Charset = ""

    Response.ContentType = "application/vnd.ms-xls"

    Dim strStyle As String = "<style>.text {mso-number-format:\@; } </style>"

    Response.Write(strStyle)

    Response.Write("<H4>") ' throw in a pageheader

    Response.Write("you can put title here")

    Response.Write("</H4>")

    stringWrite = New IO.StringWriterhtmlWrite = New HtmlTextWriter(stringWrite)

    Controls.Add(frm)

    frm.Controls.Add(GridView1)

    frm.RenderControl(htmlWrite)

    Response.Write(stringWrite.ToString())

    Response.End()

     

    but gridview must be inside form tags

  • Re: Export to excel

    11-02-2009, 5:50 AM
    • Member
      282 point Member
    • BarbaMariolino
    • Member since 03-18-2008, 6:43 AM
    • Croatia
    • Posts 96

    What you are doing here is basically fooling your browser to redirect this request to Excel program although generated output is HTML.

    There are much cleaner ways of exporting to Excel. Check out GemBox.Spreadsheet and this web sample demonstrates CSV/XLS/XLSX/ODS/HMTL writing.

  • Re: Export to excel

    12-07-2009, 5:41 PM
    • Member
      154 point Member
    • bartekm
    • Member since 05-01-2007, 7:50 AM
    • Sydney
    • Posts 56

    The problem is that you also need to overwrite the render() method.


    See if this article helps


    ASP.NET Development Blog:
    http://blog.evonet.com.au
Page 1 of 1 (10 items)