startuga Posted August 22, 2013 at 09:27 AM Report #522375 Posted August 22, 2013 at 09:27 AM 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?
Ruka284 Posted August 22, 2013 at 09:52 AM Report #522379 Posted August 22, 2013 at 09:52 AM vê se este link te dá uma ajuda http://www.macoratti.net/aspn2ede1.htm
startuga Posted August 22, 2013 at 10:16 AM Author Report #522384 Posted August 22, 2013 at 10:16 AM 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" />
Ruka284 Posted August 22, 2013 at 10:22 AM Report #522385 Posted August 22, 2013 at 10:22 AM 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 ??
startuga Posted August 22, 2013 at 10:49 AM Author Report #522386 Posted August 22, 2013 at 10:49 AM Sim
Ruka284 Posted August 22, 2013 at 10:58 AM Report #522389 Posted August 22, 2013 at 10:58 AM Sim Boa.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now