473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export datagrids to Excel

I have a web page written in asp.net that has multiple datagrids on it that
would need to be exported to Excel. Each of the datagrids would be a subset
of what the datagrid above it was.

Thus far, I've had no luck in finding anything to work reliably. Some of
the files I've saved (using a radio button click event), will open fine in
Excel, others give me an "Unable to open file" message.

Any help appreciated.

Thanks,

SC


Nov 18 '05 #1
9 3012
For simple pages, you can export the page as Excel as tables will render.
For more complex Excel, you will either have to program against the Excel
objects (set up Office components on the web server), which is awfully
expensive, perf wise, or get a third party component. My suggestion would be
to find a third party component on a site like componentsource .com.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** *************** **********
Think Outside the Box!
*************** *************** *************** *************** **********
<me@privacy.net > wrote in message
news:uT******** ******@TK2MSFTN GP09.phx.gbl...
I have a web page written in asp.net that has multiple datagrids on it that would need to be exported to Excel. Each of the datagrids would be a subset of what the datagrid above it was.

Thus far, I've had no luck in finding anything to work reliably. Some of
the files I've saved (using a radio button click event), will open fine in
Excel, others give me an "Unable to open file" message.

Any help appreciated.

Thanks,

SC

Nov 18 '05 #2
I found the following online article and sample to be useful. Check it out

http://www.c-sharpcorner.com/Code/20...GridToExcel.as

Suresh

----- me@privacy.net wrote: ----

I have a web page written in asp.net that has multiple datagrids on it tha
would need to be exported to Excel. Each of the datagrids would be a subse
of what the datagrid above it was

Thus far, I've had no luck in finding anything to work reliably. Some o
the files I've saved (using a radio button click event), will open fine i
Excel, others give me an "Unable to open file" message

Any help appreciated

Thanks

S

Nov 18 '05 #3
I've tried a VB version of this and just end up with a blank page with
nothing in it.
SC
"Suresh" <an*******@disc ussions.microso ft.com> wrote in message
news:8C******** *************** ***********@mic rosoft.com...
I found the following online article and sample to be useful. Check it out.
http://www.c-sharpcorner.com/Code/20...ridToExcel.asp
Suresh.

----- me@privacy.net wrote: -----

I have a web page written in asp.net that has multiple datagrids on it that would need to be exported to Excel. Each of the datagrids would be a subset of what the datagrid above it was.

Thus far, I've had no luck in finding anything to work reliably. Some of the files I've saved (using a radio button click event), will open fine in Excel, others give me an "Unable to open file" message.

Any help appreciated.

Thanks,

SC


Nov 18 '05 #4
It's working for me in VB.NET as well
Can you post your code? Maybe some C# wasn't coverted properly

Suresh

