Sometimes we needs to export data to file with applicable styles...
Here am trying to show an simple example...
.aspx page
<asp:DropDownList id="ddlFile" runat="server">
<asp:ListItem Value=".xls">ms-excel</asp:ListItem>
<asp:ListItem Value=".doc">msword</asp:ListItem>
</asp:DropDownList><br>
<asp:Button Visible="True" id="btnExport" runat="server" Text="Export to File"></asp:Button><br>
<asp:Label id="Label1" runat="server"></asp:Label>
Stylesheet used (CSSFile.css)
#div1 td {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
}
#div1 table {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
}
#div1 th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
color:#ffffff;
background-color: #316ac5;
}
.td1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:red;
background-color: LightSteelBlue;
}
Code-Behind page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sb As New System.Text.StringBuilder
sb.Append("<div id='div1'><table border='1' cellpadding='0' cellspacing='0' width='99%' align='center'>")
sb.Append("<tr><th height='20px' colspan='2'>Reports</th></tr><tr><td colspan='2'> </td></tr>")
sb.Append("<tr><td colspan='2' class='td1'><b>1</b></td></tr>")
sb.Append("<tr><td><b>User Name</b></td><td> Name One</td></tr>")
sb.Append("<tr><td><b>Location</b></td><td> Mumbai</td></tr>")
sb.Append("<tr><td><b>Email</b></td><td> name.one@sss.com</td></tr>")
sb.Append("<tr><td colspan='2'> </td></tr>")
sb.Append("<tr><td colspan='2' class='td1'><b>2</b></td></tr>")
sb.Append("<tr><td><b>User Name</b></td><td> Name Two</td></tr>")
sb.Append("<tr><td><b>Location</b></td><td> Delhi</td></tr>")
sb.Append("<tr><td><b>Email</b></td><td> name.two@sss.com</td></tr>")
sb.Append("<tr><td colspan='2'> </td></tr>")
sb.Append("<tr><td colspan='2' class='td1'><b>3</b></td></tr>")
sb.Append("<tr><td><b>User Name</b></td><td> Name Three</td></tr>")
sb.Append("<tr><td><b>Location</b></td><td> Chennai</td></tr>")
sb.Append("<tr><td><b>Email</b></td><td> name.three@sss.com</td></tr>")
sb.Append("</table></div>")
Label1.Text = sb.ToString()
sb.Remove(0, sb.Length)
End Sub
Button Click event used for exporting to required format
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Response.Clear()
Response.Charset = ""
Response.ContentEncoding = System.Text.Encoding.UTF8
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/" & ddlFile.SelectedItem.Text & ddlFile.SelectedValue
Response.AddHeader("content-disposition", "attachment;filename=" & "Report" & ddlFile.SelectedValue)
Dim sw As New System.IO.StringWriter
Dim htw As New HtmlTextWriter(sw)
Label1.RenderControl(htw)
'Appendg CSS file
Dim fi As FileInfo = New FileInfo(Server.MapPath("scripts/CSSFile.css"))
Dim sb As New System.Text.StringBuilder
Dim sr As StreamReader = fi.OpenText()
Do While sr.Peek() >= 0
sb.Append(sr.ReadLine())
Loop
sr.Close()
Response.Write("<html><head><style type='text/css'>" & sb.ToString() & "</style><head>" & sw.ToString() & "</html>")
sw = Nothing
htw = Nothing
Response.Flush()
Response.End()
End Sub