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