473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2989
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**************@TK2MSFTNGP09.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*******@discussions.microsoft.com> wrote in message
news:8C**********************************@microsof t.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_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Clic
Response.Clear(
Response.Buffer = Tru
Response.ContentType = "application/vnd.ms-excel
Response.Charset = "
Me.EnableViewState = Fals

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(DataGrid1
DataGrid1.RenderControl(oHtmlTextWriter

Response.Write(oStringWriter.ToString()

Response.End(
End Su

Private Sub ClearControls(ByVal ctrl As Control
Dim i As Int3

For i = ctrl.Controls.Count - 1 To 0 Step -
ClearControls(ctrl.Controls(i)
Nex

If ctrl.GetType().ToString() <> "TableCell" The
If Not ctrl.GetType().GetProperty("SelectedItem") Is Nothing The
Dim literal As LiteralControl = New LiteralControl(
ctrl.Parent.Controls.Add(literal
Tr
literal.Text = CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl, Nothing), System.String
Catc
End Tr
ctrl.Parent.Controls.Remove(ctrl
End I
Els
Dim literal As LiteralControl = New LiteralControl(
ctrl.Parent.Controls.Add(literal
literal.Text = CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl, Nothing), System.String
ctrl.Parent.Controls.Remove(ctrl
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*******@discussions.microsoft.com> wrote in messag
news:8C**********************************@microsof t.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_click line, to reflect the button I am using. It's a
radio button.

The code I have executing follows:

Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged

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(DataGrid1)

DataGrid1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())

Response.End()

End Sub

Private Sub ClearControls(ByVal ctrl As Control)

Dim i As Int32

For i = ctrl.Controls.Count - 1 To 0 Step -1

ClearControls(ctrl.Controls(i))

Next

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

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

Dim literal As LiteralControl = New LiteralControl()

ctrl.Parent.Controls.Add(literal)

Try

literal.Text =
CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl,
Nothing), System.String)

Catch

End Try

ctrl.Parent.Controls.Remove(ctrl)

End If

Else

Dim literal As LiteralControl = New LiteralControl()

ctrl.Parent.Controls.Add(literal)

literal.Text =
CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl, Nothing),
System.String)

ctrl.Parent.Controls.Remove(ctrl)

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*******@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.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_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click 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(DataGrid1)
DataGrid1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())

Response.End()
End Sub

Private Sub ClearControls(ByVal ctrl As Control)
Dim i As Int32

For i = ctrl.Controls.Count - 1 To 0 Step -1
ClearControls(ctrl.Controls(i))
Next

If ctrl.GetType().ToString() <> "TableCell" Then
If Not ctrl.GetType().GetProperty("SelectedItem") Is Nothing Then
Dim literal As LiteralControl = New LiteralControl()
ctrl.Parent.Controls.Add(literal)
Try
literal.Text = CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl,
Nothing), System.String) Catch
End Try
ctrl.Parent.Controls.Remove(ctrl)
End If
Else
Dim literal As LiteralControl = New LiteralControl()
ctrl.Parent.Controls.Add(literal)
literal.Text = CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl, Nothing),
System.String) ctrl.Parent.Controls.Remove(ctrl)
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*******@discussions.microsoft.com> wrote in message
news:8C**********************************@microsof t.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.ToString(
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_click line, to reflect the button I am using. It's
radio button

The code I have executing follows

Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object
ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChange

Response.Clear(

Response.Buffer = Tru

Response.ContentType = "application/vnd.ms-excel

Response.Charset = "

Me.EnableViewState = Fals

Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter(

Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = Ne
System.Web.UI.HtmlTextWriter(oStringWriter

Me.ClearControls(DataGrid1

DataGrid1.RenderControl(oHtmlTextWriter

Response.Write(oStringWriter.ToString()

Response.End(

End Su

Private Sub ClearControls(ByVal ctrl As Control

Dim i As Int3

For i = ctrl.Controls.Count - 1 To 0 Step -

ClearControls(ctrl.Controls(i)

Nex

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

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

Dim literal As LiteralControl = New LiteralControl(

ctrl.Parent.Controls.Add(literal

Tr

literal.Text
CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl
Nothing), System.String

Catc

End Tr

ctrl.Parent.Controls.Remove(ctrl

End I

Els

Dim literal As LiteralControl = New LiteralControl(

ctrl.Parent.Controls.Add(literal

literal.Text
CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl, Nothing)
System.String

ctrl.Parent.Controls.Remove(ctrl

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*******@discussions.microsoft.com> wrote in messag
news:8A**********************************@microsof t.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_Click(ByVal sender As System.Object, ByVal e A

System.EventArgs) Handles btnConvert.Clic Response.Clear(
Response.Buffer = Tru
Response.ContentType = "application/vnd.ms-excel
Response.Charset = "
Me.EnableViewState = Fals
Dim oStringWriter As System.IO.StringWriter = Ne System.IO.StringWriter(
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = Ne System.Web.UI.HtmlTextWriter(oStringWriter
Me.ClearControls(DataGrid1)

DataGrid1.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.End()

End Sub
Private Sub ClearControls(ByVal ctrl As Control)

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

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

If Not ctrl.GetType().GetProperty("SelectedItem") Is Nothing Then
Dim literal As LiteralControl = New LiteralControl()
ctrl.Parent.Controls.Add(literal)
Try
literal.Text =

CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl,
Nothing), System.String) Catch
End Try
ctrl.Parent.Controls.Remove(ctrl)
End If
Else
Dim literal As LiteralControl = New LiteralControl()
ctrl.Parent.Controls.Add(literal)
literal.Text = CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl, Nothing),
System.String) ctrl.Parent.Controls.Remove(ctrl)
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*******@discussions.microsoft.com> wrote in message news:8C**********************************@microsof t.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**************@TK2MSFTNGP09.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,
enableviewstatemac = 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.ToString() 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(FileNumber As Integer, ParamArray Output() As Object)'.

the expression "Tostring" has its value = ToString "ASP.customerlist_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_click line, to reflect the button I am using. It's a radio button.

The code I have executing follows:

Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged

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(DataGrid1)

DataGrid1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())

Response.End()

End Sub

Private Sub ClearControls(ByVal ctrl As Control)

Dim i As Int32

For i = ctrl.Controls.Count - 1 To 0 Step -1

ClearControls(ctrl.Controls(i))

Next

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

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

Dim literal As LiteralControl = New LiteralControl()

ctrl.Parent.Controls.Add(literal)

Try

literal.Text =
CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl, Nothing), System.String)

Catch

End Try

ctrl.Parent.Controls.Remove(ctrl)

End If

Else

Dim literal As LiteralControl = New LiteralControl()

ctrl.Parent.Controls.Add(literal)

literal.Text =
CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl, Nothing), System.String)

ctrl.Parent.Controls.Remove(ctrl)

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*******@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.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_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
> 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(DataGrid1)

> DataGrid1.RenderControl(oHtmlTextWriter)
>> Response.Write(oStringWriter.ToString())
>> Response.End()

> End Sub
>> Private Sub ClearControls(ByVal ctrl As Control)

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

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

> If Not ctrl.GetType().GetProperty("SelectedItem") Is

Nothing Then > Dim literal As LiteralControl = New LiteralControl()
> ctrl.Parent.Controls.Add(literal)
> Try
> literal.Text =

CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl, Nothing), System.String)
> Catch
> End Try
> ctrl.Parent.Controls.Remove(ctrl)
> End If
> Else
> Dim literal As LiteralControl = New LiteralControl()
> ctrl.Parent.Controls.Add(literal)
> literal.Text = CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl,

Nothing), System.String)
> ctrl.Parent.Controls.Remove(ctrl)
> 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*******@discussions.microsoft.com> wrote in
message > news:8C**********************************@microsof t.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**************@TK2MSFTNGP09.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,
enableviewstatemac = 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.ToString() 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(FileNumber As Integer, ParamArray Output() As Object)'.

the expression "Tostring" has its value = ToString "ASP.customerlist_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_click line, to reflect the button I am using.
It's a
radio button.

The code I have executing follows:

Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles

RbtnExport.SelectedIndexChanged
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(DataGrid1)

DataGrid1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())

Response.End()

End Sub

Private Sub ClearControls(ByVal ctrl As Control)

Dim i As Int32

For i = ctrl.Controls.Count - 1 To 0 Step -1

ClearControls(ctrl.Controls(i))

Next

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

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

Dim literal As LiteralControl = New LiteralControl()

ctrl.Parent.Controls.Add(literal)

Try

literal.Text =

CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl,
Nothing), System.String)

Catch

End Try

ctrl.Parent.Controls.Remove(ctrl)

End If

Else

Dim literal As LiteralControl = New LiteralControl()

ctrl.Parent.Controls.Add(literal)

literal.Text =
CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl,

Nothing),
System.String)

