最近頻繁操做Word文檔,寫了不少word的操做代碼及方法,雖然已經有不少關於word的操做類了,本身仍是進行了一下整合:web
一、經過模板建立新文件ide
二、在書籤處插入值字體
三、插入表格ui
四、合併單元格this
五、設置單元格內容對齊方式spa
六、設置表格字體code
七、是否使用邊框orm
八、給第N個表格添加行對象
九、給表格添加一行blog
十、給第N個表格添加N行
十一、填充表格的單元格
十二、給第N個表格插入內容
1三、給表格插入一行數據
1四、插入指定高寬圖片
1五、插入圖片
1六、插入一段文字
1七、保存文檔
1八、關閉Word進程
using System; using System.Collections.Generic; using System.Text; using Microsoft.Office.Interop.Word; namespace FD.Utility { /// <summary> /// word操做類 /// </summary> public class DocHelper { private _Application wordApp = null; private _Document wordDoc = null; public _Application Applicaton { get { return wordApp; } set { wordApp = value; } } public _Document WordDoc { get { return wordDoc; } set { wordDoc = value; } } /// <summary> /// 經過模板建立新文件 /// </summary> /// <param name="filePath"></param> public void CreateNewDocument(string filePath) { //killWnWordProcess(); wordApp = new ApplicationClass(); wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApp.Visible = false; object missing = System.Reflection.Missing.Value; object templateName = filePath; wordDoc = wordApp.Documents.Open(ref templateName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } /// <summary> /// 保存文檔 /// </summary> /// <param name="filePath"></param> public void SaveDocument(string filePath) { object fileName = filePath; object format = WdSaveFormat.wdFormatDocument; //保存格式 object missing = System.Reflection.Missing.Value; wordDoc.SaveAs(ref fileName, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); //關閉wordDoc,wordApp對象 object saveChanges = WdSaveOptions.wdSaveChanges; object originalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object routeDocument = false; wordDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument); wordApp.Quit(ref saveChanges, ref originalFormat, ref routeDocument); } /// <summary> /// 在書籤處插入值 /// </summary> /// <param name="bookmark">書籤</param> /// <param name="value">要插入的值</param> /// <returns></returns> public bool InsertValue(string bookmark, string value) { object bkObj = bookmark; if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark)) { wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select(); wordApp.Selection.TypeText(value); return true; } return false; } /// <summary> ///插入表格 /// </summary> /// <param name="bookmark">書籤</param> /// <param name="rows"></param> /// <param name="columns"></param> /// <param name="width"></param> /// <returns></returns> public Table InsertTable(string bookmark, int rows, int columns, float width) { object miss = System.Reflection.Missing.Value; object oStart = bookmark; Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range; //表格插入位置 Table newTable = wordDoc.Tables.Add(range, rows, columns, ref miss, ref miss); newTable.Borders.Enable = 1;//容許有邊框,默認沒有邊框,(1,實現邊框,2/3,虛線邊框) newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;//邊框寬度 if (width != 0) { newTable.PreferredWidth = width; //表格寬度 } newTable.AllowPageBreaks = false; return newTable; } /// <summary> /// 合併單元格 /// </summary> /// <param name="table">表</param> /// <param name="row1">開始行號</param> /// <param name="column1">開始列號</param> /// <param name="row2">結束行號</param> /// <param name="column2">結束列號</param> public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2) { table.Cell(row1, column1).Merge(table.Cell(row2, column2)); } /// <summary> /// 設置表格內容對齊方式 /// </summary> /// <param name="table">表</param> /// <param name="Align">水平方向:-1,左對齊;0,居中;1,右對齊</param> /// <param name="Vertical">垂直方向:-1,頂端;0,居中;1,底端</param> public void SetParagraph_Table(Microsoft.Office.Interop.Word.Table table, int Align, int Vertical) { switch (Align) { case -1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break; case 0: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break; case 1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break; } switch (Vertical) { case -1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break; case 0: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; break; case 1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom; break; } } /// <summary> /// 設置表格字體 /// </summary> /// <param name="table"></param> /// <param name="fontName"></param> /// <param name="size"></param> public void SetFont_Table(Microsoft.Office.Interop.Word.Table table, string fontName, double size) { if (size != 0) { table.Range.Font.Size = Convert.ToSingle(size); } if (fontName != "") table.Range.Font.Name = fontName; } /// <summary> /// 是否使用邊框 /// </summary> /// <param name="n">表格的序號</param> /// <param name="use">是或否</param> public void UseBroder(int n, bool use) { if (use) wordDoc.Content.Tables[n].Borders.Enable = 1; else wordDoc.Content.Tables[n].Borders.Enable = 2; } /// <summary> /// 給第n個表格添加行 /// </summary> /// <param name="n"></param> public void AddRow(int n) { object miss = System.Reflection.Missing.Value; wordDoc.Content.Tables[n].Rows.Add(ref miss); } /// <summary> /// 給表格添加一行 /// </summary> /// <param name="table"></param> public void AddRows(Microsoft.Office.Interop.Word.Table table) { object miss = System.Reflection.Missing.Value; table.Rows.Add(ref miss); } /// <summary> /// 給表格添加行 /// </summary> /// <param name="n">第n個表格</param> /// <param name="rows">添加的行數</param> public void AddRow(int n, int rows) { object miss = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n]; for (int i = 0; i < rows; i++) { table.Rows.Add(ref miss); } } /// <summary> /// 填充表格的單元格 /// </summary> /// <param name="table">表格</param> /// <param name="row">行號</param> /// <param name="column">列號</param> /// <param name="value">插入的內容</param> public void InsertCell(Microsoft.Office.Interop.Word.Table table, int row, int column, string value) { table.Cell(row, column).Range.Text = value; } /// <summary> /// 給文檔中第n個表格插入內容 /// </summary> /// <param name="n"></param> /// <param name="row">行號</param> /// <param name="column">列號</param> /// <param name="value">插入的內容</param> public void InsertCell(int n, int row, int column, string value) { wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value; } /// <summary> /// 給表格插入一行數據 /// </summary> /// <param name="n">文檔中表格的序號</param> /// <param name="row">行號</param> /// <param name="columns">列數</param> /// <param name="values">插入的值</param> public void InsertCell(int n, int row, int columns, string[] values) { Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n]; for (int i = 0; i < columns; i++) table.Cell(row, i + 1).Range.Text = values[i]; } /// <summary> /// 插入圖片 /// </summary> /// <param name="bookmark"></param> /// <param name="picturePath"></param> /// <param name="width"></param> /// <param name="hight"></param> public void InsertPicture(string bookmark, string picturePath, float width, float hight) { object miss = System.Reflection.Missing.Value; object oStart = bookmark; object linkToFile = false; //圖片不爲外部連接 object saveWithDocment = true; //圖片隨文檔一塊兒保存 object range = wordDoc.Bookmarks.get_Item(ref oStart).Range; //圖片插入位置 wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocment, ref range); wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width; wordDoc.Application.ActiveDocument.InlineShapes[1].Height = hight; } /// <summary> /// 插入圖片 /// </summary> /// <param name="bookmark"></param> /// <param name="picturePath"></param> public void InsertPicture(string bookmark, string picturePath) { object miss = System.Reflection.Missing.Value; object oStart = bookmark; object linkToFile = false; //圖片不爲外部連接 object saveWithDocment = true; //圖片隨文檔一塊兒保存 object range = wordDoc.Bookmarks.get_Item(ref oStart).Range; //圖片插入位置 wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocment, ref range); } /// <summary> /// 插入一段文字 /// </summary> /// <param name="bookmark"></param> /// <param name="text"></param> public void InsertText(string bookmark, string text) { object oStart = bookmark; object range = wordDoc.Bookmarks.get_Item(ref oStart).Range; Paragraph wp = wordDoc.Content.Paragraphs.Add(ref range); wp.Format.SpaceBefore = 6; wp.Range.Text = text; wp.Format.SpaceAfter = 24; wp.Range.InsertParagraphAfter(); wordDoc.Paragraphs.Last.Range.Text = "\n"; } /// <summary> /// 關閉Word進程 /// </summary> public void killWnWordProcess() { System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD"); foreach (System.Diagnostics.Process process in processes) { bool b = process.MainWindowTitle == ""; if (process.MainWindowTitle == "") { process.Kill(); } } } } }
QQ交流羣:200034305