C# Word操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel; using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Core; using System.IO; using System.Threading; namespace OfficeOperator { public class WordOperator { #region 创建 public Word._Document Create() { 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); return oDoc; } #endregion #region 2、打开 public void OpenDoc(string filename) { object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = filename; 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); } #endregion #region 3、导入模板 public void ImportMaster(string filename) { object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = filename; oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing); } #endregion #region 4、添加新表 public void AddTable(string filename) { object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = filename; oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing); } #endregion #region 5、表插入行 public void InsertTableToLine(string filename) { 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); } #endregion #region 6、单元格合并 public void ComBinCell() { 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)); } #endregion #region 7、单元格分离 public void CellSplit() { 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); } #endregion #region 8、通过段落控制插入 public void InsertByParagrame(string filename) { object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = filename; 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(); } #endregion #region 打开Word文档,并且返回对象wDoc,wDoc /// <summary> /// 打开Word文档,并且返回对象wDoc,wDoc /// </summary> /// <param name="FileName">完整Word文件路径+名称</param> /// <param name="wDoc">返回的Word.Document wDoc对象</param> /// <param name="WApp">返回的Word.Application对象</param> public static void CreateWordDocument(string FileName, ref Word.Document wDoc, ref Word.Application WApp) { if (FileName == "") return; Word.Document thisDocument = null; Word.FormFields formFields = null; Word.Application thisApplication = new Word.Application(); thisApplication.Visible = true; thisApplication.Caption = ""; thisApplication.Options.CheckSpellingAsYouType = false; thisApplication.Options.CheckGrammarAsYouType = false; Object filename = FileName; Object ConfirmConversions = false; Object ReadOnly = true; Object AddToRecentFiles = false; Object PasswordDocument = System.Type.Missing; Object PasswordTemplate = System.Type.Missing; Object Revert = System.Type.Missing; Object WritePasswordDocument = System.Type.Missing; Object WritePasswordTemplate = System.Type.Missing; Object Format = System.Type.Missing; Object Encoding = System.Type.Missing; Object Visible = System.Type.Missing; Object OpenAndRepair = System.Type.Missing; Object DocumentDirection = System.Type.Missing; Object NoEncodingDialog = System.Type.Missing; Object XMLTransform = System.Type.Missing; try { Word.Document wordDoc = thisApplication.Documents.Open(ref filename, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform); thisDocument = wordDoc; wDoc = wordDoc; WApp = thisApplication; formFields = wordDoc.FormFields; } catch (Exception ex) { } } #endregion #region Word填充数据(For Example) /// <summary> /// Word填充数据 /// </summary> private void WordLoadData() { //Word.Document wDoc=null; //Word.Application wApp=null; //sysFun.CreateWordDocument("E:\\监测报告(new).dot",ref wDoc,ref wApp); ////对标签"C"进行填充 //object bkmC="C"; //if(wApp.ActiveDocument.Bookmarks.Exists("C") == true) //{ // wApp.ActiveDocument.Bookmarks.get_Item // (ref bkmC).Select(); //} //wApp.Selection.TypeText(this.txt1.Text); //object bkmG = "TWaterTable3"; //object unit; //object count; //移动数 //object extend; //extend = Word.WdMovementType.wdExtend; //unit = Word.WdUnits.wdCell; ////把DataGrid中数据填充到标签TWaterTable3上 //if(wApp.ActiveDocument.Bookmarks.Exists("TWaterTable3") == true) //{ // wApp.ActiveDocument.Bookmarks.get_Item // (ref bkmG).Select(); // for(int i=0;i<this.gridEX1.RecordCount;i++) // { // if(i==0) // { // count=1; // } // else // { // count=0; // } // //需填充5列数据 // wApp.Selection.Move(ref unit,ref count); // wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[0].Text); // count=1; // wApp.Selection.Move(ref unit,ref count); // wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[1].Text); // wApp.Selection.Move(ref unit,ref count); // wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[2].Text); // wApp.Selection.Move(ref unit,ref count); // wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[3].Text); // wApp.Selection.Move(ref unit,ref count); // wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[4].Text); // //换行 // wApp.Selection.MoveRight(ref unit,ref count,ref extend); // } //} } #endregion #region 读取Word到内存中 public void ReadWordIntoMemory(string filename) { Word.Application wordApp = new Word.Application(); object file = filename; object nullobj = System.Reflection.Missing.Value; Word.Document doc = wordApp.Documents.Open( ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); doc.Close(); } #endregion } }