Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Code in ASP to export to Excel - works & doesn't work??? Help!

Reply
Thread Tools

Code in ASP to export to Excel - works & doesn't work??? Help!

 
 
Guest
Posts: n/a
 
      03-04-2004
The following code:

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



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

Dim sFile As String = Session("User") & "-Customer List-" & Today()

sFile = sFile.Replace("/", "-")

ClearControls(DataGrid1)

Select Case RbtnExport.SelectedItem.Value

Case "Excel"

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

Response.AppendHeader("content-disposition", "attachment; filename=" & sFile
& ".xls")

Case "Word"

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

Response.AppendHeader("content-disposition", "attachment; filename=" & sFile
& ".doc")

End Select

' Remove the charset from the Content-Type header.

Response.Charset = ""

' Turn off the view state.

Me.EnableViewState = False

Dim tw As New System.IO.StringWriter()

Dim hw As New System.Web.UI.HtmlTextWriter(tw)

' Get the HTML for the control.

DataGrid1.RenderControl(hw)

' Write the HTML back to the browser.

Response.Write(tw.ToString())

' End the response.

Response.End()

End Sub



I have as you see above EXACTLY in 2 separate aspx pages. One provides a
list of products and inventories, the other displays a list of customers.
The code above is copied from one to the other. The one that displays the
products & inventories works - I get an Excel book I can read - the other
does not - it returns a 0 byte file that produces the "Unable to read file"
message when I try to open it. On each page, I am taking a web page with a
datagrid on it to an excel file.

There are some differences between the 2 pages.

One, the product page does not have sortable columns, while the customer
list did (I took this out of the customer list with no effect, so I put it
back in).

Two, the product listing page is loaded as the result of an If Not
page.ispostback condition in the Page_load event, and does not require any
clicks of boxes or buttons. The Customer listing allows you to make some
selections before the datagrid is loaded (such as by a distribution
channel), and the also allows you to filter the request (so you can pick out
just shipto locations, or soldto information only). Therefore, the customer
list datagrid is generated based on one of 2 things - whether a radio button
that indicates you want all distribution channels displayed, or whether the
index has changed on a combo box. That builds your sql based on your
selections you've made and once you've selected either the All button or the
appropriate combo box item, it'll run the sql and load your datagrid based
on those selections.

You'll then click a radio button to bring it to excel. Both pages function
similarly, except the one where the datagrid is loaded in page_load works,
and the other one returns a zero byte page.

Any ideas as to what's wrong with this puppy?

SC




 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      03-04-2004
Does the bindgrid & the render control have to be done in a new page?

i.e. - in my buttonclick event, I'd transfer control to page2.aspx (via a
response.redirect), and in page2.aspx, I'd have a bindgrid & a render
control routine (similar to what I have in the selectindexchanged sub
below). But that would be all I'd have in my page2.aspx.

What I don't know is (1) if this would fix my problem, and (2) how do you
get the data results (in my case probably a meg's worth of data) into the
new page?

The code I'm looking at is this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
BindGrid()
RenderGrid()
End Sub

Private Sub BindGrid()
Dim dtResults As DataTable = CType(Session("DynamicFormResults"),
DataTable)
If Not dtResults Is Nothing Then
DataGrid1.DataSource = dtResults.DefaultView
DataGrid1.DataBind()
End If
End Sub

Private Sub RenderGrid()
Response.ContentType = "application/vnd.ms-excel"
' Remove the charset from the Content-Type header.
Response.Charset = ""
' Turn off the view state.
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' Get the HTML for the control.
DataGrid1.RenderControl(hw)
' Write the HTML back to the browser.
Response.Write(tw.ToString())
' End the response.
Response.End()
End Sub

which is located here:
http://weblogs.asp.net/dneimke/archi.../27/63348.aspx

Anyone have any ideas?

SC



<(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> The following code:
>
> 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
>
>
>
> Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged
>
> Dim sFile As String = Session("User") & "-Customer List-" & Today()
>
> sFile = sFile.Replace("/", "-")
>
> ClearControls(DataGrid1)
>
> Select Case RbtnExport.SelectedItem.Value
>
> Case "Excel"
>
> Response.ContentType = "application/vnd.ms-excel"
>
> Response.AppendHeader("content-disposition", "attachment; filename=" &

sFile
> & ".xls")
>
> Case "Word"
>
> Response.ContentType = "application/vnd.ms-word"
>
> Response.AppendHeader("content-disposition", "attachment; filename=" &

sFile
> & ".doc")
>
> End Select
>
> ' Remove the charset from the Content-Type header.
>
> Response.Charset = ""
>
> ' Turn off the view state.
>
> Me.EnableViewState = False
>
> Dim tw As New System.IO.StringWriter()
>
> Dim hw As New System.Web.UI.HtmlTextWriter(tw)
>
> ' Get the HTML for the control.
>
> DataGrid1.RenderControl(hw)
>
> ' Write the HTML back to the browser.
>
> Response.Write(tw.ToString())
>
> ' End the response.
>
> Response.End()
>
> End Sub
>
>
>
> I have as you see above EXACTLY in 2 separate aspx pages. One provides a
> list of products and inventories, the other displays a list of customers.
> The code above is copied from one to the other. The one that displays the
> products & inventories works - I get an Excel book I can read - the other
> does not - it returns a 0 byte file that produces the "Unable to read

file"
> message when I try to open it. On each page, I am taking a web page with

a
> datagrid on it to an excel file.
>
> There are some differences between the 2 pages.
>
> One, the product page does not have sortable columns, while the customer
> list did (I took this out of the customer list with no effect, so I put it
> back in).
>
> Two, the product listing page is loaded as the result of an If Not
> page.ispostback condition in the Page_load event, and does not require any
> clicks of boxes or buttons. The Customer listing allows you to make some
> selections before the datagrid is loaded (such as by a distribution
> channel), and the also allows you to filter the request (so you can pick

out
> just shipto locations, or soldto information only). Therefore, the

customer
> list datagrid is generated based on one of 2 things - whether a radio

button
> that indicates you want all distribution channels displayed, or whether

the
> index has changed on a combo box. That builds your sql based on your
> selections you've made and once you've selected either the All button or

the
> appropriate combo box item, it'll run the sql and load your datagrid based
> on those selections.
>
> You'll then click a radio button to bring it to excel. Both pages

function
> similarly, except the one where the datagrid is loaded in page_load works,
> and the other one returns a zero byte page.
>
> Any ideas as to what's wrong with this puppy?
>
> SC
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to Excel (Default File Type - Excel) =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?= ASP .Net 15 05-21-2009 12:01 PM
Export to excel in asp.net using excel template Grey ASP .Net 4 10-17-2007 08:08 AM
using Microsoft Excel image for Export to Excel button =?Utf-8?B?U3JpZGhhcg==?= ASP .Net 0 12-09-2005 08:58 PM
Code in ASP to export to Excel - works & doesn't work??? Help! ASP .Net 1 03-04-2004 10:16 PM
Code in ASP to export to Excel - works & doesn't work??? Help! ASP .Net Web Controls 1 03-04-2004 10:16 PM



Advertisments