[英]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.