在从ASP.NET导出的word文件中添加页眉/页脚
我的应用程序中有一个“导出到word”功能。它工作得很好。我使用gridview的内容导出到word文件中 现在我想在导出的word文件中添加页眉/页脚,该文件由以下代码生成:在从ASP.NET导出的word文件中添加页眉/页脚,asp.net,vb.net,ms-word,Asp.net,Vb.net,Ms Word,我的应用程序中有一个“导出到word”功能。它工作得很好。我使用gridview的内容导出到word文件中 现在我想在导出的word文件中添加页眉/页脚,该文件由以下代码生成: Dim fileName As String = "Test_" & Format(DateTime.Now, "MMddyyyyhhmmss") & ".doc" Dim sw As New StringWriter() Dim w As New HtmlTextWriter(sw) gvContent
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()
此外,页眉和页脚应该显示在word文件的所有页面中,就像使用word的页眉/页脚功能一样
可能吗?有人对此有想法吗?实际上,您正在创建一个HTML文件,并为其提供一个Word知道要打开的扩展名。您没有创建一个真正的.DOC文件,但是Word会识别并显示其中的HTML 我怀疑它所寻找的HTML的风格与它保存在其中的风格是相同的。因此,我在Word 2013中创建了一个新文档,添加了页眉和页脚,并将其保存为HTML文件。检查完HTML文件后,Word似乎将其排除在外。因此,我怀疑是否有办法在打开的HTML文件中指定页眉和页脚 您可以做的是切换到生成真正的MS Word文件。这些将在各种Word客户端和Word等价物(如Mac版本、移动版本和Libre Office)中提供更好的支持 Micrsoft提供了一个用于生成.DOCX文件的库,名为。然而,我发现这有点难以使用 我个人用过几次。下面是如何使用该库(代码取自): C# VB.NET(由Telerik翻译,因为我不懂VB.NET)
注意,上面的代码取自2010年写的一篇博客文章。在这六年中,库可能发生了变化。您是否尝试过Response.AppendHeaderWell,我认为Response.header是文件的元头,而不是实际的文件头。如果你有任何例子,请告诉我。你实际上并没有导出word文件。您正在导出HTML并将其保存到.doc文件中,以欺骗Word打开它。您需要使用特殊的MS Word HTML属性和元素来完成您想要的任务。最简单的方法可能是创建带有页眉和页脚的Word文档,然后将其另存为HTML文件。或者考虑切换到生成真正的Word文件的库,例如开放的XML SDK或DOX。生成的文件可能会混淆Word客户端,这些客户端不希望在.doc文件中使用HTML。您是对的,我尝试过创建带有页眉/页脚的Word文件,但没有找到任何示例。所以仍然在寻找解决方案。经过快速测试,当我将Word保存为HTML文件时,它实际上从未包含页眉/页脚。也许这意味着它不支持HTML中的页眉和页脚。你最好是像我之前的评论中描述的那样生成真实的Word文件。好的,我会试试这个。我如何在这里添加网格内容?@PiyushKhatri我不会渲染网格视图。相反,我将呈现底层数据(例如,绑定到GridView的模型或DataTable)。调查这件事。
// 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.
' 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.