Jump to content
startuga

[Resolvido] Gridview to excel

Recommended Posts

startuga

Boas pessoal,

Estou com este código a exportar os dados de uma grid para excel:

Private Sub btn_REQST_ExcelReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_REQST_ExcelReport.Click
    Response.Clear()
    ' Set the type and filename
    Response.AddHeader("content-disposition", "attachment;filename=REQST" & "_" & Format(Date.Now, "dd/MM/yyyy_HHmm") & ".xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.xls"
    ' Add the HTML from the GridView to a StringWriter so we can write it out later
    Dim sw As System.IO.StringWriter = New System.IO.StringWriter
    Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(sw)
    GV_REQST.RenderControl(hw)
    ' Write out the data
    Response.Write(sw.ToString)
    Response.End()
   End Sub

Contudo os dois primeiros campos da grid são de "Edit" & "Delete", como faço para que os mesmo não apareçam no report do ficheiro de excel?

Share this post


Link to post
Share on other sites
startuga

Código final:

 Sub doExcel(ByVal Source As Object, ByVal E As EventArgs)
    GV_REQST.Columns.RemoveAt(1)
    GV_REQST.Columns.RemoveAt(0)
    'se o grid tiver mais que 65536  linhas não podemos exportar
    If GV_REQST.Rows.Count.ToString + 1 < 65536 Then
	    GV_REQST.AllowPaging = "False"
	    GV_REQST.DataBind()
	    Dim tw As System.IO.StringWriter = New System.IO.StringWriter
	    Dim hw As New System.Web.UI.HtmlTextWriter(tw)
	    Dim frm As HtmlForm = New HtmlForm()
	    Response.ContentType = "application/vnd.ms-excel"
	    Response.AddHeader("content-disposition", "attachment;filename=REQST" & "_" & Format(Date.Now, "dd/MM/yyyy_HHmm") & ".xls")
	    Response.Charset = ""
	    EnableViewState = False
	    Controls.Add(frm)
	    frm.Controls.Add(GV_REQST)
	    frm.RenderControl(hw)
	    Response.Write(tw.ToString())
	    Response.End()
	    GV_REQST.AllowPaging = "True"
	    GV_REQST.DataBind()
    End If
   End Sub

<asp:Button ID="btn_REQST_ExcelReport" runat="server" Text="Export to Excel"
		  Height="26px" onclick="doExcel" />

Share this post


Link to post
Share on other sites
Ruka284

Código final:

Sub doExcel(ByVal Source As Object, ByVal E As EventArgs)
 GV_REQST.Columns.RemoveAt(1)
 GV_REQST.Columns.RemoveAt(0)
 'se o grid tiver mais que 65536 linhas não podemos exportar
 If GV_REQST.Rows.Count.ToString + 1 < 65536 Then
	 GV_REQST.AllowPaging = "False"
	 GV_REQST.DataBind()
	 Dim tw As System.IO.StringWriter = New System.IO.StringWriter
	 Dim hw As New System.Web.UI.HtmlTextWriter(tw)
	 Dim frm As HtmlForm = New HtmlForm()
	 Response.ContentType = "application/vnd.ms-excel"
	 Response.AddHeader("content-disposition", "attachment;filename=REQST" & "_" & Format(Date.Now, "dd/MM/yyyy_HHmm") & ".xls")
	 Response.Charset = ""
	 EnableViewState = False
	 Controls.Add(frm)
	 frm.Controls.Add(GV_REQST)
	 frm.RenderControl(hw)
	 Response.Write(tw.ToString())
	 Response.End()
	 GV_REQST.AllowPaging = "True"
	 GV_REQST.DataBind()
 End If
End Sub

<asp:Button ID="btn_REQST_ExcelReport" runat="server" Text="Export to Excel"
		 Height="26px" onclick="doExcel" />

Já resolveste ??

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.