ctrl.Parent.Controls.Remove(ctrl)

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*******@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.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_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles btnConvert.Click
> 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(DataGrid1)
> DataGrid1.RenderControl(oHtmlTextWriter)
>> Response.Write(oStringWriter.ToString())
>> Response.End()
> End Sub
>> Private Sub ClearControls(ByVal ctrl As Control)
> Dim i As Int32
>> For i = ctrl.Controls.Count - 1 To 0 Step -1
> ClearControls(ctrl.Controls(i))
> Next
>> If ctrl.GetType().ToString() <> "TableCell" Then
> If Not ctrl.GetType().GetProperty("SelectedItem") Is

Nothing Then > Dim literal As LiteralControl = New LiteralControl()
> ctrl.Parent.Controls.Add(literal)
> Try
> literal.Text =

CType(ctrl.Controls.GetType().GetProperty("Selecte dItem").GetValue(ctrl,
Nothing), System.String)
> Catch
> End Try
> ctrl.Parent.Controls.Remove(ctrl)
> End If
> Else
> Dim literal As LiteralControl = New LiteralControl()
> ctrl.Parent.Controls.Add(literal)
> literal.Text =

CType(ctrl.Controls.GetType().GetProperty("Text"). GetValue(ctrl,

Nothing),
System.String)
> ctrl.Parent.Controls.Remove(ctrl)
> 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*******@discussions.microsoft.com> wrote in

message > news:8C**********************************@microsof t.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
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,...
0
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. ...
1
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...
1
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...
3
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...
7
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...
13
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"...
1
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...
5
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.