2016-03-31 19 views
6

私のアプリケーションには「単語にエクスポート」機能があります。それは完全に動作します。私は、単語ファイルにエクスポートされたgridviewのコンテンツを使用します。エクスポートされた単語ファイルのヘッダー/フッターをASP.NETから追加

また、ヘッダ&フッターはワードファイルのすべてのページに表示されます
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() 

、yustとして:

は、今私は以下のコードから生成されたエクスポートされたWordファイルのヘッダ/フッタを追加したいですWordのヘッダー/フッター機能を使用します。

それは可能ですか?誰もがこれについてのアイデアを持っていますか?

+0

あなたはResponse.AppendHeaderを試しましたか –

+0

まあ、私はresponse.headerはファイルのメタヘッダーであり、実際のファイルのヘッダーではないと思います。例があれば、私に見せてください。 –

+1

あなたは実際に単語ファイルをエクスポートしていません。 HTMLをエクスポートして.docファイルに保存しているので、Wordを開いて開くことができます。必要なものを達成するためには、特別なMS WordのHTML属性と要素を使用する必要があります。最も簡単な方法は、ヘッダーとフッターを含むWord文書を作成し、それをHTMLファイルとして保存することです。また、Open XML SDKやDocXなど、実際のWordファイルを生成するライブラリに切り替えることも検討してください。 .docファイルにHTMLがないと思われるWordクライアントと混同している可能性のあるファイル。 – mason

答えて

2

あなたがやっていることは、実際にはHTMLファイルを作成し、それにWordが開くことを知っている拡張子を与えることです。実際の.DOCファイルを作成していませんが、WordがHTMLを認識して表示します。

私が探しているHTMLのフレーバーは保存するフレーバーと同じだと思うので、Word 2013で新しい文書を作成し、ヘッダーとフッターを追加してHTMLファイルとして保存しました。 HTMLファイルを調べた後、Wordはそれらを外しているように見えます。ですから、HTMLファイルにヘッダーとフッターを指定する方法があるとは思えません。

あなたができることは、実際のMS Wordファイルを生成することに切り替えることです。これらは、さまざまなWordクライアントやWord同等の機能(Mac版、モバイル版、Libre Officeなど)でサポートされています。

Micrsoftは、Open XML SDKと呼ばれる.DOCXファイルを生成するライブラリを提供しています。しかし、私は使用するのが少し難しいことを発見しました。

私は個人的にはDocXを数回使用しています。ここでは、そのライブラリ(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(私はVB.NETを知らないので、Telerikによって翻訳されたもの)

' 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. 

上記のコードは、2010年に作成されたブログ記事から取られたことに注意してください。図書館は、介入する6年間で変更されている可能性があります。

+0

これを試してみます。 –

+0

@ PiyushKhatri私はGridViewをレンダリングしません。代わりに、基になるデータ(たとえば、GridViewにバインドしたモデルまたはDataTable)をレンダリングします。 [DocXが埋め込みテーブル用に持っているオプション](http://cathalscorner.blogspot.com/2010/06/docx-and-tables.html)を見てください。 – mason

関連する問題