您的位置:首页 > 其它

word 文档合并

2012-08-24 10:05 495 查看
using VIVE.CNUOutlay.Model;
using VIVE.CNUOutlay.BLL;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.Diagnostics;

public partial class Admin_WordPrint_Print_Attachment : System.Web.UI.Page
{
private DateTime beforeTime = DateTime.Now;
private ApplicationClass objApp = new ApplicationClass();
private DateTime afterTime = DateTime.Now;
private Document objDocLast = null;
private string path2 = UIHelper.GetReportFileName(Convert.ToInt32(HttpContext.Current.Request["ReportID"]), "附件");
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request["ReportID"]))
{

string NewfileName = Server.MapPath("/Admin/WordPrint/Templet/合并附件.doc");

VIVE.CNUOutlay.BLL.FileInfo file = new VIVE.CNUOutlay.BLL.FileInfo();
string fileName = string.Empty;
DataSet ds = file.GetAllDynamic("ReportID='" + Request["ReportID"] + "'", "");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
fileName += ds.Tables[0].Rows[i]["FilePath"].ToString() + ',';

}
string[] getFileNames = fileName.TrimEnd(',').Split(',');
InsertMerge(NewfileName, getFileNames, path2);
}
}

private void Open(string tempDoc)
{
object objTempDoc = tempDoc;
object objMissing = System.Reflection.Missing.Value;
objDocLast = objApp.Documents.Open(
ref objTempDoc,    //FileName
ref objMissing,   //ConfirmVersions
ref objMissing,   //ReadOnly
ref objMissing,   //AddToRecentFiles
ref objMissing,   //PasswordDocument
ref objMissing,   //PasswordTemplate
ref objMissing,   //Revert
ref objMissing,   //WritePasswordDocument
ref objMissing,   //WritePasswordTemplate
ref objMissing,   //Format
ref objMissing,   //Enconding
ref objMissing,   //Visible
ref objMissing,   //OpenAndRepair
ref objMissing,   //DocumentDirection
ref objMissing,   //NoEncodingDialog
ref objMissing    //XMLTransform
);
objDocLast.Activate();
}

//合并
public void InsertMerge(string tempDoc, string[] arrCopies, string outDoc)
{
object objMissing = Missing.Value;
object objFalse = false;
object confirmConversion = false;
object link = false;
object attachment = false;
try
{
//打开模板文件
Open(tempDoc);
foreach (string strCopy in arrCopies)
{
if (!string.IsNullOrEmpty(strCopy))
{
if (File.Exists(strCopy))
{
objApp.Selection.InsertFile(
strCopy,

ref objMissing,
ref confirmConversion,
ref link,
ref attachment
);
}
}

}
if (System.IO.File.Exists(outDoc))
{
System.IO.File.Delete(outDoc);
}
//保存到输出文件
SaveAs(outDoc);
foreach (Document objDocument in objApp.Documents)
{
objDocument.Close(
ref objFalse,
//SaveChanges
ref objMissing,
//OriginalFormat
ref objMissing    //RouteDocument
);
}
objApp.Quit(
ref objMissing,     //SaveChanges
ref objMissing,     //OriginalFormat
ref objMissing      //RoutDocument
);
objApp = null;

UIHelper.DownLoadFile(path2);
}
catch (Exception ex)
{
KillProcess();
oJavaScript.AlertAndRedirect("下载失败,原因:" + ex.Message, "/Admin/Print/Print_ProjectList.aspx");
return;
}
}

private void SaveAs(string outDoc)
{
object objMissing = System.Reflection.Missing.Value;
object objOutDoc = outDoc;
Document objDocLast = objApp.ActiveDocument;
objDocLast.SaveAs(
ref objOutDoc,      //FileName
ref objMissing,     //FileFormat
ref objMissing,     //LockComments
ref objMissing,     //PassWord
ref objMissing,     //AddToRecentFiles
ref objMissing,     //WritePassword
ref objMissing,     //ReadOnlyRecommended
ref objMissing,     //EmbedTrueTypeFonts
ref objMissing,     //SaveNativePictureFormat
ref objMissing,     //SaveFormsData
ref objMissing,     //SaveAsAOCELetter,
ref objMissing,     //Encoding
ref objMissing,     //InsertLineBreaks
ref objMissing,     //AllowSubstitutions
ref objMissing,     //LineEnding
ref objMissing      //AddBiDiMarks
);
}
public void KillProcess()
{
Process[] myProcesses;
DateTime startTime;
myProcesses = Process.GetProcessesByName("WINWORD");

//得不到进程ID,暂时只能判断进程启动时间
foreach (Process myProcess in myProcesses)
{
startTime = myProcess.StartTime;

if (startTime > beforeTime && startTime < afterTime)
{
myProcess.Kill();
}
}
}
}


以前写的,有点乱,不过功能实现了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: