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(); } } } }
以前写的,有点乱,不过功能实现了。
相关文章推荐
- 如何将两个word文档合并?
- c#如何合并多个word文档中的内容到一个word中
- 批量制作word文档-邮件合并
- 黄聪:C#操作合并多个Word文档
- 多个 Word 文档合并为一个
- 实现word文档合并
- 让Team Foundation Server Client(TFS)能比较和合并Word文档
- 合并两个WORD文档,并且修改Word中标签的内容
- 用C#编程合并多个WORD文档
- 如何把服务器上的多个Word文档合并到一个Word文档中并且动态填充指定的内容
- 用C#编程合并多个WORD文档
- Aspose.Words在word文档合并时如何控制列表编号
- 用C#编程合并多个WORD文档
- C#生成word文档 、word转pdf、合并pdf文件等
- 使用Jacob合并Word文档并填充文档中的书签
- 批量合并word文档
- Word文档中--邮件合并
- 获取Word目录结构、合并Word文档
- C#实现简单合并word文档的方法
- 用C#实现word文档合并