简体   繁体   English

服务器端单词自动化

[英]Server-side word automation

I am looking for alternatives to using openxml for a server-side word automation project. 我正在寻找将openxml用于服务器端单词自动化项目的替代方法。 Does anyone know any other ways that have features to let me manipulate word bookmarks and tables? 有谁知道其他方法可以使我操作单词书签和表格吗?

I am currently doing a project of developing a word automation project for my company and I am using DocX Very simple and straight forward API to work with. 我目前正在为我的公司开发一个单词自动化项目,并且正在使用DocX非常简单直接的API。 The approach I am using is, whenever I need to work with XML directly, this API has a property named "xml" in the Paragraph class which gives you access to the underlying xml direclty so that I can work with it. 我使用的方法是,每当我需要直接使用XML时,此API在Paragraph类中都有一个名为“ xml”的属性,该属性使您可以访问基础xml目录,以便我可以使用它。 The best part is its not breaking the xml and not corrupting the resulting document. 最好的部分是不破坏xml且不破坏生成的文档。 Hope this helps! 希望这可以帮助!

Example code using DocX.. 使用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..");
                  }
             }
         }
    }

Alternative API exclusively to work with bookmarks is .. http://simpleooxml.codeplex.com/ 专门用于书签的替代API是.. http://simpleooxml.codeplex.com/

Example on how to delete text from bookmarkstart to bookmarkend using this API.. 有关如何使用此API将文本从书签开始删除到书签结束的示例。

 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);

Loading the word document into different API's from memory stream. 将单词文档从内存流加载到不同的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); 

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

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