Link to home
Start Free TrialLog in
Avatar of NursingCorp
NursingCorpFlag for United States of America

asked on

ASP.NET Form Export to Word CSS Settings

Hello Experts,

I have a button click event that will export my datagrid to a word document. The code works to bring the information out, but it is not applying my css styles that I have defined. Is there a way that I can replace the css style that is created with the export my css defined in the /Portals/0/portal.css file. (see attached code for button click).
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Response.AddHeader("content-disposition", "attachment;filename=Export.doc")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType = "application/vnd.word"

        Dim stringWrite As New System.IO.StringWriter()
        Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
        Dim fi As FileInfo = New FileInfo(Server.MapPath("Portals/0/portal.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()

        ' Create a form to contain the grid
        Dim frm As New HtmlForm()
        applicationGrid.Parent.Controls.Add(frm)
        frm.Attributes("runat") = "server"
        frm.Controls.Add(applicationGrid)
        frm.RenderControl(htmlWrite)

        Response.Write(stringWrite.ToString())
        Response.[End]()
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NursingCorp

ASKER

I ended up iterating through the grid and placing each row into a string, then assigning that string to a Label, and using the code from the link provided to pull in my style sheet.