1
考虑到MS Word的“跟踪更改”模式,是否有任何方法可以使用库DocumentFormat.OpenXML
合并2个word文档。 或者也许有可能使用另一个库?使用DocumentFormat.OpenXml合并word文档(c#)
考虑到MS Word的“跟踪更改”模式,是否有任何方法可以使用库DocumentFormat.OpenXML
合并2个word文档。 或者也许有可能使用另一个库?使用DocumentFormat.OpenXml合并word文档(c#)
您可以使用微软Word
Microsoft.Office.Interop.Word.Document worddoc;
Microsoft.Office.Interop.Word.Application wordApp;
Microsoft.Office.Interop.Word.Range _range;
object file = "1.doc";
wordApp = new Microsoft.Office.Interop.Word.Application();
worddoc = wordApp.Documents.Open(ref file);
_range = worddoc.Content;
Microsoft.Office.Interop.Word.Range rng = _range;
rng.Start = rng.End;
rng.InsertFile("2.doc");
编辑
的非标准API和阅读本Merging-Word-Documents
EDIT2
所以试试这个
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
....
public byte[] OpenAndCombine(List<string> FileNames)
{
List<byte[]> documents = new List<byte[]>();
foreach (string _FileName in FileNames)
documents.Add(File.ReadAllBytes(_FileName));
MemoryStream mainStream = new MemoryStream();
mainStream.Write(documents[0], 0, documents[0].Length);
mainStream.Position = 0;
int pointer = 1;
byte[] ret;
using (WordprocessingDocument mainDocument = WordprocessingDocument.Open(mainStream, true))
{
XElement newBody = XElement.Parse(mainDocument.MainDocumentPart.Document.Body.OuterXml);
for (pointer = 1; pointer < documents.Count; pointer++)
{
WordprocessingDocument tempDocument = WordprocessingDocument.Open(new MemoryStream(documents[pointer]), true);
XElement tempBody = XElement.Parse(tempDocument.MainDocumentPart.Document.Body.OuterXml);
newBody.Add(tempBody);
mainDocument.MainDocumentPart.Document.Body = new Body(newBody.ToString());
mainDocument.MainDocumentPart.Document.Save();
mainDocument.Package.Flush();
}
}
ret = mainStream.ToArray();
mainStream.Close();
mainStream.Dispose();
return (ret);
}
我想避免使用库Interop – Aishi 2012-04-27 08:13:12
我明白为什么)))但你问“可能使用另一个库?” – Likurg 2012-04-27 08:15:50
是的,我提出的问题并不完全准确)) 是否有任何其他库使用它可以完成? (不包括Interop库)如果这是唯一的方法 - 我非常难过这个:( – Aishi 2012-05-02 08:45:20