将多个word文档合并为一个Open Xml [英] Merge multiple word documents into one Open Xml
问题描述
我有大约 10 个 word 文档,这些文档是使用 open xml 和其他东西生成的.现在我想创建另一个word文档,我想将它们一个一个地加入到这个新创建的文档中.我希望使用 open xml,任何提示都是可观的.下面是我的代码:
I have around 10 word documents which I generate using open xml and other stuff. Now I would like to create another word document and one by one I would like to join them into this newly created document. I wish to use open xml, any hint would be appreciable. Below is my code:
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):
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:
Why this code overwrites the content of the last file when I use multiple files? Update 2:
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 数据.意味着我得到:
This code is appending the Test2 data twice, in place of Test1 data as well. Means I get:
Test
Test2
Test2
而不是:
Test
Test1
Test2
推荐答案
仅使用 openXML SDK,您可以使用 AltChunk
元素将多个文档合并为一个.
Using openXML SDK only, you can use AltChunk
element to merge the multiple document into one.
这个链接the-easy-way-to-assemble-multiple-word-documents 和这个 如何使用 altChunk 进行文档组装 提供了一些示例.
This link the-easy-way-to-assemble-multiple-word-documents and this one How to Use altChunk for Document Assembly provide some samples.
编辑 1
根据您在更新的问题 (update#1) 中使用 altchunk
的代码,这里是我测试过的 VB.Net 代码,它的工作原理很有魅力对我来说:
Based on your code that uses altchunk
in the updated question (update#1), here is the VB.Net code I have tested and that works like a charm for me:
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 数据作为嗯.
This code is appending the Test2 data twice, in place of Test1 data as well.
与 altchunkid
相关.
对于要在主文档中合并的每个文档,您需要:
For each document you want to merge in the main document, you need to:
- 在
mainDocumentPart
中添加一个AlternativeFormatImportPart
,其中Id
必须是唯一的. 此元素包含插入的数据 - 在正文中添加一个
Altchunk
元素,您可以在其中设置id
以引用之前的AlternativeFormatImportPart
.
- add an
AlternativeFormatImportPart
in themainDocumentPart
with anId
which must to be unique. This element contains the Inserted data - add in the body an
Altchunk
element in which you set theid
to reference the previousAlternativeFormatImportPart
.
在您的代码中,您对所有 AltChunks
使用相同的 Id.这就是为什么您会多次看到相同的文本.
In your code, you are using the same Id for all the AltChunks
. It's why you see many time the same text.
我不确定 altchunkid 在您的代码中是否是唯一的:string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 2);
I am not sure the altchunkid will be unique with your code: string altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString().Substring(0, 2);
如果您不需要设置特定值,我建议您在添加 AlternativeFormatImportPart
时不要显式设置 AltChunkId
.相反,您会像这样得到一个由 SDK 生成的:
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文档合并为一个Open Xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!