我需要以编程方式使用C#将几个预先存在的docx
文件附加到一个长docx
文件中 - 包括子弹和图像等特殊标记.页眉和页脚信息将被删除,因此这些信息不会导致任何问题.
我可以找到有关docx
使用.NET Framework 3 操作单个文件的大量信息,但对于如何合并文件没有任何简单或明显的信息.还有一个第三方程序(Acronis.Words)可以做到这一点,但它非常昂贵.
已经建议通过Word进行自动化,但是我的代码将在IIS Web服务器上的ASP.NET上运行,因此对我而言,不能选择使用Word.很抱歉没有提到这一点.
GRG*_*doi 24
尽管提交了所有好的建议和解决方案,我还是开发了另一种选择.在我看来,你应该完全避免在服务器应用程序中使用Word.所以我使用OpenXML,但它不适用于AltChunk.我将文本添加到原始主体,我收到了一个byte []列表而不是文件名列表,但您可以轻松地根据需要更改代码.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace OfficeMergeControl
{
public class CombineDocs
{
public byte[] OpenAndCombine( IList<byte[]> documents )
{
MemoryStream mainStream = new MemoryStream();
mainStream.Write(documents[0], 0, documents[0].Length);
mainStream.Position = 0;
int pointer = 1;
byte[] ret;
try
{
using (WordprocessingDocument mainDocument = WordprocessingDocument.Open(mainStream, true))
{
XElement newBody = XElement.Parse(mainDocument.MainDocumentPart.Document.Body.OuterXml);
for (pointer = 1; pointer < documents.Count; pointer++)
{
WordprocessingDocument tempDocument = WordprocessingDocument.Open(new MemoryStream(documents[pointer]), true);
XElement tempBody = XElement.Parse(tempDocument.MainDocumentPart.Document.Body.OuterXml);
newBody.Add(tempBody);
mainDocument.MainDocumentPart.Document.Body = new Body(newBody.ToString());
mainDocument.MainDocumentPart.Document.Save();
mainDocument.Package.Flush();
}
}
}
catch (OpenXmlPackageException oxmle)
{
throw new OfficeMergeControlException(string.Format(CultureInfo.CurrentCulture, "Error while merging files. Document index {0}", pointer), oxmle);
}
catch (Exception e)
{
throw new OfficeMergeControlException(string.Format(CultureInfo.CurrentCulture, "Error while merging files. Document index {0}", pointer), e);
}
finally
{
ret = mainStream.ToArray();
mainStream.Close();
mainStream.Dispose();
}
return (ret);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你.
您不需要使用自动化.DOCX文件基于OpenXML格式.它们只是zip文件,里面有一堆XML和二进制部分(思考文件).您可以使用Packaging API(WindowsBase.dll中的System.IO.Packaging)打开它们,并使用Framework中的任何XML类对它们进行操作.
查看OpenXMLDeveloper.org了解详细信息.
这是一个非常晚的原始问题,并且有很多变化,但我想我会分享我编写合并逻辑的方式.这使用了Open XML Power Tools
public byte[] CreateDocument(IList<byte[]> documentsToMerge)
{
List<Source> documentBuilderSources = new List<Source>();
foreach (byte[] documentByteArray in documentsToMerge)
{
documentBuilderSources.Add(new Source(new WmlDocument(string.Empty, documentByteArray), false));
}
WmlDocument mergedDocument = DocumentBuilder.BuildDocument(documentBuilderSources);
return mergedDocument.DocumentByteArray;
}
Run Code Online (Sandbox Code Playgroud)
目前,这在我们的应用程序中运行良好.我稍微更改了代码,因为我的要求是每个需要首先处理的文档.所以传入的是具有模板字节数组的DTO对象以及需要替换的各种值.这是我的代码目前的样子.这使得代码更进一步.
public byte[] CreateDocument(IList<DocumentSection> documentTemplates)
{
List<Source> documentBuilderSources = new List<Source>();
foreach (DocumentSection documentTemplate in documentTemplates.OrderBy(dt => dt.Rank))
{
// Take the template replace the items and then push it into the chunk
using (MemoryStream templateStream = new MemoryStream())
{
templateStream.Write(documentTemplate.Template, 0, documentTemplate.Template.Length);
this.ProcessOpenXMLDocument(templateStream, documentTemplate.Fields);
documentBuilderSources.Add(new Source(new WmlDocument(string.Empty, templateStream.ToArray()), false));
}
}
WmlDocument mergedDocument = DocumentBuilder.BuildDocument(documentBuilderSources);
return mergedDocument.DocumentByteArray;
}
Run Code Online (Sandbox Code Playgroud)
我不久前编写了一个小测试应用程序来执行此操作。我的测试应用程序使用的是 Word 2003 文档 (.doc),而不是 .docx,但我想过程是相同的 - 我认为您需要更改的就是使用主互操作程序集的较新版本。使用新的 C# 4.0 功能,这段代码看起来会整洁很多...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
using System.Runtime.InteropServices;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
new Program().Start();
}
private void Start()
{
object fileName = Path.Combine(Environment.CurrentDirectory, @"NewDocument.doc");
File.Delete(fileName.ToString());
try
{
WordApplication = new ApplicationClass();
var doc = WordApplication.Documents.Add(ref missing, ref missing, ref missing, ref missing);
try
{
doc.Activate();
AddDocument(@"D:\Projects\WordTests\ConsoleApplication1\Documents\Doc1.doc", doc, false);
AddDocument(@"D:\Projects\WordTests\ConsoleApplication1\Documents\Doc2.doc", doc, true);
doc.SaveAs(ref fileName,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
}
finally
{
doc.Close(ref missing, ref missing, ref missing);
}
}
finally
{
WordApplication.Quit(ref missing, ref missing, ref missing);
}
}
private void AddDocument(string path, Document doc, bool lastDocument)
{
object subDocPath = path;
var subDoc = WordApplication.Documents.Open(ref subDocPath, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing);
try
{
object docStart = doc.Content.End - 1;
object docEnd = doc.Content.End;
object start = subDoc.Content.Start;
object end = subDoc.Content.End;
Range rng = doc.Range(ref docStart, ref docEnd);
rng.FormattedText = subDoc.Range(ref start, ref end);
if (!lastDocument)
{
InsertPageBreak(doc);
}
}
finally
{
subDoc.Close(ref missing, ref missing, ref missing);
}
}
private static void InsertPageBreak(Document doc)
{
object docStart = doc.Content.End - 1;
object docEnd = doc.Content.End;
Range rng = doc.Range(ref docStart, ref docEnd);
object pageBreak = WdBreakType.wdPageBreak;
rng.InsertBreak(ref pageBreak);
}
private ApplicationClass WordApplication { get; set; }
private object missing = Type.Missing;
}
}
Run Code Online (Sandbox Code Playgroud)