2012-03-21 88 views
0

我正在尋找使用openxml作爲服務器端字自動化項目的替代方案。有沒有人知道任何其他方式有功能,讓我操縱字書籤和表?服務器端字自動化

+0

你在試圖自動化什麼,爲什麼openxml不適合你的需求,沒有這些細節,將很難提供其他選項。 – 2012-03-22 14:17:40

+0

我正試圖刪除書籤開始和bookmarkend內的所有文本。就我所知,openxml不允許我這樣做 – 2012-03-22 14:22:23

+0

現在知道你想要做什麼看到這個問題,它基本上是你想要做的同樣的事情。 http://stackoverflow.com/questions/3308299/replace-bookmark-text-in-word-file-using-open-xml-sdk – 2012-03-22 14:34:00

回答

1

我目前正在爲我公司開發一個字自動化項目的項目,我正在使用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://simpleooxml.codeplex.com/

如何刪除bookmarkstart文本使用這個API 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); 
+0

我能使用docX訪問word doc中的書籤嗎? – 2012-03-22 13:31:59

+0

不是開箱即用......但你可以直接從底層XML獲得它..取決於你的要求.. – Flowerking 2012-03-22 13:40:10

+0

你可以舉一個例子,我可以如何使用XML來獲取書籤 – 2012-03-22 13:48:56