OpenXML 操作例子

Creating a new document

using (WordProcessingMLDocument myDoc =
WordProcessingMLDocument.Open(@"C:/myDoc.docx"))
{
Paragraph paragraph = myDoc.Body.Paragraphs.New();
paragraph.AddText("Hello World!");
myDoc.Save();
}

Insert a custom XML file

using (WordProcessingMLDocument myDoc =
WordProcessingMLDocument.Open(@"C:/myDoc.docx"))
{
myDoc.CustomXml.Add(@"C:/customXML.xml");
}

Import a paragraph from a document

using (WordProcessingMLDocument sourceDoc =
WordProcessingMLDocument.Open(@"C:/source.docx"))
using (WordProcessingMLDocument targetDoc =
WordProcessingMLDocument.Open(@"C:/target.docx"))
{
Paragraph paragraph = sourceDoc.Body.Paragraphs 1;
targetDoc.Body.Insert(paragraph);
targetDoc.Save();
}

Move a paragraph inside the document to a specific location (after the third table in this sample)

using (WordProcessingMLDocument myDoc =
WordProcessingMLDocument.Open(@"C:/myDoc.docx"))
{
Paragraph paragraph = myDoc.Body.Paragraphs 1;
Table table = myDoc.Body.Tables 3;
myDoc.Body.Paragraphs.InsertAfter(
paragraph, table);
myDoc.Save();
}

你可能感兴趣的:(OpenXML 操作例子)