- /// <summary>
- /// </summary>
- /// <param name="p_TemplatePath">末班文件的路径</param>
- /// <param name="savePath">生成末班文件的路径</param>
- /// <param name="file">生成文件的名称</param>
- /// <param name="bookQ">书签是一个数组</param>
- /// <param name="bookValue">书签对应的值</param>
- /// <returns></returns>
- public object Save(string p_TemplatePath,string savePath,string file,Hashtable table)
- {
- //生成WORD程序对象和WORD文档对象
- Microsoft.Office.Interop.Word.Application appWord;
- Microsoft.Office.Interop.Word.Document doc;
- object miss;
- getDoc(p_TemplatePath, out appWord, out doc, out miss);
- //doc = (Document)appWord.Documents.Add(ref objTemplate, oMissing, ref objDocType, ref oMissing);
- //获取模板中所有的书签
- Bookmarks odf = doc.Bookmarks;
- // string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker" };
- //string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker", };
- //循环所有的书签,并给书签赋值
- foreach (DictionaryEntry item in table)
- {
- doc.Bookmarks.get_Item(item.Key).Range.Text = item.Value.ToString();
- }
- //for (int oIndex = 0; oIndex < bookQ.Length; oIndex++)
- //{
- // // obDD_Name = WD + testTableremarks[oIndex];
- // doc.Bookmarks.get_Item(bookQ[oIndex]).Range.Text = bookValue[oIndex];//此处Range也是WORD中很重要的一个对象,就是当前操作参数所在的区域
- //}
- //doc.Bookmarks.get_Item("房屋补偿金大写").Range.Text = "ss";
- //第四步 生成word,将当前的文档对象另存为指定的路径,然后关闭doc对象。关闭应用程序
- object filename = savePath + "\\\\" + file + ".doc";
- if (File.Exists(filename.ToString()))
- {
- File.Delete(filename.ToString());
- }
- try
- {
- //实现文档加密
- // doc.ReadOnlyRecommended=true;
- // doc.Password="";
- //doc.WritePassword = "ls20120305";
- // doc.RemoveDateAndTime=false;
- // doc.RemovePersonalInformation=false;
- //实现文档保护自读功能
- object noReset = false;
- object password = "ls20120305";
- object useIRM = false;
- object enforceStyleLock = false;
- doc.Protect(WdProtectionType.wdAllowOnlyReading, ref noReset, ref password, ref useIRM, ref enforceStyleLock);
- doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
- // doc.webPagePreview();
- //doc.PrintPreview();
- Type docType = doc.GetType();
- /*下面是Microsoft Word 9 Object Library的写法: */
- /*docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormathtml });*/
- /*下面是Microsoft Word 10 Object Library的写法: */
- string saveFilePath = HttpContext.Current.Server.MapPath("~/AttmentHtml/" + file + ".html");
- if (File.Exists(saveFilePath))
- {
- File.Delete(saveFilePath);
- }
- docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
- null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatFilteredHTML });
- object missingValue = Type.Missing;
- object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
- doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
- appWord.Application.Quit(ref miss, ref miss, ref miss);
- doc = null;
- appWord = null;
- DownLoadDoc("/AttmentHtml/" + file + ".html");
- killAllProcess();
- }
- catch (Exception e)
- {
- killAllProcess();
- throw;
- }
- filePath = filename.ToString();
- return filename;
- }
- private static void getDoc(string p_TemplatePath, out Microsoft.Office.Interop.Word.Application appWord, out Microsoft.Office.Interop.Word.Document doc, out object miss)
- {
- appWord = new Application();
- doc = new Document();
- object oMissing = System.Reflection.Missing.Value;//这个是什么东西,我始终没搞明白-_-
- //打开模板文档,并指定doc的文档类型
- object objTemplate = p_TemplatePath;
- object objDocType = WdDocumentType.wdTypeDocument;
- miss = System.Reflection.Missing.Value;
- doc = appWord.Documents.Open(ref objTemplate, ref miss, ref miss, ref miss, //使用这种方式打开word文档就没有问题了。
- ref miss, ref miss, ref miss, ref miss,
- ref miss, ref miss, ref miss, ref miss,
- ref miss, ref miss, ref miss, ref miss);
- doc.Activate();
- }
- protected void killAllProcess() // 杀掉所有winword.exe进程
- {
- System.Diagnostics.Process[] myPs;
- myPs = System.Diagnostics.Process.GetProcesses();
- foreach (System.Diagnostics.Process p in myPs)
- {
- if (p.Id != 0)
- {
- string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString();
- try
- {
- if (p.Modules != null)
- if (p.Modules.Count > 0)
- {
- System.Diagnostics.ProcessModule pm = p.Modules[0];
- myS += "\\n Modules[0].FileName:" + pm.FileName;
- myS += "\\n Modules[0].ModuleName:" + pm.ModuleName;
- myS += "\\n Modules[0].FileVersionInfo:\\n" + pm.FileVersionInfo.ToString();
- if (pm.ModuleName.ToLower() == "winword.exe")
- p.Kill();
- }
- }
- catch
- { }
- finally
- {
- }
- }
- public void DownLoad(string path)
- {
- HttpContext.Current.Response.ClearHeaders();
- HttpContext.Current.Response.Clear();
- HttpContext.Current.Response.Expires = 0;
- HttpContext.Current.Response.Buffer = true;
- HttpContext.Current.Response.AddHeader("Accept-Language", "zh-tw");
- string name = System.IO.Path.GetFileName(path);
- System.IO.FileStream files = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
- byte[] byteFile = null;
- if (files.Length == 0)
- {
- byteFile = new byte[1];
- }
- else
- {
- byteFile = new byte[files.Length];
- }
- files.Read(byteFile, 0, (int)byteFile.Length);
- files.Close();
- HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
- HttpContext.Current.Response.ContentType = "application/octet-stream;charset=gbk";
- HttpContext.Current.Response.BinaryWrite(byteFile);
- HttpContext.Current.Response.End();
- }
- //该片段来自于http://www.codesnippet.cn/detail/0909201513660.html
来源: http://www.codesnippet.cn/detail/0909201513660.html