Making A Dynaically Created Excel Report Downloadable
Posted
by Nick LaMarca
on Stack Overflow
See other posts from Stack Overflow
or by Nick LaMarca
Published on 2010-04-06T15:11:29Z
Indexed on
2010/04/06
15:13 UTC
Read the original article
Hit count: 171
I have 2 blocks of code, if someone could help me put them together I would get the functionality I am looking for. The first block of code downloads a gridview to excel using the download dialog I am looking for: Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) ' Verifies that the control is rendered End Sub
Private Sub ExportToExcel(ByVal filename As String, ByVal gv As GridView, ByVal numOfCol As Integer)
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", filename))
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
gv.AllowPaging = False
gv.DataBind()
'Change the Header Row back to white color
gv.HeaderRow.Style.Add("background-color", "#FFFFFF")
For i As Integer = 0 To numOfCol - 1
gv.HeaderRow.Cells(i).Style.Add("background-color", "blue")
gv.HeaderRow.Cells(i).Style.Add("color", "#FFFFFF")
Next
For i As Integer = 0 To gv.Rows.Count - 1
Dim row As GridViewRow = gv.Rows(i)
'Change Color back to white
row.BackColor = System.Drawing.Color.White
For j As Integer = 0 To numOfCol - 1
row.Cells(j).Style.Add("text-align", "center")
Next
'Apply text style to each Row
row.Attributes.Add("class", "textmode")
'Apply style to Individual Cells of Alternating Row
If i Mod 2 <> 0 Then
For j As Integer = 0 To numOfCol - 1
row.Cells(j).Style.Add("background-color", "#CCFFFF")
row.Cells(j).Style.Add("text-align", "center")
'#C2D69B
'row.Cells(j).Style.Add("font-size", "12pt")
Next
End If
Next
gv.RenderControl(hw)
'style to format numbers to string
Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Sub
The second block of code is a sample report I am wish to be downloaded. So instead of downloading a gridview I want this function to accept a worksheet object.
© Stack Overflow or respective owner