问题描述
我有我使用生成的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,可以使用<$c$c>AltChunk$c$c>元件的多个文件合并成一个。
此链接the-easy-way-to-assemble-multiple-word-documents而这其中的一些样品。
修改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)的
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 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!