Is there a way to export a gridview to MS word and keep the formatting from the gridview. The code below works to export the gridview but I cannot figure out how to keep the formatting. (I cannot use any 3rd party software)
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.RenderControl(hw)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
My best guess is that you are defining most of the Grid’s colors, column alignments etc, via
classesdefined in aCSSfile and when you export, these classes can no longer be referenced.To fix it, you need to use inline styles so that the HTML rendered is self-contained and the colors and other formatting is preserved when the grid is written as a Word document.