Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add header/footer in exported word file from ASP.NET

I have an "export to word" feature in my application. It works perfectly. I use gridview's content exported into word file.

Now I want to add header/footer in exported word file which is generated from the below code:

Dim fileName As String = "Test_" & Format(DateTime.Now, "MMddyyyyhhmmss") & ".doc"
Dim sw As New StringWriter()
Dim w As New HtmlTextWriter(sw)
gvContent.RenderControl(w)
Dim content As String = sw.GetStringBuilder().ToString()
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word"
Response.Write(finalContent)
Response.Flush()
Response.End()

Also the header & footer should be displayed in all pages of the word file, yust as when using Word's header/footer feature.

Is it possible? Does anyone have an idea for this?

like image 232
INDIA IT TECH Avatar asked Mar 31 '16 10:03

INDIA IT TECH


Video Answer


1 Answers

What you're doing is actually creating an HTML file and giving it an extension that Word knows to open. You aren't creating a real .DOC file, but Word will recognize the HTML in and and display it.

I suspect that the flavor of HTML it's looking for is identical to the flavor it saves in. So I created a new document in Word 2013, added headers and footers, and saved it as an HTML file. After inspecting the HTML file, it appears that Word leaves those out. So I doubt that there is a way of specifying headers and footers in HTML files that it opens.

What you can do is switch to generating real MS Word files. These will have better support across various Word clients and Word-equivalents (such as Mac versions, mobile versions, and Libre Office).

Micrsoft provides a library for generating .DOCX files, called Open XML SDK. However, I've found that a bit hard to use.

I personally have used DocX a few times. Here's how you'd accomplish this with that library (code taken from this blog post):

C#

// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
    // Add Header and Footer support to this document.
    document.AddHeaders();
    document.AddFooters();

    // Get the default Header for this document.
    Header header_default = document.Headers.odd;

    // Get the default Footer for this document.
    Footer footer_default = document.Footers.odd;

    // Insert a Paragraph into the default Header.
    Paragraph p1 = header_default.InsertParagraph();
    p1.Append("Hello Header.").Bold();

    // Insert a Paragraph into the document.
    Paragraph p2 = document.InsertParagraph();
    p2.AppendLine("Hello Document.").Bold();

    // Insert a Paragraph into the default Footer.
    Paragraph p3 = footer_default.InsertParagraph();
    p3.Append("Hello Footer.").Bold();

    // Save all changes to this document.
    document.Save();
}// Release this document from memory.

VB.NET (as translated by Telerik because I don't know VB.NET)

' Create a new document.
Using document As DocX = DocX.Create("Test.docx")
    ' Add Header and Footer support to this document.
    document.AddHeaders()
    document.AddFooters()

    ' Get the default Header for this document.
    Dim header_default As Header = document.Headers.odd

    ' Get the default Footer for this document.
    Dim footer_default As Footer = document.Footers.odd

    ' Insert a Paragraph into the default Header.
    Dim p1 As Paragraph = header_default.InsertParagraph()
    p1.Append("Hello Header.").Bold()

    ' Insert a Paragraph into the document.
    Dim p2 As Paragraph = document.InsertParagraph()
    p2.AppendLine("Hello Document.").Bold()

    ' Insert a Paragraph into the default Footer.
    Dim p3 As Paragraph = footer_default.InsertParagraph()
    p3.Append("Hello Footer.").Bold()

    ' Save all changes to this document.
    document.Save()
End Using
' Release this document from memory.

Note that the above code was taken from a blog post written in 2010. The library is likely to have changed in the intervening six years.

like image 144
mason Avatar answered Oct 13 '22 14:10

mason