.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Export to Excel/Word with Cascading Style Sheets

Posted By:Suthish Nair       Posted Date: August 29, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article we will see how to export to Excel/Word with cascading style sheets.
 


Sometimes we needs to export data to file with applicable styles...

Here am trying to show an simple example... 

.aspx page
 
Select File Format
 
  ms-excel
  msword



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("
") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("") sb.Append("
Reports
 
1
User Name Name One
Location Mumbai
Email name.one@sss.com
 
2
User Name Name Two
Location Delhi
Email name.two@sss.com
 
3
User Name Name Three
Location Chennai
Email name.three@sss.com
") 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("" & sw.ToString() & "")
        sw = Nothing
        htw = Nothing
        Response.Flush()
        Response.End()
    End Sub

  
Hope, you all enjoyed.. If there is a better way.. please share with us...


 Subscribe to Articles

     

Further Readings:

Responses
Author: harika         Company URL: http://www.dotnetspark.com
Posted Date: August 23, 2013

This code is really awesome.It helped me a lot in solving the issuw i have been facing while exporting to excel..


Thanks a ton :):)

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend