【问题标题】:c# Interop.Word Join two WordDocument without saving to diskc# Interop.Word 加入两个WordDocument而不保存到磁盘
【发布时间】:2014-06-29 03:00:47
【问题描述】:

我已经可以从它们的文件路径中加入两个或多个 word 文档。

如何在不将它们保存到磁盘的情况下加入它们?

string TemporaryPath = "";
string TemplatePath = "";
Object MissingValue;
object OFalse = false;
object OTrue = true;

public WordWorker()
{
    TemporaryPath = AppDomain.CurrentDomain.BaseDirectory + "Temporary";
    MissingValue = Missing.Value;
    TemplatePath = @"C:\TemplateTest.dotx";
}
public void Merge()
{
    object wdPageBreak = 7;
    object wdStory = 6;


    Application WordApplication = new Application();
    Document WordDocument = WordApplication.Documents.Add(ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue);

    for (int i = 0; i < 100; i++)
    {
        string Path = CreateWordFromTemplate(WordApplication, TemplatePath, new Dictionary<string, string>() { { "Name", "MyNewValue" + i } });
        WordDocument.Application.Selection.Range.InsertFile(Path, ref MissingValue, ref MissingValue, ref MissingValue, ref OFalse);
    }

    object WordSaveFormat = WdSaveFormat.wdFormatDocument;
    object MergedDocumentPath = TemporaryPath + "Merged.dox";
    WordDocument.SaveAs(ref MergedDocumentPath, ref WordSaveFormat, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue);
    WordApplication.Application.Quit();
}
public string CreateWordFromTemplate(Application WordApplication, Object oTemplatePath, Dictionary<string, string> dictionary)
{
    Guid DocumentID = Guid.NewGuid();

    Document WordDocument = new Document();

    WordDocument = WordApplication.Documents.Add(ref oTemplatePath, ref MissingValue, ref MissingValue, ref MissingValue);

    foreach (Field FieldToMerge in WordDocument.Fields)
    {
        Range FieldRange = FieldToMerge.Code;
        String FieldText = FieldRange.Text;

        if (FieldText.StartsWith(" MERGEFIELD"))
        {
            Int32 EndMerge = FieldText.IndexOf("\\");
            Int32 FieldNameLength = FieldText.Length - EndMerge;
            String FieldName = FieldText.Substring(11, EndMerge - 11).Trim();

            if (dictionary.ContainsKey(FieldName))
            {
                FieldToMerge.Select();
                WordApplication.Selection.TypeText(dictionary[FieldName]);
            }
        }
    }
    string Path = @"{0}\{1}.doc".SFormat(TemporaryPath, DocumentID);

    WordDocument.SaveAs(Path);
    WordDocument.Close();
    return Path;
}

【问题讨论】:

标签: c# merge office-interop mailmerge


【解决方案1】:

使用其中一个不需要 Office COM 的库,结果直接与内存或其他相关联的二进制数据一起工作。

https://www.codeplex.com/site/search?tagname=office&projectsearchtext=%22office%22

然后假设,使用MemoryStream,您将能够以您想要的格式创建您想要的文档,然后用您想要的Stream 做任何事情。

v/

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多