Here I am trying to show a
simple example...
.aspx page
Select File Format
<asp:DropDownListid="ddlFile"runat="server">
<asp:ListItemValue=".xls">ms-excel</asp:ListItem>
<asp:ListItemValue=".doc">msword</asp:ListItem>
</asp:DropDownList><br>
<asp:ButtonVisible="True"id="btnExport"runat="server"Text="Export
to File"></asp:Button><br>
<asp:Labelid="Label1"runat="server"></asp:Label>
Stylesheet used (CSSFile.css)
#div1td {
font-family:Verdana, Arial,Helvetica, sans-serif;
font-size:11px;
}
#div1table {
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;
}
#div1th {
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
PrivateSub Page_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
Dim sbAs 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
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
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("</table></div>")
Label1.Text = sb.ToString()
sb.Remove(0, sb.Length)
EndSub
Button Click event used for
exporting to required format
PrivateSub 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 swAs New System.IO.StringWriter
Dim htwAs NewHtmlTextWriter(sw)
Label1.RenderControl(htw)
'Appendg
CSS file
Dim fiAs FileInfo = NewFileInfo(Server.MapPath("scripts/CSSFile.css"))
Dim sbAs New System.Text.StringBuilder
Dim srAs StreamReader =
fi.OpenText()
DoWhile 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()
EndSub
Hope, you all enjoyed. if
there is a better way.. please share with us...