从ASP.NET导出的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.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文件的库,名为Open XML SDK.但是,我发现有点难以使用. 我个人已经使用了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(由Telerik翻译,因为我不懂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. 请注意,上述代码取自2010年撰写的博客文章.图书馆可能会在六年内发生变化. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.net C#一个响应对象上的多个文档
- ASP.NET MVC中的jQuery AJAX响应
- asp.net文件上传解决方案(图片上传、单文件上传、多文件上
- RSS2.0中文规范详解及示例
- asp.net-mvc – 如何添加代码隐藏页面到视图或部分视图
- asp.net-mvc-5 – 使用Glass Mapper的Sitecore 8可编辑字段
- ASP.NET 5 MVC6自定义CSS和Javascript放置约定
- asp.net – 触发OWIN cookie中间件设置为被动身份验证模式的
- 第11章 ASP.NET Web API(WCF宿主+Web API、OWIN宿主+Web A
- asp.net-mvc-2 – 我的Asp.Net C#类如何返回一个json格式