|
|
ASP.NET VB.NET
Started by NewCents at 08-27-2005 1:53 PM. Topic has 7 replies.
 
 
|
|
Sort Posts:
|
|
|
|
08-27-2005, 1:53 PM
|
NewCents
Joined on 08-28-2005
Posts 1
|
|
|
|
I've seen a lot of sample code but I can't get anything to work on the webhost4life servers. Not sure what the problem is. If anyone has anything available, could they please post a code snippet?
Thank you
|
|
|
|
|
Report
|
|
|
|
09-15-2005, 7:21 PM
|
gdltec
Joined on 09-16-2005
Posts 1
|
Re: VB.net Datagrid to Excel
|
|
|
|
|
|
Place the following code under the onclick event of a button (just change the name "yourDataGrid" to your datagrid's name and the name of the Excel file to whatever you want:
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=AnyName.xls;")
Response.Charset = String.Empty
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
yourDataGrid.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString)
Response.End()
If you have "Sorting", "Paging" enabled on your datagrid, you'll need to disable those just before the data is exported to Excel, the sorting and paging will be enabled the next time your datagrid is binded.
yourDatagrid.AllowPaging = False
yourDatagrid.AllowSorting = False
BindGrid()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=AnyName.xls;")
Response.Charset = String.Empty
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
yourDataGrid.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString)
Response.End()
Hope this helps!
|
|
|
|
|
Report
|
|
|
|
09-18-2005, 2:58 PM
|
Bean
Joined on 09-19-2005
Posts 3
|
Re: VB.net Datagrid to Excel
|
|
|
|
|
|
|
|
09-18-2005, 3:04 PM
|
Bean
Joined on 09-19-2005
Posts 3
|
Re: VB.net Datagrid to Excel
|
|
|
|
|
|
Or, since I have never tried that link method, I have used this...
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Response.Clear() Response.Buffer= True Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Me.EnableViewState = False Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter() Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter) Me.ClearControls(dg) dg.RenderControl(oHtmlTextWriter) Response.Write(oStringWriter.ToString()) Response.End() End Sub
Private Sub ClearControls(ByVal control As Control) Dim i As Integer For i = control.Controls.Count -1 To 0 Step i - 1 ClearControls(control.Controls(i)) Next If TypeOf Not (control Is TableCell) Then If Not control.GetType().GetProperty("SelectedItem") Is Nothing Then Dim literal As LiteralControl = New LiteralControl() control.Parent.Controls.Add(literal) Try literal.Text = CType(control.GetType().GetProperty("SelectedItem").GetValue(control,Nothing), String) End Try control.Parent.Controls.Remove(control) Else Dim literal As LiteralControl = New LiteralControl() control.Parent.Controls.Add(literal) literal.Text = CType(control.GetType().GetProperty("Text").GetValue(control,Nothing), String) control.Parent.Controls.Remove(control) End If End If Return End Sub
|
|
|
|
|
Report
|
|
|
|
04-17-2007, 4:11 AM
|
aspnetdunse
Joined on 04-17-2007
Posts 1
|
Re: VB.net Datagrid to Excel
|
|
|
|
|
|
Hi,
I am using the code you created and I am getting the error message 'TableCell' is a type and cannot be used as an expression on the following line could you tell me how to fix this?
|
|
|
|
|
Report
|
|
|
|
03-28-2008, 9:06 AM
|
robertsams23
Joined on 03-29-2008
Posts 1
|
Re: VB.net Datagrid to Excel
|
|
|
|
|
just check the following urls
http://vb.net-informations.com/excel-2007/vb.net_excel_oledb.htm
http://vb.net-informations.com/excel-2007/vb.net_excel_inset_data_oledb.htm
http://vb.net-informations.com/excel-2007/vb.net_excel_update_data_oledb.htm
|
|
|
|
|
Report
|
|
|
|
06-04-2008, 1:35 AM
|
Barba
Joined on 06-04-2008
Posts 1
|
Re: VB.net Datagrid to Excel
|
|
|
|
|
|
|
|
|
Webhost4life Fo... » General Discuss... » ASP.NET VB.NET » Re: VB.net Datagrid to Excel
|
|
|
|