Việc bạn đang thực sự đang tạo một tệp HTML và cung cấp cho nó phần mở rộng mà Word biết để mở. Bạn không tạo tệp .DOC thực, nhưng Word sẽ nhận dạng HTML và hiển thị nó.
Tôi nghi ngờ rằng hương vị của HTML mà nó tìm kiếm giống với hương vị mà nó lưu. Vì vậy, tôi đã tạo một tài liệu mới trong Word 2013, thêm đầu trang và chân trang và lưu nó dưới dạng tệp HTML. Sau khi kiểm tra tệp HTML, có vẻ như Word đã loại bỏ chúng ra. Vì vậy, tôi nghi ngờ rằng có một cách để chỉ định đầu trang và chân trang trong các tệp HTML mà nó mở ra.
Điều bạn có thể làm là chuyển sang tạo tệp MS Word thực. Những ứng dụng này sẽ hỗ trợ tốt hơn cho nhiều ứng dụng Word và Word tương đương (chẳng hạn như phiên bản Mac, phiên bản di động và Libre Office).
Micrsoft cung cấp thư viện để tạo tệp .DOCX, được gọi là Open XML SDK. Tuy nhiên, tôi thấy rằng hơi khó sử dụng.
Cá nhân tôi đã sử dụng DocX một vài lần. Đây là cách bạn muốn thực hiện điều này với thư viện mà (mã lấy từ 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 (như dịch bởi Telerik bởi vì tôi không biết 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.
Lưu ý rằng mã trên được lấy từ một bài đăng trên blog được viết vào năm 2010. Thư viện có thể đã thay đổi trong sáu năm qua.
Bạn đã thử Response.AppendHeader –
Vâng, tôi nghĩ response.header dành cho tiêu đề meta của tệp, chứ không phải tiêu đề của tệp thực tế. Nếu bạn có bất kỳ ví dụ, xin vui lòng chỉ cho tôi. –
Bạn không thực sự xuất tệp tin từ. Bạn đang xuất HTML và lưu nó vào một tệp .doc để lừa Word mở nó ra. Bạn sẽ cần phải sử dụng các thuộc tính và các phần tử HTML Word đặc biệt để thực hiện những gì bạn muốn. Cách dễ nhất có thể là tạo một tài liệu Word với đầu trang và chân trang, sau đó lưu nó dưới dạng tệp HTML. Hoặc xem xét chuyển sang thư viện tạo tệp Word thực, chẳng hạn như Open XML SDK hoặc DocX. Các tệp mà việc tạo của bạn có thể gây nhầm lẫn cho các trình khách Word không mong đợi HTML trong một tệp .doc. – mason