首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将多个word文档合并为一个Open Xml

将多个word文档合并为一个Open Xml
EN

Stack Overflow用户
提问于 2013-08-21 15:55:24
回答 4查看 33.8K关注 0票数 22

我有大约10个word文档,我使用open xml和其他东西生成。现在我想创建另一个word文档,一个接一个地将它们加入到这个新创建的文档中。我希望使用开放的xml,任何提示都会很有用。下面是我的代码:

代码语言:javascript
复制
 private void CreateSampleWordDocument()
    {
        //string sourceFile = Path.Combine("D:\\GeneralLetter.dot");
        //string destinationFile = Path.Combine("D:\\New.doc");
        string sourceFile = Path.Combine("D:\\GeneralWelcomeLetter.docx");
        string destinationFile = Path.Combine("D:\\New.docx");
        try
        {
            // Create a copy of the template file and open the copy
            //File.Copy(sourceFile, destinationFile, true);
            using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true))
            {
                // Change the document type to Document
                document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
                //Get the Main Part of the document
                MainDocumentPart mainPart = document.MainDocumentPart;
                mainPart.Document.Save();
            }
        }
        catch
        {
        }
    }

更新(使用AltChunks):

代码语言:javascript
复制
using (WordprocessingDocument myDoc = WordprocessingDocument.Open("D:\\Test.docx", true))
        {
            string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 2) ;
            MainDocumentPart mainPart = myDoc.MainDocumentPart;
            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                AlternativeFormatImportPartType.WordprocessingML, altChunkId);
            using (FileStream fileStream = File.Open("D:\\Test1.docx", FileMode.Open))
                chunk.FeedData(fileStream);
            AltChunk altChunk = new AltChunk();
            altChunk.Id = altChunkId;
            mainPart.Document
                .Body
                .InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
            mainPart.Document.Save();
        } 

当我使用多个文件时,为什么这段代码会覆盖最后一个文件的内容?更新2:

代码语言:javascript
复制
 using (WordprocessingDocument myDoc = WordprocessingDocument.Open("D:\\Test.docx", true))
        {

            MainDocumentPart mainPart = myDoc.MainDocumentPart;
            string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 3);
            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
            using (FileStream fileStream = File.Open("d:\\Test1.docx", FileMode.Open))
            {
                chunk.FeedData(fileStream);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document
                    .Body
                    .InsertAfter(altChunk, mainPart.Document.Body
                    .Elements<Paragraph>().Last());
                mainPart.Document.Save();
            }
            using (FileStream fileStream = File.Open("d:\\Test2.docx", FileMode.Open))
            {
                chunk.FeedData(fileStream);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document
                    .Body
                    .InsertAfter(altChunk, mainPart.Document.Body
                    .Elements<Paragraph>().Last());
            }
            using (FileStream fileStream = File.Open("d:\\Test3.docx", FileMode.Open))
            {
                chunk.FeedData(fileStream);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document
                    .Body
                    .InsertAfter(altChunk, mainPart.Document.Body
                    .Elements<Paragraph>().Last());
            } 
        }

这段代码两次添加Test2数据,也替换了Test1数据。意味着我得到了:

代码语言:javascript
复制
Test
Test2
Test2

而不是:

代码语言:javascript
复制
Test
Test1
Test2
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-08-21 16:30:54

只使用openXML SDK,您可以使用AltChunk元素将多个文档合并为一个文档。

这个链接the-easy-way-to-assemble-multiple-word-documents和这个How to Use altChunk for Document Assembly提供了一些示例。

编辑1个

