Welcome to Webhost4life Forum Sign in | Join | Faq

ASP.NET VB.NET

Started by NewCents at 08-27-2005 1:53 PM. Topic has 7 replies.

Print Search
Sort Posts:    
   08-27-2005, 1:53 PM
NewCents is not online. Last active: 8/28/2005 4:52:00 AM NewCents

Top 500 Posts
Joined on 08-28-2005
Posts 1
VB.net Datagrid to Excel
Reply Quote

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 
   08-29-2005, 9:14 PM
jsmirnow is not online. Last active: 8/30/2005 12:12:26 PM jsmirnow

Top 500 Posts
Joined on 08-30-2005
Posts 1
Re: VB.net Datagrid to Excel
Reply Quote

can you post a code snippet and give us some idea of the error message? 

 


   Report 
   09-15-2005, 7:21 PM
gdltec is not online. Last active: 9/16/2005 10:13:42 AM gdltec

Top 500 Posts
Joined on 09-16-2005
Posts 1
Re: VB.net Datagrid to Excel
Reply Quote

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 is not online. Last active: 11/8/2006 11:14:08 AM Bean

Top 100 Posts
Joined on 09-19-2005
Posts 3
Re: VB.net Datagrid to Excel
Reply Quote

See here:

http://www.codeproject.com/aspnet/Excel_CSV_export_button.asp


   Report 
   09-18-2005, 3:04 PM
Bean is not online. Last active: 11/8/2006 11:14:08 AM Bean

Top 100 Posts
Joined on 09-19-2005
Posts 3
Re: VB.net Datagrid to Excel
Reply Quote

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 is not online. Last active: 4/17/2007 7:09:50 PM aspnetdunse

Top 500 Posts
Joined on 04-17-2007
Posts 1
Re: VB.net Datagrid to Excel
Reply Quote

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 is not online. Last active: 3/29/2008 1:03:47 AM robertsams23

Not Ranked
Joined on 03-29-2008
Posts 1
Re: VB.net Datagrid to Excel
Reply Quote
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 is not online. Last active: 6/4/2008 4:25:26 PM Barba

Not Ranked
Joined on 06-04-2008
Posts 1
Re: VB.net Datagrid to Excel
Reply Quote
Hi,

You can try using some third party component like GemBox.Spreadsheet - .NET component for importing/exporting data to XLS, CSV, HTML or XLSX files. There is an example that shows how to export DataGrid to Excel by using only GemBox.Spreadsheet. Try the GemBox.Spreadsheet free version that you can use even in your commercial applications (free version has 150 rows limit).

Mario
GemBox Software
--
GemBox.Spreadsheet for .NET - Easily read and write Excel (XLS, XLSX or CSV)
or export to HTML files from your .NET apps
.
--


   Report 
Webhost4life Fo... » General Discuss... » ASP.NET VB.NET » Re: VB.net Datagrid to Excel

Powered by Community Server, by Telligent Systems