简体   繁体   English

创建2013 Word文档(C#)

[英]Create a 2013 Word Document ( C#)

I am using the following code to generate a MS Word Document successfully, but when I save the document it saves as 97-2003 document instead of 2013 Document. 我正在使用以下代码成功生成MS Word文档,但是当我保存文档时,它将另存为97-2003文档而不是2013 Document。

 strHTMLContent.Append("<html xmlns:o='urn:schemas-microsoft-com:office:office'
             xmlns:w='urn:schemas-microsoft-com:office:word'
           xmlns:m='http://schemas.microsoft.com/office/word/2012/wordml'
           xmlns='http://www.w3.org/TR/REC-html40'>")
 .Append("<head><title></title>");   

  strHTMLContent.Append("<[if gte mso 9]>")
                        .Append("<xml>")
                        .Append("<w:wordDocument xmlns:w='http://schemas.microsoft.com/office/word/2012/wordml'>")
                        .Append("<w:View>Print</w:View>")
                        .Append("<w:Zoom>90</w:Zoom>  ")
                        .Append("<w:Compatibility>")
                        .Append("<w:BreakWrappedTables/>")
                        .Append("<w:SnapToGridInCell/>")
                        .Append("<w:WrapTextWithPunct/>")
                        .Append("<w:UseAsianBreakRules/>")
                        .Append("<w:DontGrowAutofit/>")
                        .Append("</w:Compatibility>")
                        .Append("<w:wordDocument xmlns:w='http://schemas.microsoft.com/office/word/2012/wordml'>")
                        .Append("</xml>")
                        .Append("<![endif]");

The easiest option is to use Office interop or a wrapper around the Office interop libraries (like DocX available through NuGet). 最简单的选择是使用Office互操作或Office互操作库周围的包装器(例如可通过NuGet获得的DocX)。 You import Microsoft.Office.Interop.Word , create an application object (set its properties to what is desired, I imagine ideally a big one would be to make the Visible property false). 导入Microsoft.Office.Interop.Word ,创建一个应用程序对象(将其属性设置为所需的属性,我想理想情况下,将Visible属性设置为false是一个很大的方法)。 You create a document, then save it using the application object. 您创建一个文档,然后使用应用程序对象将其保存。

That's my summary of a guide here: http://www.c-sharpcorner.com/uploadfile/muralidharan.d/how-to-create-word-document-using-c-sharp/ 那是我在这里的指南摘要: http : //www.c-sharpcorner.com/uploadfile/muralidharan.d/how-to-create-word-document-using-c-sharp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM