微软的Open XML SDL 2.0附加文档模板文档asp.net C# [英] Microsoft Open XML SDL 2.0 append document to template document asp.net c#
问题描述
我的asp.net C#Web的应用程序是通过用数据填充现有模板的Word文档创建Word文档。现在我需要进一步现有文档添加到该文件作为下一个页面。
My asp.net c# web-application is creating word documents by filling an existing template word document with data. Now I need to add a further existing documents to that document as next page.
例如:我的模板有两页。我需要添加文件有一个页面。作为结果,我希望得到一个word文档有3页。
For example: My template has two pages. The document I need to append has one page. As result I want to get one word document with 3 pages.
我如何添加文件到现有的Word文档中asp.net/c#与微软的Open XML SDK 2.0?
How do I append documents to an existing word document in asp.net/c# with the Microsoft Open XML SDK 2.0?
推荐答案
使用此code合并两个文件
Use this code to merge two documents
using System.Linq;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace altChunk
{
class Program
{
static void Main(string[] args)
{
string fileName1 = @"c:\Users\Public\Documents\Destination.docx";
string fileName2 = @"c:\Users\Public\Documents\Source.docx";
string testFile = @"c:\Users\Public\Documents\Test.docx";
File.Delete(fileName1);
File.Copy(testFile, fileName1);
using (WordprocessingDocument myDoc =
WordprocessingDocument.Open(fileName1, true))
{
string altChunkId = "AltChunkId1";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk =
mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkId);
using (FileStream fileStream = File.Open(fileName2, 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();
}
}
}
}
这完美的作品,并在同一code也可<一个href=\"http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.altchunk.aspx\">here.
This works flawlessly and the same code is also available here.
有是使用<一个另一种方法href=\"http://blogs.technet.com/b/ptsblog/archive/2011/01/24/open-xml-sdk-merging-documents.aspx\">Open XML PowerTools的
这篇关于微软的Open XML SDL 2.0附加文档模板文档asp.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!