OpenXML 操作例子

来源:互联网 发布:js判断是否为function 编辑:程序博客网 时间:2024/04/30 22:27
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.Paragraphs1;
Table table = myDoc.Body.Tables3;
myDoc.Body.Paragraphs.InsertAfter(
paragraph, table);
myDoc.Save();
}
原创粉丝点击