合并多个Word文档到一个开放的XML [英] Merge multiple word documents into one Open 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,真正的);
使用(WordprocessingDocument文件= WordprocessingDocument.Open(destinationFile,真))
{
//更改文档类型文档
document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
//获取文件的主要部分
MainDocumentPart mainPart = document.MainDocumentPart;
mainPart.Document.Save();
}
}
抓住
{
}
}
更新(使用AltChunks):
使用(WordprocessingDocument myDoc = WordprocessingDocument.Open(D:\\\\ Test.docx,真))
{
。串altChunkId =AltChunkId+ DateTime.Now.Ticks.ToString()子串(0,2);
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart块= mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML,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:
使用(WordprocessingDocument myDoc = WordprocessingDocument.Open(D:\\\\ Test.docx,真))
{ MainDocumentPart mainPart = myDoc.MainDocumentPart;
。串altChunkId =AltChunkId+ DateTime.Now.Ticks.ToString()子串(0,3);
AlternativeFormatImportPart块= mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML,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,可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.altchunk.aspx\"><$c$c>AltChunk$c$c>元件的多个文件合并成一个。
此链接<一个href=\"http://blogs.msdn.com/b/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx\">the-easy-way-to-assemble-multiple-word-documents而这其中的How使用altChunk为大会文件提供一些样品。
修改1
基于使用 altchunk
在更新的问题,您的code 的(更新#1)的,这里是VB.Net $ C $三我已经测试和工程就像一个魅力对我来说:
使用myDoc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(D:\\\\ Test.docx,真)
暗淡altChunkId =AltChunkId+ DateTime.Now.Ticks.ToString()。子串(0,2)
昏暗mainPart = myDoc.MainDocumentPart
点心块= mainPart.AddAlternativeFormatImportPart(
DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML,altChunkId)
使用FILESTREAM作为IO.FileStream = IO.File.Open(D:\\\\ Test1.docx,IO.FileMode.Open)
chunk.FeedData(FILESTREAM)
使用完
昏暗altChunk =新DocumentFormat.OpenXml.Wordprocessing.AltChunk()
altChunk.Id = altChunkId
mainPart.Document.Body.InsertAfter(altChunk,mainPart.Document.Body.Elements(中DocumentFormat.OpenXml.Wordprocessing.Paragraph)。去年())
mainPart.Document.Save()
使用完
编辑2
第二个问题的(更新#2)的
这code两次追加Test2的数据,在地方Test1的数据作为
好。
块引用>是与
altchunkid
。对于要在主文档中合并的每个文件,您需要:
- 添加
AlternativeFormatImportPart
在mainDocumentPart
与编号
它必须是唯一的。此元素包含插入的数据- 在正文中添加在其中设置了
ID的
引用previousAltchunk
元素AlternativeFormatImportPart
。在您的code,您使用的是相同的ID为所有的
AltChunks
。这就是为什么你看到大量的时间相同的文本。我不知道该altchunkid将与您code独特的:
字符串altChunkId =AltChunkId+ DateTime.Now.Ticks.ToString()子串(0,2);
如果您不需要设置一个特定的值,我建议你不设置明确的
AltChunkId
当您添加AlternativeFormatImportPart
。相反,你被这样的SDK生成的一个:VB.Net
昏暗块作为AlternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML)
昏暗altchunkid作为字符串= mainPart.GetIdOfPart(块)C#
AlternativeFormatImportPart块= mainPart.AddAlternativeFormatImportPart(DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPartType.WordprocessingML);
字符串altchunkid = mainPart.GetIdOfPart(块);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 { } }
Update( using 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(); }
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()); } }
This code is appending the Test2 data twice, in place of Test1 data as well. Means I get:
Test Test2 Test2
instead of :
Test Test1 Test2
解决方案Using openXML SDK only, you can use
AltChunk
element to merge the multiple document into one.This link the-easy-way-to-assemble-multiple-word-documents and this one How to Use altChunk for Document Assembly provide some samples.
EDIT 1
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
EDIT 2
The second issue (update#2)
This code is appending the Test2 data twice, in place of Test1 data as well.
is related to
altchunkid
.For each document you want to merge in the main document, you need to:
- 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
.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 theAlternativeFormatImportPart
. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!