Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Export to Excel/Word with Cascading Style Sheets (css)

0.00/5 (No votes)
19 Jan 2010 1  
Sometimes we needs to export data to file with applicable styles...Here am trying to show an simple example... .aspx page<asp:DropDownList

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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'>&nbsp;</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>&nbsp;Name One</td></tr>")
   sb.Append("<tr><td><b>Location</b></td><td>&nbsp;Mumbai</td></tr>")
   sb.Append("<tr><td><b>Email</b></td><td>&nbsp;name.one@sss.com</td></tr>")
   sb.Append("<tr><td colspan='2'>&nbsp;</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>&nbsp;Name Two</td></tr>")
   sb.Append("<tr><td><b>Location</b></td><td>&nbsp;Delhi</td></tr>")
   sb.Append("<tr><td><b>Email</b></td><td>&nbsp;name.two@sss.com</td></tr>")
   sb.Append("<tr><td colspan='2'>&nbsp;</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>&nbsp;Name Three</td></tr>")
   sb.Append("<tr><td><b>Location</b></td><td>&nbsp;Chennai</td></tr>")
   sb.Append("<tr><td><b>Email</b></td><td>&nbsp;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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here