根据您在更新的问题(update#1)中使用altchunk的代码,以下是我测试过的VB.Net代码,它对我来说非常有用:

代码语言:javascript
复制
Using myDoc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open("D:\\Test.docx", True)
        Dim altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 2)
        Dim mainPart = myDoc.MainDocumentPart
        Dim chunk = mainPart.AddAlternativeFormatImportPart(
            DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML, altChunkId)
        Using fileStream As IO.FileStream = IO.File.Open("D:\\Test1.docx", IO.FileMode.Open)
            chunk.FeedData(fileStream)
        End Using
        Dim altChunk = New DocumentFormat.OpenXml.Wordprocessing.AltChunk()
        altChunk.Id = altChunkId
        mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph).Last())
        mainPart.Document.Save()
End Using

编辑2个

第二个问题(update#2)

这段代码两次附加Test2数据,也替换了Test1数据。

altchunkid相关。

对于要在主文档中合并的每个文档,您需要:

  1. 使用必须唯一的mainDocumentPart中添加一个。此元素包含插入的数据
  2. 在正文中添加一个Altchunk元素,您可以在该元素中设置id以引用以前的AlternativeFormatImportPart.

在您的代码中,您对所有AltChunks使用相同的Id。这就是为什么你会多次看到相同的文本。

我不确定altchunkid在您的代码中是否是唯一的:string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 2);

如果您不需要设置特定值,我建议您在添加AlternativeFormatImportPart时不要显式设置AltChunkId。相反,您会得到一个由SDK生成的代码,如下所示:

VB.Net

代码语言:javascript
复制
Dim chunk As AlternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML)
Dim altchunkid As String = mainPart.GetIdOfPart(chunk)

C#

代码语言:javascript
复制
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML);
string altchunkid = mainPart.GetIdOfPart(chunk);
票数 20
EN

Stack Overflow用户

发布于 2013-08-21 16:17:27

在open xml周围有一个很好的包装器API (Document Builder2.2),专门为合并文档而设计,可以灵活地选择要合并的段落等。你可以从here下载它(更新:移动到github)。

关于如何使用它的文档和屏幕投射是here

更新:代码示例

代码语言:javascript
复制
 var sources = new List<Source>();
 //Document Streams (File Streams) of the documents to be merged.
 foreach (var stream in documentstreams)
 {
        var tempms = new MemoryStream();
        stream.CopyTo(tempms);
        sources.Add(new Source(new WmlDocument(stream.Length.ToString(), tempms), true));
 }

  var mergedDoc = DocumentBuilder.BuildDocument(sources);
  mergedDoc.SaveAs(@"C:\TargetFilePath");

类型SourceWmlDocument来自Document Builder API。

如果你选择这样做,你甚至可以直接添加文件路径:

代码语言:javascript
复制
sources.Add(new Source(new WmlDocument(@"C:\FileToBeMerged1.docx"));
sources.Add(new Source(new WmlDocument(@"C:\FileToBeMerged2.docx"));

在合并文档的AltChunkDocument Builder方法之间找到了这个Nice Comparison --这对根据需求进行选择很有帮助。

您也可以使用DocX库来合并文档,但我更喜欢使用Document Builder来合并文档。

希望这能有所帮助。

票数 12
EN

Stack Overflow用户

发布于 2020-03-05 15:24:17

这些答案中唯一缺少的是for循环。

对于那些只想复制/粘贴它的人:

代码语言:javascript
复制
void MergeInNewFile(string resultFile, IList<string> filenames)
{
    using (WordprocessingDocument document = WordprocessingDocument.Create(resultFile, WordprocessingDocumentType.Document))
    {
        MainDocumentPart mainPart = document.AddMainDocumentPart();
        mainPart.Document = new Document(new Body());

        foreach (string filename in filenames)
        {
            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);
            string altChunkId = mainPart.GetIdOfPart(chunk);

            using (FileStream fileStream = File.Open(filename, FileMode.Open))
            {
                chunk.FeedData(fileStream);
            }

            AltChunk altChunk = new AltChunk { Id = altChunkId };
            mainPart.Document.Body.AppendChild(altChunk);
        }

        mainPart.Document.Save();
    }
}

所有的荣誉都归Chris和yonexbat所有

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18351829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档