服务器端字自动化

我正在寻找使用openxml作为服务器端word自动化项目的替代方案。 有没有人知道有任何其他方式让我操纵单词书签和表格?

我目前正在为我的公司开发一个文字自动化项目的项目,我正在使用DocX非常简单和直接的API来使用。 我正在使用的方法是,每当我需要直接使用XML时,这个API在Paragraph类中有一个名为“xml”的属性,它允许您访问底层的xml direclty,以便我可以使用它。 最好的部分是它不破坏xml而不破坏生成的文档。 希望这可以帮助!

使用DocX的示例代码..

XNamespace ns = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; using(DocX doc = DocX.Load(@"c:\temp\yourdoc.docx")) { foreach( Paragraph para in doc.Paragraphs ) { if(para.Xml.ToString().Contains("w:Bookmark")) { if(para.Xml.Element(ns + "BookmarkStart").Attribute("Name").Value == "yourbookmarkname") { // you got to your bookmark, if you want to change the text..then para.Xml.Elements(ns + "t").FirstOrDefault().SetValue("Text to replace.."); } } } } 

专门用于书签的替代API是.. http://mplempleooxml.codeplex.com/

有关如何使用此API从bookmarkstart删除文本到bookmarkend的示例..

  MemoryStream stream = DocumentReader.Copy(string.Format("{0}\\template.docx", TestContext.TestDeploymentDir)); WordprocessingDocument doc = WordprocessingDocument.Open(stream, true); MainDocumentPart mainPart = doc.MainDocumentPart; DocumentWriter writer = new DocumentWriter(mainPart); //Simply Clears all text between bookmarkstart and end writer.PasteText("", "YourBookMarkName"); //Save to the memory stream, and then to a file writer.Save(); DocumentWriter.StreamToFile(string.Format("{0}\\templatetest.docx", GetOutputFolder()), stream); 

将word文档从内存流加载到不同的API中。

 //Loading a document file into memorystream using SimpleOOXML API MemoryStream stream = DocumentReader.Copy(@"c\template.docx"); //Opening it from the memory stream as OpenXML document WordprocessingDocument doc = WordprocessingDocument.Open(stream, true); //Opening it as DocX document for working with DocX Api DocX document = DocX.Load(stream);