AlternativeFormatImportPart

AlternativeFormatImportPart

本文介绍了合并多个Word文档到一个开放的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用生成的Open XML和其他的东西约10 Word文档。
现在我想创建另一个Word文档,一个接一个,我想他们加入到这个新创建的文档。
我想使用Open XML,任何暗示将AP preciable。
下面是我的code:

 私人无效CreateSampleWordDocument()
    {
        //字符串的SourceFile = Path.Combine(D:\\\\ GeneralLetter.dot);
        //字符串destinationFile = Path.Combine(D:\\\\ New.doc);
        字符串的SourceFile = Path.Combine(D:\\\\ GeneralWelcomeLetter.docx);
        字符串destinationFile = Path.Combine(D:\\\\ New.docx);
        尝试
        {
            //创建模板文件的副本,然后打开副本
            //File.Copy(sourceFile,destinationFile,真正的);
            使用(Wordpro​​cessingDocument文件= Wordpro​​cessingDocument.Open(destinationFile,真))
            {
                //更改文档类型文档
                document.ChangeDocumentType(DocumentFormat.OpenXml.Wordpro​​cessingDocumentType.Document);
                //获取文件的主要部分
                MainDocumentPart mainPart = document.MainDocumentPart;
                mainPart.Document.Save();
            }
        }
        抓住
        {
        }
    }

更新(使用AltChunks):

 使用(Wordpro​​cessingDocument myDoc = Wordpro​​cessingDocument.Open(D:\\\\ Test.docx,真))
        {
            。串altChunkId =AltChunkId+ DateTime.Now.Ticks.ToString()子串(0,2);
            MainDocumentPart mainPart = myDoc.MainDocumentPart;
            AlternativeFor​​matImportPart块= mainPart.AddAlternativeFor​​matImportPart(
                AlternativeFor​​matImportPartType.Wordpro​​cessingML,altChunkId);
            使用(的FileStream FILESTREAM = File.Open(D:\\\\ Test1.docx,FileMode.Open))
                chunk.FeedData(FILESTREAM);
            AltChunk altChunk =新AltChunk();
            altChunk.Id = altChunkId;
            mainPart.Document
                。身体
                .InsertAfter(altChunk,mainPart.Document.Body.Elements<&款GT;()最后());
            mainPart.Document.Save();
        }

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

 使用(Wordpro​​cessingDocument myDoc = Wordpro​​cessingDocument.Open(D:\\\\ Test.docx,真))
        {            MainDocumentPart mainPart = myDoc.MainDocumentPart;
            。串altChunkId =AltChunkId+ DateTime.Now.Ticks.ToString()子串(0,3);
            AlternativeFor​​matImportPart块= mainPart.AddAlternativeFor​​matImportPart(AlternativeFor​​matImportPartType.Wordpro​​cessingML,altChunkId);
            使用(的FileStream FILESTREAM = File.Open(D:\\\\ Test1.docx,FileMode.Open))
            {
                chunk.FeedData(FILESTREAM);
                AltChunk altChunk =新AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document
                    。身体
                    .InsertAfter(altChunk,mainPart.Document.Body
                    .Elements<&款GT;()最后());
                mainPart.Document.Save();
            }
            使用(的FileStream FILESTREAM = File.Open(D:\\\\ Test2.docx,FileMode.Open))
            {
                chunk.FeedData(FILESTREAM);
                AltChunk altChunk =新AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document
                    。身体
                    .InsertAfter(altChunk,mainPart.Document.Body
                    .Elements<&款GT;()最后());
            }
            使用(的FileStream FILESTREAM = File.Open(D:\\\\ Test3.docx,FileMode.Open))
            {
                chunk.FeedData(FILESTREAM);
                AltChunk altChunk =新AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document
                    。身体
                    .InsertAfter(altChunk,mainPart.Document.Body
                    .Elements<&款GT;()最后());
            }
        }

这code两次追加Test2的数据,到位的Test1的数据也是如此。
意味着我得到:

 测试
TEST2
TEST2

而不是:

 测试
测试1
TEST2


解决方案

仅使用OpenXML的SDK,可以使用<$c$c>AltChunk元件的多个文件合并成一个。

此链接the-easy-way-to-assemble-multiple-word-documents而这其中的一些样品。

修改1

基于使用 altchunk 在更新的问题,您的code

(更新#1)的,这里是VB.Net $ C $三我已经测试和工程就像一个魅力对我来说:

 使用myDoc = DocumentFormat.OpenXml.Packaging.Wordpro​​cessingDocument.Open(D:\\\\ Test.docx,真)
        暗淡altChunkId =AltChunkId+ DateTime.Now.Ticks.ToString()。子串(0,2)
        昏暗mainPart = myDoc.MainDocumentPart
        点心块= mainPart.AddAlternativeFor​​matImportPart(
            DocumentFormat.OpenXml.Packaging.AlternativeFor​​matImportPartType.Wordpro​​cessingML,altChunkId)
        使用FILESTREAM作为IO.FileStream = IO.File.Open(D:\\\\ Test1.docx,IO.FileMode.Open)
            chunk.FeedData(FILESTREAM)
        使用完
        昏暗altChunk =新DocumentFormat.OpenXml.Wordpro​​cessing.AltChunk()
        altChunk.Id = altChunkId
        mainPart.Document.Body.InsertAfter(altChunk,mainPart.Document.Body.Elements(中DocumentFormat.OpenXml.Wordpro​​cessing.Paragraph)。去年())
        mainPart.Document.Save()
使用完

编辑2

第二个问题的(更新#2)

is related to altchunkid.

For each document you want to merge in the main document, you need to:

  1. add an AlternativeFormatImportPart in the mainDocumentPart with an Id which must to be unique. This element contains the Inserted data
  2. add in the body an Altchunk element in which you set the id to reference the previous AlternativeFormatImportPart.

In your code, you are using the same Id for all the AltChunks. It's why you see many time the same text.

I am not sure the altchunkid will be unique with your code: string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 2);

If you don't need to set a specific value, I recommend you to not set explicitly the AltChunkId when you add the AlternativeFormatImportPart. Instead, you get one generated by the SDK like this:

VB.Net

Dim chunk As AlternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML)
Dim altchunkid As String = mainPart.GetIdOfPart(chunk)

C#

AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML);
string altchunkid = mainPart.GetIdOfPart(chunk);

这篇关于合并多个Word文档到一个开放的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:04