Click here to Skip to main content
16,020,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
How to add header and footer when export gridview to word document using asp.net and C#.

thanks


Edit /

http://img.prntscr.com/img?url=http://i.imgur.com/rPWToFj.png[^]
Posted
Updated 6-Apr-15 2:13am
v2
Comments
[no name] 4-Apr-15 5:48am    
You want to export gridview whole style with word document ?
araskurd 4-Apr-15 5:58am    
yes, i want add header and footer
[no name] 4-Apr-15 6:17am    
check my solution,we can convert gridview in word,pdf,etc using c#.
[no name] 4-Apr-15 9:55am    
Your problem is solved?
araskurd 5-Apr-15 1:24am    
no, i want add header and footer to word document.

 
Share this answer
 
This assumes you are simply taking HTML and sending it out as content type ms-word. Add something like this to your HTML document:

XML
<div style='mso-element:header' id=h1>
    <p class=MsoHeader style='border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0in;mso-padding-alt:0in 0in 1.0pt 0in'>
        <o:p>Some Header  Footer Text</o:p>
    </p>
    <!-- Some paging and date stuff -->
    <font size='2'>Page
        <span style='mso-field-code: PAGE '>
            <span style='mso-no-proof:yes'>1</span>
        </span> of
        <span style='mso-field-code: NUMPAGES '></span>
        <span style='mso-tab-count: 9'>
            <span style='mso-field-code:" DATE \\\@ \0022d-MMM-yyyy\0022 "'></span>
    </font>
</div>

<div style='mso-element:footer' id=f1>
    <p class=MsoFooter style='border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0in;mso-padding-alt:0in 0in 1.0pt 0in'>
        <o:p>Some Footer Text</o:p>&nbsp;
    </p>
    <font size='2'>Page
        <span style='mso-field-code: PAGE '>
            <span style='mso-no-proof:yes'>1</span>
        </span> of
        <span style='mso-field-code: NUMPAGES '></span>
        <span style='mso-field-code:" DATE \\\@ \0022d-MMM-yyyy\0022 "'></span>
    </font>
</div>



It defines the header and footer areas. Now, there is one problem with this that I have yet to solve, and that is the header and footer information will also appear in the document itself. Hopefully you can use this as a starting point for a final solution.
 
Share this answer
 
VB
Response.Clear()

Response.Buffer =True

Response.ContentType ="application/vnd.ms-word"

Response.AddHeader("content-disposition",, attachment)t)

Response.ContentEncoding = System.Text.Encoding.UTF8.UTF8

Response.Charset =""

 EnableViewState =False




Dim writer As New System.IO.StringWriter

'Dim html As New System.Web.UI.HtmlTextWriter(writer)

Dim form As New HtmlForm()

form.Controls.Add(tblTopAllRL)

If RLid = "001" Then

   tblRL1_1case.Visible = True

    form.Controls.Add(tblRL1_1case)

    form.Controls.Add(tblNote2Print)

    form.Controls.Add(tblImgKKHPrint)

    form.Controls.Add(tblSincerely)

ElseIf RLid = "002" Then

    tblRL1_Mcase.Visible = True

    gvAllCase.Visible = True

    form.Controls.Add(tblRL1_Mcase)

    form.Controls.Add(tblNote2Print)

    form.Controls.Add(tblImgKKHPrint)

    form.Controls.Add(tblSincerely)

    form.Controls.Add(gvAllCase)

End If

form.Controls.Add(tblNote2)

Me.Controls.Add(form)

form.RenderControl(html)

 Response.Write(writer)




Response.End()
 
Share this answer
 
Instead of trying to export data from gridview consider template based approach: you first create Word template and set placeholders for data and then at runtime merge the data with the template. This way you have complete control over formatting, including headers and footers, as in your case. Everything that is supported in Word, you can include in template. Typical uses include tables (including nesting), charts, advanced formatting and conditional content based on business logic of displayed data.

One of the tools that you can use to achieve this is Docentric Toolkit.

If you still prefer a generic solution, you can also use Docentric Toolkit and use its APIs to create the document programmatically: in your scenario that would mean to create a new document, include headers and footers and then create a table with the same number of columns as your gridview, which data you want to export. Then you fill the table with data from the gridview.
 
Share this answer
 
v2
Comments
CHill60 6-May-15 18:02pm    
Expensive though

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900