Formatting the result of Gridview to Excel

To export gridview to an excel use the following code. There is an event that the formal you want will not display on your excel file. To do this use the style formatting on your gridview


Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim style As String = "<style> .text { mso-number-format:\@; } </script> "
       Response.Clear()
       Response.AddHeader("content-disposition", "attachment;filename=report.xlsx")
       Dim mystringwriter As New IO.StringWriter()
       Dim myhtmltextwriter As New HtmlTextWriter(mystringwriter)
       GridView1.RenderControl(myhtmltextwriter)
       Response.Write(style)
       Response.Write(mystringwriter.ToString())
       Response.End()
   End Sub

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(0).Attributes.Add("class", "text")
        End If
    End Sub


Available formats:
mso-number-format:"0" No Decimals 
mso-number-format:"0\.00" 2 Decimals 
mso-number-format:"mm\/dd\/yy" Date format
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" D -T AMPM 
mso-number-format:"Short Date" 05/06/-2008  
mso-number-format:"Medium Date" 05-jan-2008 
mso-number-format:"Short Time" 8:67
mso-number-format:"Medium Time" 8:67 am 
mso-number-format:"Long Time"  8:67:25:00 
mso-number-format:"Percent" Percent - two decimals
mso-number-format:"0\.E+00" Scientific Notation 
mso-number-format:"\@" Text 
mso-number-format:"\#\ ???\/???" Fractions - up to 3 digits (312/943) 
mso-number-format:"\0022£\0022\#\,\#\#0\.00" £12.76 
mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\ " 2 decimals, negative numbers in red and signed
(1.86   -1.66)
mso-number-format:”\\#\\,\\#\\#0\\.00_\\)\\;\\[Black\\]\\\\(\\#\\,\\#\\#0\\.00\\\\)”   Accounting Format –5,(5)

Comments

Popular Posts