Click here to Skip to main content
15,881,424 members
Articles / General Programming / File
Article

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 16.4K   2  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --