[英]Server-side word automation
我正在尋找將openxml用於服務器端單詞自動化項目的替代方法。 有誰知道其他方法可以使我操作單詞書簽和表格嗎?
我目前正在為我的公司開發一個單詞自動化項目,並且正在使用DocX非常簡單直接的API。 我使用的方法是,每當我需要直接使用XML時,此API在Paragraph類中都有一個名為“ xml”的屬性,該屬性使您可以訪問基礎xml目錄,以便我可以使用它。 最好的部分是不破壞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/
有關如何使用此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);
將單詞文檔從內存流加載到不同的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.