合并word文档C#

时间:2011-08-23 15:33:21

标签: c# ms-word sharepoint-2007

我正在尝试合并c#文档。我的想法是拥有一个主文档,并将其他文档附加到此主文档。环境在SharePoint中,所以基本上文档存在于文档库中,我的代码能够找到文档没有问题。一旦我以编程方式找到它们,我就不知道如何打开和合并它们。我一直在寻找,似乎就像OpenXML SDK 2.0是一种方法,但我根本不熟悉该工具,并试图看看他们是否可以用我在Visual中的当前工具来做到这一点Studio 3.5。使用 Microsoft.Office.Interop.Word 或其他内容。据我所知,实际文件以二进制形式存储在内容数据库中,因此物理文件实际上并不存在,所以我不能轻易地用word打开它们。任何帮助。

2 个答案:

答案 0 :(得分:0)

我认为这个链接正是您所寻找的:

Merging Word Documents on the Server Side with SharePoint 2010

有一点Open Xml,但没什么不好。如果您认为这不能为您提供所需的控制权,您可以查看以下文章:

Creating Documents by Using the Open XML Format SDK 2.0 (Part 3 of 3)

答案 1 :(得分:0)

以下脚本取自Tortoise SVN,可以帮助您了解如何合并.doc文件。如果要分发许可证,请小心许可。

您需要使用以下命令从C#调用脚本:

wscript.exe "C:\<path_to_the_script>\merge-doc.js" merged.doc theirs.doc mine.doc base.doc //E:javascript

这是脚本代码,仅用于doc,还有其他docx我没有包含在这里,但你可以看一下安装Tortoise SVN。

//
// TortoiseSVN Merge script for Word Doc files
//
// Copyright (C) 2004-2008 the TortoiseSVN team
// This file is distributed under the same license as TortoiseSVN
//
// Last commit by:
// $Author: tortoisesvn $
// $Date: 2008-12-05 17:38:43 +0100 (Fr, 05 Dez 2008) $
// $Rev: 14781 $
//
// Authors:
// Dan Sheridan, 2008
// Davide Orlandi and Hans-Emil Skogh, 2005
//

var objArgs,num,sTheirDoc,sMyDoc,sBaseDoc,sMergedDoc,objScript,word,baseDoc,WSHShell;

// Microsoft Office versions for Microsoft Windows OS
var vOffice2000 = 9;
var vOffice2002 = 10;
var vOffice2003 = 11;
var vOffice2007 = 12;
// WdCompareTarget
var wdCompareTargetSelected = 0;
var wdCompareTargetCurrent = 1;
var wdCompareTargetNew = 2;

objArgs = WScript.Arguments;
num = objArgs.length;
if (num < 4)
{
   WScript.Echo("Usage: [CScript | WScript] merge-doc.js merged.doc theirs.doc mine.doc base.doc");
   WScript.Quit(1);
}

sMergedDoc=objArgs(0);
sTheirDoc=objArgs(1);
sMyDoc=objArgs(2);
sBaseDoc=objArgs(3);

objScript = new ActiveXObject("Scripting.FileSystemObject")
if ( ! objScript.FileExists(sTheirDoc))
{
    WScript.Echo("File " + sTheirDoc +" does not exist.  Cannot compare the documents.", vbExclamation, "File not found");
    WScript.Quit(1);
}
if ( ! objScript.FileExists(sMergedDoc))
{
    WScript.Echo("File " + sMergedDoc +" does not exist.  Cannot compare the documents.", vbExclamation, "File not found");
    WScript.Quit(1);
}

objScript = null

try
{
    word = WScript.CreateObject("Word.Application");
}
catch(e)
{
   WScript.Echo("You must have Microsoft Word installed to perform this operation.");
   WScript.Quit(1);
}

word.visible = true

// Open the base document
baseDoc = word.Documents.Open(sTheirDoc);

// Merge into the "My" document
if (parseInt(word.Version) < vOffice2000)
{
        baseDoc.Compare(sMergedDoc);
}
else if (parseInt(word.Version) < vOffice2007)
{
        baseDoc.Compare(sMergedDoc, "Comparison", wdCompareTargetNew, true, true);
} else {
    baseDoc.Merge(sMergedDoc);
}

// Show the merge result
if (parseInt(word.Version) < 12)
{
    word.ActiveDocument.Windows(1).Visible = 1;
}

// Close the first document
if (parseInt(word.Version) >= 10)
{
   baseDoc.Close();
}

// Show usage hint message
WSHShell = WScript.CreateObject("WScript.Shell");
if(WSHShell.Popup("You have to accept or reject the changes before\nsaving the document to prevent future problems.\n\nWould you like to see a help page on how to do this?", 0, "TSVN Word Merge", 4 + 64) == 6)
{
    WSHShell.Run("http://office.microsoft.com/en-us/assistance/HP030823691033.aspx");
}