サーバー側の単語自動化プロジェクトに openxml を使用する代わりの方法を探しています。単語のブックマークとテーブルを操作できる機能を備えた他の方法を知っている人はいますか?
1772 次
1 に答える
1
私は現在、会社の単語自動化プロジェクトを開発するプロジェクトを行っており、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 を使用して、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);
于 2012-03-22T10:41:50.603 に答える