Word 操作总汇
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Word = Microsoft.Office.Interop.Word; namespace TEST { class MyWord { /// <summary> /// Creat a word document /// </summary> public void CreatWord() { 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); } /// <summary> /// Opan a word document /// </summary> public void OpenWord() { object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); // Open the word silently oWord.Visible = false; object fileName = @"C:\TestDoc.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); //Such writing can also, but generally it is not recommended //oDoc = oWord.Documents.Open(ref fileName); } /// <summary> /// Import word template /// </summary> public void ImportTemplate() { object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = @"C:\TestDoc.doc"; //Useing this method to open a word ,can not use the oDoc.Save() to save this word,can only use oDoc.SaveAs() oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing); } /// <summary> /// Insert the table into a word document /// </summary> public void InsertTabel() { object oMissing = System.Reflection.Missing.Value; Word.Application oWord; Word.Document oDoc; oWord = new Word.Application(); oWord.Visible = false; object fileName = @"c:\TestDoc.docx"; 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); Word.Table table = oDoc.Tables.Add(oWord.Selection.Range, 2, 2, oMissing, oMissing); //In the Word count of rows and columns are started with number one table.Cell(1, 1).Range.Text = "AAAAAA"; table.Cell(1, 2).Range.Text = "BBBBBB"; table.Cell(2, 1).Range.Text = "CCCCCC"; table.Cell(2, 2).Range.Text = "DDDDDD"; } /// <summary> /// To Insert a row into the table /// </summary> public void InsertTableRow() { object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = false; object fileName = @"c:\TestDoc.docx"; 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); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); Word.Table table = oDoc.Tables.Add(tableLocation, 2, 2, ref oMissing, ref oMissing); table.Cell(1, 1).Range.Text = "AAAAAA"; table.Cell(1, 2).Range.Text = "BBBBBB"; table.Cell(2, 1).Range.Text = "CCCCCC"; table.Cell(2, 2).Range.Text = "DDDDDD"; Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[2]; //If you want to insert to the last line ,Incoming the oMissing as parameter is ok Word.Row row = newTable.Rows.Add(ref beforeRow); row.Cells[1].Range.Text = "EEEEEE"; row.Cells[2].Range.Text = "FFFFFF"; } /// <summary> /// Merge Cells /// </summary> public void CellMerge() { 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)); } /// <summary> /// To separate cell /// </summary> public void SeparateCell() { 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); } /// <summary> /// Controlled by paragraph inserted /// </summary> public void Insert() { 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); Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "Heading 1"; oPara1.Range.Font.Bold = 1; oPara1.Format.SpaceAfter = 24; oPara1.Range.InsertParagraphAfter(); } } }
智者乐山山如画,
仁者乐水水无涯。
从从容容一杯酒,
平平淡淡一杯茶。