FYI: (in VB.NET from the code that worked for me
Private Sub btnConvert_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnConvert.Clic
Response.Clear(
Response.Buffer = Tru
Response.Conten tType = "applicatio n/vnd.ms-excel
Response.Charse t = "
Me.EnableViewSt ate = Fals

Dim oStringWriter As System.IO.Strin gWriter = New System.IO.Strin gWriter(
Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = New System.Web.UI.H tmlTextWriter(o StringWriter

Me.ClearControl s(DataGrid1
DataGrid1.Rende rControl(oHtmlT extWriter

Response.Write( oStringWriter.T oString()

Response.End(
End Su

Private Sub ClearControls(B yVal ctrl As Control
Dim i As Int3

For i = ctrl.Controls.C ount - 1 To 0 Step -
ClearControls(c trl.Controls(i)
Nex

If ctrl.GetType(). ToString() <> "TableCell" The
If Not ctrl.GetType(). GetProperty("Se lectedItem") Is Nothing The
Dim literal As LiteralControl = New LiteralControl(
ctrl.Parent.Con trols.Add(liter al
Tr
literal.Text = CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl, Nothing), System.String
Catc
End Tr
ctrl.Parent.Con trols.Remove(ct rl
End I
Els
Dim literal As LiteralControl = New LiteralControl(
ctrl.Parent.Con trols.Add(liter al
literal.Text = CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl, Nothing), System.String
ctrl.Parent.Con trols.Remove(ct rl
End I

Retur
End Su

----- me@privacy.net wrote: ----

I've tried a VB version of this and just end up with a blank page wit
nothing in it
S
"Suresh" <an*******@disc ussions.microso ft.com> wrote in messag
news:8C******** *************** ***********@mic rosoft.com..
I found the following online article and sample to be useful. Check i out http://www.c-sharpcorner.com/Code/20...GridToExcel.as
Suresh
----- me@privacy.net wrote: ----
I have a web page written in asp.net that has multiple datagrids o

it tha would need to be exported to Excel. Each of the datagrids would be subse of what the datagrid above it was
Thus far, I've had no luck in finding anything to work reliably
Some o the files I've saved (using a radio button click event), will ope fine i Excel, others give me an "Unable to open file" message
Any help appreciated
Thanks
S
>

Nov 18 '05 #5
When I put in your code, exactly as you have it below, I change only the
Private Sub btnconvert_clic k line, to reflect the button I am using. It's a
radio button.

The code I have executing follows:

Private Sub RbtnExport_Sele ctedIndexChange d(ByVal sender As System.Object,
ByVal e As System.EventArg s) Handles RbtnExport.Sele ctedIndexChange d

Response.Clear( )

Response.Buffer = True

Response.Conten tType = "applicatio n/vnd.ms-excel"

Response.Charse t = ""

Me.EnableViewSt ate = False

Dim oStringWriter As System.IO.Strin gWriter = New System.IO.Strin gWriter()

Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = New
System.Web.UI.H tmlTextWriter(o StringWriter)

Me.ClearControl s(DataGrid1)

DataGrid1.Rende rControl(oHtmlT extWriter)

Response.Write( oStringWriter.T oString())

Response.End()

End Sub

Private Sub ClearControls(B yVal ctrl As Control)

Dim i As Int32

For i = ctrl.Controls.C ount - 1 To 0 Step -1

ClearControls(c trl.Controls(i) )

Next

If ctrl.GetType(). ToString() <> "TableCell" Then

If Not ctrl.GetType(). GetProperty("Se lectedItem") Is Nothing Then

Dim literal As LiteralControl = New LiteralControl( )

ctrl.Parent.Con trols.Add(liter al)

Try

literal.Text =
CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl,
Nothing), System.String)

Catch

End Try

ctrl.Parent.Con trols.Remove(ct rl)

End If

Else

Dim literal As LiteralControl = New LiteralControl( )

ctrl.Parent.Con trols.Add(liter al)

literal.Text =
CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl, Nothing),
System.String)

ctrl.Parent.Con trols.Remove(ct rl)

End If

Return

End Sub

as you can see, it is exactly the same as yours, excluding the sub on the
first line.

However, when this runs, I'll step thru it in debug mode, and it'll only
return a blank page. It appears that it's just empty. Do not know why. It
does appear to iterate thru the loop in the clearcontrols section. When it
gets to response.end though, it just sits there.

I'm trying to return a customer list, with names, addresses, etc. on it.

Steve


"Suresh" <an*******@disc ussions.microso ft.com> wrote in message
news:8A******** *************** ***********@mic rosoft.com...
It's working for me in VB.NET as well.
Can you post your code? Maybe some C# wasn't coverted properly.

Suresh.

FYI: (in VB.NET from the code that worked for me)
Private Sub btnConvert_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnConvert.Clic k Response.Clear( )
Response.Buffer = True
Response.Conten tType = "applicatio n/vnd.ms-excel"
Response.Charse t = ""
Me.EnableViewSt ate = False

Dim oStringWriter As System.IO.Strin gWriter = New System.IO.Strin gWriter() Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = New System.Web.UI.H tmlTextWriter(o StringWriter)
Me.ClearControl s(DataGrid1)
DataGrid1.Rende rControl(oHtmlT extWriter)

Response.Write( oStringWriter.T oString())

Response.End()
End Sub

Private Sub ClearControls(B yVal ctrl As Control)
Dim i As Int32

For i = ctrl.Controls.C ount - 1 To 0 Step -1
ClearControls(c trl.Controls(i) )
Next

If ctrl.GetType(). ToString() <> "TableCell" Then
If Not ctrl.GetType(). GetProperty("Se lectedItem") Is Nothing Then
Dim literal As LiteralControl = New LiteralControl( )
ctrl.Parent.Con trols.Add(liter al)
Try
literal.Text = CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl,
Nothing), System.String) Catch
End Try
ctrl.Parent.Con trols.Remove(ct rl)
End If
Else
Dim literal As LiteralControl = New LiteralControl( )
ctrl.Parent.Con trols.Add(liter al)
literal.Text = CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl, Nothing),
System.String) ctrl.Parent.Con trols.Remove(ct rl)
End If

Return
End Sub

----- me@privacy.net wrote: -----

I've tried a VB version of this and just end up with a blank page with nothing in it.
SC
"Suresh" <an*******@disc ussions.microso ft.com> wrote in message
news:8C******** *************** ***********@mic rosoft.com...
> I found the following online article and sample to be useful. Check it
out. >> http://www.c-sharpcorner.com/Code/20...ridToExcel.asp
>> Suresh.
>> ----- me@privacy.net wrote: -----
>> I have a web page written in asp.net that has multiple

datagrids on it that
> would need to be exported to Excel. Each of the datagrids
would be a subset
> of what the datagrid above it was.
>> Thus far, I've had no luck in finding anything to work
reliably. Some of
> the files I've saved (using a radio button click event), will
open fine in
> Excel, others give me an "Unable to open file" message.
>> Any help appreciated.
>> Thanks,
>> SC
>>>>>

Nov 18 '05 #6
Interesting..
This may be something other than just converting the grid to excel

Couple of more checks/tests you can perform to narrow the problem down.

1. Ensure EnableViewState is set to true on the page that has datagrid

2. In the immediate window check if there's anything at all is in thi
oStringWriter.T oString(
This will narrow the problem to the conversion code. If it has your table in HTML format then it might be a browser problem

3. Try creating a test page that simply has a button and a grid. Clicking on the button export the grid to Excel. Leave all the default settings of the page

Suresh

----- me@privacy.net wrote: ----

When I put in your code, exactly as you have it below, I change only th
Private Sub btnconvert_clic k line, to reflect the button I am using. It's
radio button

The code I have executing follows

Private Sub RbtnExport_Sele ctedIndexChange d(ByVal sender As System.Object
ByVal e As System.EventArg s) Handles RbtnExport.Sele ctedIndexChange

Response.Clear(

Response.Buffer = Tru

Response.Conten tType = "applicatio n/vnd.ms-excel

Response.Charse t = "

Me.EnableViewSt ate = Fals

Dim oStringWriter As System.IO.Strin gWriter = New System.IO.Strin gWriter(

Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = Ne
System.Web.UI.H tmlTextWriter(o StringWriter

Me.ClearControl s(DataGrid1

DataGrid1.Rende rControl(oHtmlT extWriter

Response.Write( oStringWriter.T oString()

Response.End(

End Su

Private Sub ClearControls(B yVal ctrl As Control

Dim i As Int3

For i = ctrl.Controls.C ount - 1 To 0 Step -

ClearControls(c trl.Controls(i)

Nex

If ctrl.GetType(). ToString() <> "TableCell" The

If Not ctrl.GetType(). GetProperty("Se lectedItem") Is Nothing The

Dim literal As LiteralControl = New LiteralControl(

ctrl.Parent.Con trols.Add(liter al

Tr

literal.Text
CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl
Nothing), System.String

Catc

End Tr

ctrl.Parent.Con trols.Remove(ct rl

End I

Els

Dim literal As LiteralControl = New LiteralControl(

ctrl.Parent.Con trols.Add(liter al

literal.Text
CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl, Nothing)
System.String

ctrl.Parent.Con trols.Remove(ct rl

End I

Retur

End Su

as you can see, it is exactly the same as yours, excluding the sub on th
first line

However, when this runs, I'll step thru it in debug mode, and it'll onl
return a blank page. It appears that it's just empty. Do not know why. I
does appear to iterate thru the loop in the clearcontrols section. When i
gets to response.end though, it just sits there

I'm trying to return a customer list, with names, addresses, etc. on it

Stev


"Suresh" <an*******@disc ussions.microso ft.com> wrote in messag
news:8A******** *************** ***********@mic rosoft.com..
It's working for me in VB.NET as well
Can you post your code? Maybe some C# wasn't coverted properly
Suresh
FYI: (in VB.NET from the code that worked for me Private Sub btnConvert_Clic k(ByVal sender As System.Object, ByVal e A

System.EventArg s) Handles btnConvert.Clic Response.Clear(
Response.Buffer = Tru
Response.Conten tType = "applicatio n/vnd.ms-excel
Response.Charse t = "
Me.EnableViewSt ate = Fals
Dim oStringWriter As System.IO.Strin gWriter = Ne System.IO.Strin gWriter(
Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = Ne System.Web.UI.H tmlTextWriter(o StringWriter
Me.ClearControl s(DataGrid1)

DataGrid1.Rende rControl(oHtmlT extWriter)
Response.Write( oStringWriter.T oString())
Response.End()

End Sub
Private Sub ClearControls(B yVal ctrl As Control)

Dim i As Int32
For i = ctrl.Controls.C ount - 1 To 0 Step -1

ClearControls(c trl.Controls(i) )
Next
If ctrl.GetType(). ToString() <> "TableCell" Then

If Not ctrl.GetType(). GetProperty("Se lectedItem") Is Nothing Then
Dim literal As LiteralControl = New LiteralControl( )
ctrl.Parent.Con trols.Add(liter al)
Try
literal.Text =

CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl,
Nothing), System.String) Catch
End Try
ctrl.Parent.Con trols.Remove(ct rl)
End If
Else
Dim literal As LiteralControl = New LiteralControl( )
ctrl.Parent.Con trols.Add(liter al)
literal.Text = CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl, Nothing),
System.String) ctrl.Parent.Con trols.Remove(ct rl)
End If
Return End Sub
----- me@privacy.net wrote: -----
I've tried a VB version of this and just end up with a blank page

with nothing in it. SC
"Suresh" <an*******@disc ussions.microso ft.com> wrote in message news:8C******** *************** ***********@mic rosoft.com...
I found the following online article and sample to be useful.
Check it out. http://www.c-sharpcorner.com/Code/20...ridToExcel.asp Suresh.
----- me@privacy.net wrote: -----
I have a web page written in asp.net that has multiple
datagrids on it that
would need to be exported to Excel. Each of the datagrids
would be a subset
of what the datagrid above it was.
Thus far, I've had no luck in finding anything to work
reliably. Some of
the files I've saved (using a radio button click event), will
open fine in
Excel, others give me an "Unable to open file" message.
Any help appreciated.
Thanks,
SC
>>

Nov 18 '05 #7
One problem with exporting the datagrid to excel is that it only works
for one page -- if you ever set up the datagrid with paging, and it's
more than one page, it's not going to work.

What I've been doing is iterating through the datatable or collection
that
is the datasource for the grid, assembling an html table in a string,
and doing a response.write as excel (don't have the syntax in front of
me, but you can find this in ASP groups).

It's a bit of coding, but if you have to do this many times you'll
probably see
a pattern and find a way to create a funtion to do this.

<me@privacy.net > wrote in message news:<uT******* *******@TK2MSFT NGP09.phx.gbl>. ..
I have a web page written in asp.net that has multiple datagrids on it that
would need to be exported to Excel. Each of the datagrids would be a subset
of what the datagrid above it was.

Thus far, I've had no luck in finding anything to work reliably. Some of
the files I've saved (using a radio button click event), will open fine in
Excel, others give me an "Unable to open file" message.

Any help appreciated.

Thanks,

SC

Nov 18 '05 #8
<<1. Ensure EnableViewState is set to true on the page that has datagrid.>>

I just clicked on the aspx page for the one I'm looking at. There are 2
enableviewstate properties - one, enableviewstate = true, the other,
enableviewstate mac = false. In the code for the button index changed, the
enableviewstate is set to false.

<<2. In the immediate window check if there's anything at all is in this
oStringWriter.T oString() This will narrow the problem to the conversion
code. If it has your table in HTML format then it might be a browser
problem.>>

This is what I get when I step thru the code, after the response.write has
been done:

Write Argument not specified for parameter 'FileNumber' of 'Public Sub
Write(FileNumbe r As Integer, ParamArray Output() As Object)'.

the expression "Tostring" has its value = ToString "ASP.customerli st_aspx"
String

I have my stop points on the button index changed, and then on the
rendercontrol step. I single step (F8) thru the next 2 statements, and what
I get is above.

<<3. Try creating a test page that simply has a button and a grid.
Clicking on the button export the grid to Excel. Leave all the default
settings of the page.>>

I will give this a try, but the display above works fine as far as it is
displaying stuff now. Using my old method, it would export the display to
excel, but would barf if it was over 30 records displayed.
Thanks for the help - any further advice is appreciated.

SC

Suresh.

----- me@privacy.net wrote: -----

When I put in your code, exactly as you have it below, I change only the Private Sub btnconvert_clic k line, to reflect the button I am using. It's a radio button.

The code I have executing follows:

Private Sub RbtnExport_Sele ctedIndexChange d(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles RbtnExport.Sele ctedIndexChange d

Response.Clear( )

Response.Buffer = True

Response.Conten tType = "applicatio n/vnd.ms-excel"

Response.Charse t = ""

Me.EnableViewSt ate = False

Dim oStringWriter As System.IO.Strin gWriter = New System.IO.Strin gWriter()
Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = New
System.Web.UI.H tmlTextWriter(o StringWriter)

Me.ClearControl s(DataGrid1)

DataGrid1.Rende rControl(oHtmlT extWriter)

Response.Write( oStringWriter.T oString())

Response.End()

End Sub

Private Sub ClearControls(B yVal ctrl As Control)

Dim i As Int32

For i = ctrl.Controls.C ount - 1 To 0 Step -1

ClearControls(c trl.Controls(i) )

Next

If ctrl.GetType(). ToString() <> "TableCell" Then

If Not ctrl.GetType(). GetProperty("Se lectedItem") Is Nothing Then

Dim literal As LiteralControl = New LiteralControl( )

ctrl.Parent.Con trols.Add(liter al)

Try

literal.Text =
CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl, Nothing), System.String)

Catch

End Try

ctrl.Parent.Con trols.Remove(ct rl)

End If

Else

Dim literal As LiteralControl = New LiteralControl( )

ctrl.Parent.Con trols.Add(liter al)

literal.Text =
CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl, Nothing), System.String)

ctrl.Parent.Con trols.Remove(ct rl)

End If

Return

End Sub

as you can see, it is exactly the same as yours, excluding the sub on the first line.

However, when this runs, I'll step thru it in debug mode, and it'll only return a blank page. It appears that it's just empty. Do not know why. It does appear to iterate thru the loop in the clearcontrols section. When it gets to response.end though, it just sits there.

I'm trying to return a customer list, with names, addresses, etc. on it.
Steve


"Suresh" <an*******@disc ussions.microso ft.com> wrote in message
news:8A******** *************** ***********@mic rosoft.com...
> It's working for me in VB.NET as well.
> Can you post your code? Maybe some C# wasn't coverted properly.
>> Suresh.
>> FYI: (in VB.NET from the code that worked for me) > Private Sub btnConvert_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnConvert.Clic k
> Response.Clear( )
> Response.Buffer = True
> Response.Conten tType = "applicatio n/vnd.ms-excel"
> Response.Charse t = ""
> Me.EnableViewSt ate = False
>> Dim oStringWriter As System.IO.Strin gWriter = New System.IO.Strin gWriter()
> Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = New System.Web.UI.H tmlTextWriter(o StringWriter) >> Me.ClearControl s(DataGrid1)

> DataGrid1.Rende rControl(oHtmlT extWriter)
>> Response.Write( oStringWriter.T oString())
>> Response.End()

> End Sub
>> Private Sub ClearControls(B yVal ctrl As Control)

> Dim i As Int32
>> For i = ctrl.Controls.C ount - 1 To 0 Step -1

> ClearControls(c trl.Controls(i) )
> Next
>> If ctrl.GetType(). ToString() <> "TableCell" Then

> If Not ctrl.GetType(). GetProperty("Se lectedItem") Is

Nothing Then > Dim literal As LiteralControl = New LiteralControl( )
> ctrl.Parent.Con trols.Add(liter al)
> Try
> literal.Text =

CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl, Nothing), System.String)
> Catch
> End Try
> ctrl.Parent.Con trols.Remove(ct rl)
> End If
> Else
> Dim literal As LiteralControl = New LiteralControl( )
> ctrl.Parent.Con trols.Add(liter al)
> literal.Text = CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl,

Nothing), System.String)
> ctrl.Parent.Con trols.Remove(ct rl)
> End If
>> Return > End Sub
>> ----- me@privacy.net wrote: -----
>> I've tried a VB version of this and just end up with a blank page with
> nothing in it.
>>> SC
>>> "Suresh" <an*******@disc ussions.microso ft.com> wrote in
message > news:8C******** *************** ***********@mic rosoft.com...
>> I found the following online article and sample to be useful.

Check it
> out.
>>>>

http://www.c-sharpcorner.com/Code/20...ridToExcel.asp >>> Suresh.
>>> ----- me@privacy.net wrote: -----
>>> I have a web page written in asp.net that has multiple

datagrids on
> it that
>> would need to be exported to Excel. Each of the datagrids

would be a
> subset
>> of what the datagrid above it was.
>>> Thus far, I've had no luck in finding anything to work

reliably.
> Some of
>> the files I've saved (using a radio button click event), will

open
> fine in
>> Excel, others give me an "Unable to open file" message.
>>> Any help appreciated.
>>> Thanks,
>>> SC
>>>>>>

Nov 18 '05 #9
So far, nothing I've tried has worked. Any advice appreciated.

SC
<me@privacy.net > wrote in message
news:eM******** ******@TK2MSFTN GP09.phx.gbl...
<<1. Ensure EnableViewState is set to true on the page that has datagrid.>>
I just clicked on the aspx page for the one I'm looking at. There are 2
enableviewstate properties - one, enableviewstate = true, the other,
enableviewstate mac = false. In the code for the button index changed, the
enableviewstate is set to false.

<<2. In the immediate window check if there's anything at all is in this
oStringWriter.T oString() This will narrow the problem to the conversion
code. If it has your table in HTML format then it might be a browser
problem.>>

This is what I get when I step thru the code, after the response.write has
been done:

Write Argument not specified for parameter 'FileNumber' of 'Public Sub
Write(FileNumbe r As Integer, ParamArray Output() As Object)'.

the expression "Tostring" has its value = ToString "ASP.customerli st_aspx" String

I have my stop points on the button index changed, and then on the
rendercontrol step. I single step (F8) thru the next 2 statements, and what I get is above.

<<3. Try creating a test page that simply has a button and a grid.
Clicking on the button export the grid to Excel. Leave all the default
settings of the page.>>

I will give this a try, but the display above works fine as far as it is
displaying stuff now. Using my old method, it would export the display to
excel, but would barf if it was over 30 records displayed.
Thanks for the help - any further advice is appreciated.

SC

Suresh.

----- me@privacy.net wrote: -----

When I put in your code, exactly as you have it below, I change only
the
Private Sub btnconvert_clic k line, to reflect the button I am using.
It's a
radio button.

The code I have executing follows:

Private Sub RbtnExport_Sele ctedIndexChange d(ByVal sender As System.Object,
ByVal e As System.EventArg s) Handles

RbtnExport.Sele ctedIndexChange d
Response.Clear( )

Response.Buffer = True

Response.Conten tType = "applicatio n/vnd.ms-excel"

Response.Charse t = ""

Me.EnableViewSt ate = False

Dim oStringWriter As System.IO.Strin gWriter = New

System.IO.Strin gWriter()

Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = New
System.Web.UI.H tmlTextWriter(o StringWriter)

Me.ClearControl s(DataGrid1)

DataGrid1.Rende rControl(oHtmlT extWriter)

Response.Write( oStringWriter.T oString())

Response.End()

End Sub

Private Sub ClearControls(B yVal ctrl As Control)

Dim i As Int32

For i = ctrl.Controls.C ount - 1 To 0 Step -1

ClearControls(c trl.Controls(i) )

Next

If ctrl.GetType(). ToString() <> "TableCell" Then

If Not ctrl.GetType(). GetProperty("Se lectedItem") Is Nothing Then

Dim literal As LiteralControl = New LiteralControl( )

ctrl.Parent.Con trols.Add(liter al)

Try

literal.Text =

CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl,
Nothing), System.String)

Catch

End Try

ctrl.Parent.Con trols.Remove(ct rl)

End If

Else

Dim literal As LiteralControl = New LiteralControl( )

ctrl.Parent.Con trols.Add(liter al)

literal.Text =
CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl,

Nothing),
System.String)

ctrl.Parent.Con trols.Remove(ct rl)

End If

Return

End Sub

as you can see, it is exactly the same as yours, excluding the sub on the
first line.

However, when this runs, I'll step thru it in debug mode, and it'll only
return a blank page. It appears that it's just empty. Do not know

why. It
does appear to iterate thru the loop in the clearcontrols section.

When it
gets to response.end though, it just sits there.

I'm trying to return a customer list, with names, addresses, etc.

on it.

Steve


"Suresh" <an*******@disc ussions.microso ft.com> wrote in message
news:8A******** *************** ***********@mic rosoft.com...
> It's working for me in VB.NET as well.
> Can you post your code? Maybe some C# wasn't coverted properly.
>> Suresh.
>> FYI: (in VB.NET from the code that worked for me)
> Private Sub btnConvert_Clic k(ByVal sender As System.Object, ByVal e
As
System.EventArg s) Handles btnConvert.Clic k
> Response.Clear( )
> Response.Buffer = True
> Response.Conten tType = "applicatio n/vnd.ms-excel"
> Response.Charse t = ""
> Me.EnableViewSt ate = False
>> Dim oStringWriter As System.IO.Strin gWriter = New System.IO.Strin gWriter()
> Dim oHtmlTextWriter As System.Web.UI.H tmlTextWriter = New

System.Web.UI.H tmlTextWriter(o StringWriter)
>> Me.ClearControl s(DataGrid1)
> DataGrid1.Rende rControl(oHtmlT extWriter)
>> Response.Write( oStringWriter.T oString())
>> Response.End()
> End Sub
>> Private Sub ClearControls(B yVal ctrl As Control)
> Dim i As Int32
>> For i = ctrl.Controls.C ount - 1 To 0 Step -1
> ClearControls(c trl.Controls(i) )
> Next
>> If ctrl.GetType(). ToString() <> "TableCell" Then
> If Not ctrl.GetType(). GetProperty("Se lectedItem") Is

Nothing Then > Dim literal As LiteralControl = New LiteralControl( )
> ctrl.Parent.Con trols.Add(liter al)
> Try
> literal.Text =

CType(ctrl.Cont rols.GetType(). GetProperty("Se lectedItem").Ge tValue(ctrl,
Nothing), System.String)
> Catch
> End Try
> ctrl.Parent.Con trols.Remove(ct rl)
> End If
> Else
> Dim literal As LiteralControl = New LiteralControl( )
> ctrl.Parent.Con trols.Add(liter al)
> literal.Text =

CType(ctrl.Cont rols.GetType(). GetProperty("Te xt").GetValue(c trl,

Nothing),
System.String)
> ctrl.Parent.Con trols.Remove(ct rl)
> End If
>> Return
> End Sub
>> ----- me@privacy.net wrote: -----
>> I've tried a VB version of this and just end up with a
blank page
with
> nothing in it.
>>> SC
>>> "Suresh" <an*******@disc ussions.microso ft.com> wrote in

message > news:8C******** *************** ***********@mic rosoft.com...
>> I found the following online article and sample to be useful.

Check it
> out.
>>>>

http://www.c-sharpcorner.com/Code/20...ridToExcel.asp >>> Suresh.
>>> ----- me@privacy.net wrote: -----
>>> I have a web page written in asp.net that has multiple

datagrids on
> it that
>> would need to be exported to Excel. Each of the datagrids

would be a
> subset
>> of what the datagrid above it was.
>>> Thus far, I've had no luck in finding anything to work

reliably.
> Some of
>> the files I've saved (using a radio button click event),

will open
> fine in
>> Excel, others give me an "Unable to open file" message.
>>> Any help appreciated.
>>> Thanks,
>>> SC
>>>>>>


Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1662
by: Benny | last post by:
Hello Experts, I am currently working on a web application using vs.net 2002 with c#. Assume I have a page with 3 datagrids, how can I export these 3 datagrids to an excel file with 3 sheets, where each sheet contain 1 datagrid data? Thanks,
0
1279
by: Steve Chatham | last post by:
I need to export multiple datagrids (where we have a drill-through web application) into an Excel workbook. For instance, when you click on the first datagrid, you get a subset of the sql table. Subsequent clicks on those items in the table further filter the request, until you go from a business area down to a single SKU. Thus far, I have gotten it to work, but there have been size issues (I guess, as smaller files seem to do okay),...
1
1070
by: Michael | last post by:
I have used the following article to try to export from a datagrid into excel http://www.dotnetjohn.com/articles/articleid78.aspx It runs fine for him, but when I try to use it, it just refreshes to a blank page. Any help? Thanks, Michael
1
1492
by: VK | last post by:
Hey, I have some nested datagrids, which I would like to export to a excel. Exporting is not a problem - however I would like to export the nested data into different sheets on the same excel page. Is that possible. Basically I just need to know how to create new sheets in excel. Thanks
3
1669
by: mattdaddym | last post by:
Hi all, I have been using the following code to export my datagrids to excel for quite some time. Very simple and very effective. <code> Sub btnExcelExport_Click ( s As Object, e As EventArgs ) Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
7
4915
by: mattmerc | last post by:
Hi all, We have all seen lot of method for exporting datagrids to Excel. I have a slightly different need. I think it should be easy to accomplish but I am not sure how. I would like when a user clicks a button for exporting a datagrid, that the excel file is saved on the server and that it DOES NOT prompt the user to open or save. Here is the code I am using for standard export that prompts the user. Thanks all. Dim...
13
13241
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to save it the Default File Type is Web Page(*.htm; *.html)
1
1319
by: =?Utf-8?B?Sm9obiBXYWxrZXI=?= | last post by:
Hi, I am using the code below to export a webpage to Excel. The webpage has three datagrids on it and they are all exported to Excel properly and everything looks very nice, but we would really only like two of the three datagrids to be exported. Is there a way to exclude certain sections of the webpage from being exported to Excel? "DG" in the code here is one of the datagrids on the page: ...
5
2537
by: Reggie | last post by:
Hi and TIA! I have an export procedure that exports my data from my datagrids to excel. The problem is that I lose leading zero's cause excel removes them and doesn't treat them as text. Is it possible to design an excel template file, place it in my shared network folder and have the procedure write data to the template file? I've thought of reading the recordset line by line, field by field and concatenating an apostrophe at the...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.