最近在做一個程序,需要在程序中對Word內容做些處理。從網上查了很多資料,發現,許多都是重復的。更有許多知識,根本沒有講到。為了以后使用方便。將所有的這些知識,加以總結,以備后來人使用。
1、引用
需要引用 COM庫:Microsoft word 11.0 Object Library. 不同的版本,會有不同的版本號。
如 2010版Office 就是 Microsoft word 14.0 Object Library.
2、引用相應的名字空間:
using Microsoft.Office.Core;
using word = Microsoft.Office.Interop.Word;
3、打開一個已存在的word文檔
public void OpenDocFile(string docName) { object oMissing = System.Reflection.Missing.Value; //一個編程時需要經常使用的一個參數 word.ApplicationClass wordapp = null; //這是WORD程序,在這個程序下,可以同時打開多個文檔,盡量不要同時打開多個Word程序,否則會出錯的。 word.Document doc = null; //第一個需要打開的WORD文檔 word.Document doc2 = null; //另一個需要打開的WORD文檔 wordapp = new word.ApplicationClass(); wordapp.Visible = true; //所打開的WORD程序,是否是可見的。 object docObject = docName; //由於COM操作中,都是使用的 object ,所以,需要做一些轉變 if (File.Exists(docName)) // 如果要打開的文件名存在,那就使用doc來打開 { doc = wordapp.Documents.Add(ref docObject, ref oMissing, ref oMissing, ref oMissing); doc.Activate(); //將當前文件設定為活動文檔 ParagraphsCount = doc.Content.Paragraphs.Count; //此文檔中,段落的數量,也就是這個文檔中,有幾個段落。 } else //如果文件名不存在,那就使用doc2來打開 { doc2 = wordapp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); DocFileName = docName; doc2.Activate(); //將當前文件設定為活動文檔 } }
4、獲取某一段的具體文本內容
public string Paragraph(int index) { word.Paragraph para; para = doc.Content.Paragraphs[index]; ///這是一個設定對應的某一段 return para.Range.Text; }
5、DOC內容借助於剪貼板的復制與粘貼
/// <summary> /// 將doc某一段的內容復制到剪貼板上 /// </summary> /// <param name="index">段的序號</param> public void CopyParagraph( int index) { word.Paragraph para; para = doc.Content.Paragraphs[index]; para.Range.Copy(); } /// <summary> /// 將剪貼板的內容粘貼到doc2文檔中 /// </summary> public void PasteParagraph() { if (doc2 != null) { word.Paragraph para = doc2.Content.Paragraphs.Add(ref oMissing); try { para.Range.Paste(); para.Range.InsertParagraphBefore();//添加一次回車 } catch (Exception e) { throw e; } } }
6、關閉WORD程序和文檔
/// <summary> /// 關閉 word程序 /// </summary> public void CloseWord() { if (wordapp != null) { if (doc != null) { word._Document docc = doc as word._Document; docc.Close(ref oMissing, ref oMissing, ref oMissing); } if (doc2 != null) { word._Document docc = doc2 as word._Document; docc.Close(ref oMissing, ref oMissing, ref oMissing); } wordapp.Quit(ref oMissing, ref oMissing, ref oMissing); } }
7、替換文檔中的內容
/// <summary> /// 替換文檔中的內容 /// </summary> /// <param name="oldString">原有內容</param> /// <param name="newString">替換后的內容</param> public void Replace(string oldString, string newString) { doc2.Content.Find.Text = oldString; object FindText, ReplaceWith, ReplaceAll; FindText = oldString; ReplaceWith = newString; ReplaceAll = word.WdReplace.wdReplaceAll; doc2.Content.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref ReplaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing); }
以上,如果要替換其中的回車符為空格,可以這樣使用
Replace(“^p”,"");
就可以了,這一點,與在WORD中使用的替換功能是一樣的。
8、保存文檔內容
/// <summary> /// 保存word文檔(只保存doc2) /// </summary> public void SaveDocFile() { if (doc2 != null) { if (!string.IsNullOrEmpty(DocFileName)) { object docname = DocFileName;//要保存的文件名稱,包括路徑 Replace("^p^p", "^p"); doc2.SaveAs2(ref docname, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); } } }
9、在復制過程中,如果文檔中有表格,那么,上面所使用的復制方法,一般會報錯。“"此方法或屬性無效,因為 對象涉及表格行尾"。出現的原因就是因為 Paragraphs 只能是文字內容不能包括表格,在WORD中,表格是另一類重要的文檔類型。其實在表格中,每一個單元格都是一個獨立的段落。所以,如果文檔中有表格,那么文檔的 doc.Content.Paragraphs.Count 數量會增加很多。在復制過程中,可以采用下面的方法來復制。
/// <summary> /// 復制文檔的內容,從開始到結束(包括結束與開始的段落)的段落內容。 /// </summary> /// <param name="first">開始的段落號</param> /// <param name="next">結束的段落號</param> public void CopyParagraph2(int first,int next) { word.Range range = doc.Range(); word.Paragraph para1; para1 = doc.Content.Paragraphs[first]; range.Start = para1.Range.Start; word.Paragraph para2; para2 = doc.Content.Paragraphs[next]; range.End = para2.Range.End; range.Copy(); }
在這個復制過程中,重新建立了一個區域 word.Range range = doc.Range(); 這個區域包括文檔的所有內容。
然后根據段落的要求分別設定區域的Start 和 End ,如果在 Start 和 End 之間有一個表格的話,那么表格也會被正確復制過去.
10、創建一個新的word文檔
void CreateWord() { object path;//文件路徑 string strContent;//文件內容 MSWord.Application wordApp;//Word應用程序變量 MSWord.Document wordDoc;//Word文檔變量 path = "d:\\myWord.doc";//保存為Word2003文檔 // path = "d:\\myWord.doc";//保存為Word2007文檔 wordApp = new MSWord.ApplicationClass();//初始化 if (File.Exists((string)path)) { File.Delete((string)path); } //由於使用的是COM 庫,因此有許多變量需要用Missing.Value 代替 Object Nothing = System.Reflection.Missing.Value; //新建一個word對象 wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //WdSaveDocument為Word2003文檔的保存格式(文檔后綴.doc)\wdFormatDocumentDefault為Word2007的保存格式(文檔后綴.docx) object format = MSWord.WdSaveFormat.wdFormatDocument; //將wordDoc 文檔對象的內容保存為DOC 文檔,並保存到path指定的路徑 wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //關閉wordDoc文檔 wordDoc.Close(ref Nothing, ref Nothing, ref Nothing); //關閉wordApp組件對象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); }
