Gridview (and Datagrid) exported to Excel  
Author Message
RGS





PostPosted: Fri May 25 22:34:13 CDT 2007 Top

ASP.Net >> Gridview (and Datagrid) exported to Excel

I have an app that gives a user an option to pull an online report into
Excel.

I have the rows formatted in a couple different colors, with a grey
header & footer.

No matter what colors the alternating (and regular) rows are between the
header & footer, they come out an off-white color.

Is there a way to programatically set the colors of cells when they're
exported to excel?

Second question - on the row above the header in the export page, I put
out a confidentiality message. Is there a way to have the background of
that cell a different color other than white? I want to do something to
highlight it or something, to make it stand out somewhat versus the rest
of the report.


Thanks,

BC

Web Programming153  
 
 
KJ





PostPosted: Fri May 25 22:34:13 CDT 2007 Top

ASP.Net >> Gridview (and Datagrid) exported to Excel Can you tell us how you are exporting to Excel - are you doing it by setting
the Content-Type header and rendering the control inline, or some other
method?



>I have an app that gives a user an option to pull an online report into
>Excel.
>
> I have the rows formatted in a couple different colors, with a grey header
> & footer.
>
> No matter what colors the alternating (and regular) rows are between the
> header & footer, they come out an off-white color.
>
> Is there a way to programatically set the colors of cells when they're
> exported to excel?
>
> Second question - on the row above the header in the export page, I put
> out a confidentiality message. Is there a way to have the background of
> that cell a different color other than white? I want to do something to
> highlight it or something, to make it stand out somewhat versus the rest
> of the report.
>
>
> Thanks,
>
> BC


 
 
Blasting





PostPosted: Tue May 29 08:15:22 CDT 2007 Top

ASP.Net >> Gridview (and Datagrid) exported to Excel KJ - sorry, I forgot to mention that.

I'm doing it by the content-type header & rendering control in-line.

Here's a good bit of the code:


Private Sub RenderGrid()

Dim sFile As String = Session("User") & "-" & Label2.Text
sFile = sFile.Replace("/", "-")

If DataGrid1.Visible = True Then
ClearControls(DataGrid1)
End If

If GridView1.Visible = True Then
ClearControls(GridView1)
End If


Select Case Session("bc")
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)

Label2.RenderControl(hw)
hw.Write(("<br>"))

Label1.RenderControl(hw)

' Get the HTML for the control.
If DataGrid1.Visible = True Then
DataGrid1.RenderControl(hw)
End If

If GridView1.Visible = True Then
GridView1.RenderControl(hw)
End If

' Write the HTML back to the browser.
Response.Write(tw.ToString())
' End the response.
Response.End()
End Sub





BC



> Can you tell us how you are exporting to Excel - are you doing it by setting
> the Content-Type header and rendering the control inline, or some other
> method?
>


>> I have an app that gives a user an option to pull an online report into
>> Excel.
>>
>> I have the rows formatted in a couple different colors, with a grey header
>> & footer.
>>
>> No matter what colors the alternating (and regular) rows are between the
>> header & footer, they come out an off-white color.
>>
>> Is there a way to programatically set the colors of cells when they're
>> exported to excel?
>>
>> Second question - on the row above the header in the export page, I put
>> out a confidentiality message. Is there a way to have the background of
>> that cell a different color other than white? I want to do something to
>> highlight it or something, to make it stand out somewhat versus the rest
>> of the report.
>>
>>
>> Thanks,
>>
>> BC
>
>
 
 
KJ





PostPosted: Tue May 29 10:06:15 CDT 2007 Top

ASP.Net >> Gridview (and Datagrid) exported to Excel The only thing I can think of is that the CSS styles aren't being
applied correctly - maybe try using inline styles (using the style
attribute) and see if that changes things?



> KJ - sorry, I forgot to mention that.
>
> I'm doing it by the content-type header & rendering control in-line.
>
> Here's a good bit of the code:
>
> Private Sub RenderGrid()
>
> Dim sFile As String = Session("User") & "-" & Label2.Text
> sFile = sFile.Replace("/", "-")
>
> If DataGrid1.Visible = True Then
> ClearControls(DataGrid1)
> End If
>
> If GridView1.Visible = True Then
> ClearControls(GridView1)
> End If
>
> Select Case Session("bc")
> 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)
>
> Label2.RenderControl(hw)
> hw.Write(("<br>"))
>
> Label1.RenderControl(hw)
>
> ' Get the HTML for the control.
> If DataGrid1.Visible = True Then
> DataGrid1.RenderControl(hw)
> End If
>
> If GridView1.Visible = True Then
> GridView1.RenderControl(hw)
> End If
>
> ' Write the HTML back to the browser.
> Response.Write(tw.ToString())
> ' End the response.
> Response.End()
> End Sub
>
> BC
>

> > Can you tell us how you are exporting to Excel - are you doing it by setting
> > the Content-Type header and rendering the control inline, or some other
> > method?
>


> >> I have an app that gives a user an option to pull an online report into
> >> Excel.
>
> >> I have the rows formatted in a couple different colors, with a grey header
> >> & footer.
>
> >> No matter what colors the alternating (and regular) rows are between the
> >> header & footer, they come out an off-white color.
>
> >> Is there a way to programatically set the colors of cells when they're
> >> exported to excel?
>
> >> Second question - on the row above the header in the export page, I put
> >> out a confidentiality message. Is there a way to have the background of
> >> that cell a different color other than white? I want to do something to
> >> highlight it or something, to make it stand out somewhat versus the rest
> >> of the report.
>
> >> Thanks,
>
> >> BC