//產生WORD程式對象和WORD文檔對象 Microsoft.Office.Interop.Word.Application appWord = new Application(); Microsoft.Office.Interop.Word.Document doc = new Document(); object oMissing = System.Reflection.Missing.Value;//這個是什麼東西,我始終沒搞明白-_- //開啟模板文檔,並指定doc的文件類型 object objTemplate = Server.MapPath(p_TemplatePath); object objDocType = WdDocumentType.wdTypeDocument; doc = (Document)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); //擷取模板中所有的書籤 Bookmarks odf = doc.Bookmarks; string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"}; string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",}; //迴圈所有的書籤,並給書籤賦值 for (int oIndex = 0; oIndex < testTableremarks.Length; oIndex++) { obDD_Name = WD + testTableremarks[oIndex]; doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = p_TestReportTable.Rows[0][testTablevalues [oIndex]].ToString();//此處Range也是WORD中很重要的一個對象,就是當前巨集指令引數所在的地區 } //第四步 產生word,將當前的文檔對象另存新檔指定的路徑,然後關閉doc對象。關閉應用程式 object filename = Server.MapPath(p_SavePath) + "\\Testing_" + DateTime.Now.ToShortDateString() + ".doc"; object miss = System.Reflection.Missing.Value; 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); 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; this.Hid_ShowMessage.Value = "產生成功!";
上述代碼就是一個通過模板檔案產生WORD的過程。其實也就是一個替換書籤內容的過程。 在開發的過程中,有些資料是動態增加的,假如我要向一個表格中動態添加幾行資料,就無法用替換書籤的方式來進行操作,需要通過程式在文檔頁面的表格中添加行。 向表格中添加行,有兩種操作形式:一種是在WORD模板中已經存在了一個表格。一種是我們在程式中直接添加一個表格對象。 第一種情況下,需要注意:在WORD模板中要操作的表格中,不能有縱向合并的儲存格,不然程式無法擷取到當前要操作對象導致程式報錯.儲存格的合并,我們可以在程式中控制。 第二種情況下就需要我們通過程式去直接添加表格了。 產生表格的代碼具體如下: 1.擷取文檔中已存在的表格: Microsoft.Office.Interop.Word.Table characterTable = doc.Tables[2];//在document對象的集合操作中,起始點是從1開始,並不是從0開始的,此處需要注意。 2.在文檔中直接產生表格,首先要擷取插入表格的位置,然後添加表格對象: object oEndOfDoc = "\\endofdoc";//WORD中預定義的書籤,還有很多,此處就不一一列舉。 object oMissing = System.Reflection.Missing.Value; Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//擷取當前文檔的末尾位置。 wrdRng.InsertAfter(" ");//插入一行,此處不能用 wrdRng.InsertAfter(""),如果用這個,就不能換行,我也不知道為什麼。
[複製此代碼]CODE:object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd; object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//分頁符 wrdRng.Collapse(ref oCollapseEnd); wrdRng.InsertBreak(ref oPageBreak);//插入了一頁 wrdRng.Collapse(ref oCollapseEnd); wrdRng.InsertAfter("圖片資訊"); wrdRng.Font.Size = 20;//指定操作對象的文字大小 wrdRng.Font.Bold = 1;//指定操作對象的粗體:1為粗體,0為正常 wrdRng.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//指定操作地區的文字布局:置中對齊 //上述代碼的意思是:找到當前的末尾位置,然後插入一個分頁符,相當於跳到了一個新頁,在這個新頁的頂端寫入文字“圖片資訊”,並指定文字大小為20,粗體置中顯示。 wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range; wrdRng.InsertAfter(" "); wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range; wrdRng.InsertParagraphAfter();//插入一個段落,在此段落上插入一個2行一列的表格。 Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(wrdRng, 2, 1, ref oMissing, ref oMissing);
我們還可以對錶格進行格式設定,此處我們就不在一一列舉。 3.下面我們分析一下對錶格的儲存格的操作:合并,拆分。這個就需要我們根據實際表格來進行操作: //擷取具體的某個儲存格(1,1),擷取第一行第一列的儲存格 Cell cell = doc.Tables[1].Cell(1,1); cell.Range.Text="Text";//指定目前的儲存格的內容為Text 在Table的操作中,添加新行: object beforeRow = doc.Tables[1].Rows[2];//此行是先擷取到第二行 doc.Tables[1].Rows.Add(ref beforeRow);//效果類似於在WORD中此表格的第二行上進行【插入行】操作,插入的新行將會插入到當前行的上一行中,格式也是和此行一致的。 //合併儲存格:感覺在此處合併儲存格挺傻瓜的,你只需要指定你要合并的起始儲存格和結束儲存格,然後通過Merge操作就行了 Cell cell = doc.Tables[1].Cell(iRow, 2);//列合并 cell.Merge(doc.Tables[1].Cell(iRow, 6)); Cell cell1 = doc.Tables[1].Cell(iRow - 1, 1);//行合并 cell1.Merge(doc.Tables[1].Cell(iRow + 1, 1)); 上述操作就是在此程式中用到的一些知識點,還有好多的東西需要去熟悉、理解。 另外,在程式的測試過程中發現,當執行一次文檔產生後,在資源管理員中始終有winword.exe進程殺不掉,目前的解決辦法是:直接殺進程,代碼如下:
[複製此代碼]CODE: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 { } } } }
目前為止,一個WORD文檔就產生了。上述為我在這個程式開發中遇到的問題和解決方案,可能有好多地方都是考慮不全的,如果在程式開發中對WORD的操作有新的認識的話,歡迎和我溝通交流,彼此提高! 下邊是在網上一些比較好的摘抄: 建立新Word
[複製此代碼]CODE:object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
開啟文檔:
[複製此代碼]CODE:object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = @"E:CCCXCXXTestDoc.doc"; oDoc = oWord.Documents.Open(ref fileName, 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);
匯入模板
[複製此代碼]CODE:object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = @"E:XXXCCXTest.doc"; oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
.添加新表
[複製此代碼]CODE:object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
.表插入行
[複製此代碼]CODE:object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow);
.儲存格合并
[複製此代碼]CODE:object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2));
.儲存格分離
[複製此代碼]CODE:object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add( oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2)); object Rownum = 2; object Columnnum = 2; cell.Split(ref Rownum, ref Columnnum);
通過段落控制插入
[複製此代碼]CODE:
object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */ //Start Word and create a new document. Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "Heading 1"; oPara1.Range.Font.Bold = 1; oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter();