溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中using word的使用方法

發布時間:2021-07-16 10:16:40 來源:億速云 閱讀:171 作者:chen 欄目:編程語言

這篇文章主要介紹“C#中using word的使用方法”,在日常操作中,相信很多人在C#中using word的使用方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C#中using word的使用方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

在C#中using word的命名空間,大多是利用Microsoft.Office.Interop.Word來生成word的方法。以下是一些C#中using word的不同用法

將現有的C#中using word勞動成果放在這里。有時間在加以完善!

一、添加頁眉

view plaincopy to clipboardprint?  using System;     using System.Collections.Generic;     using System.ComponentModel;     using System.Data;     using System.Linq;     using System.Text;     using Word = Microsoft.Office.Interop.Word;     using System.IO;     using System.Reflection;     using Microsoft.Office.Interop.Word;             namespace WordCreateDLL     {        public class AddHeader         {             public static void AddSimpleHeader(Application WordApp,string HeaderText)             {                 //添加頁眉                 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;                 WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);                 WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//設置左對齊                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;             }             public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)             {                 //添加頁眉                 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;                 WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);                 //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//設置字體顏色                 WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設置左對齊                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;             }             public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)             {                 //添加頁眉                 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;                 WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);                 WordApp.Selection.Font.Color =fontcolor;//設置字體顏色                 WordApp.Selection.Font.Size = fontsize;//設置字體大小                 WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設置對齊方式                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;             }                 }     }    using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Linq;  using System.Text;  using Word = Microsoft.Office.Interop.Word;  using System.IO;  using System.Reflection;  using Microsoft.Office.Interop.Word;    namespace WordCreateDLL  {     public class AddHeader      {          public static void AddSimpleHeader(Application WordApp,string HeaderText)          {              //添加頁眉              WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;              WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;              WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);              WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//設置左對齊              WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;          }          public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)          {              //添加頁眉              WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;              WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;              WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);              //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//設置字體顏色              WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設置左對齊              WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;          }          public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)          {              //添加頁眉              WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;              WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;              WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);              WordApp.Selection.Font.Color =fontcolor;//設置字體顏色              WordApp.Selection.Font.Size = fontsize;//設置字體大小              WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//設置對齊方式              WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;          }      }  }

二、插入圖片

view plaincopy to clipboardprint?  using System;     using System.Collections.Generic;     using System.ComponentModel;     using System.Data;     using System.Linq;     using System.Text;     using Word = Microsoft.Office.Interop.Word;     using System.IO;     using System.Reflection;     using Microsoft.Office.Interop.Word;         namespace WordCreateDLL     {       public class AddPic         {           public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)           {               //插入圖片               string FileName = @FName;//圖片所在路徑               object LinkToFile = false;               object SaveWithDocument = true;               object Anchor = An;               WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);               WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//圖片寬度               WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//圖片高度               //將圖片設置為四周環繞型               Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();               s.WrapFormat.Type = wdWrapType;           }             }     }    using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Linq;  using System.Text;  using Word = Microsoft.Office.Interop.Word;  using System.IO;  using System.Reflection;  using Microsoft.Office.Interop.Word;   namespace WordCreateDLL  {    public class AddPic      {        public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)        {            //插入圖片            string FileName = @FName;//圖片所在路徑            object LinkToFile = false;            object SaveWithDocument = true;            object Anchor = An;            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);            WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//圖片寬度            WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//圖片高度            //將圖片設置為四周環繞型            Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();            s.WrapFormat.Type = wdWrapType;        }       }  }

三、插入表格

view plaincopy to clipboardprint?  using System;     using System.Collections.Generic;     using System.ComponentModel;     using System.Data;     using System.Linq;     using System.Text;     using Word = Microsoft.Office.Interop.Word;     using System.IO;     using System.Reflection;     using Microsoft.Office.Interop.Word;         namespace WordCreateDLL     {        public class AddTable         {            public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)            {                Object Nothing = System.Reflection.Missing.Value;                //文檔中創建表格                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);                //設置表格樣式                newTable.Borders.OutsideLineStyle = outStyle;                newTable.Borders.InsideLineStyle = intStyle;                newTable.Columns[1].Width = 100f;                newTable.Columns[2].Width = 220f;                newTable.Columns[3].Width = 105f;                    //填充表格內容                newTable.Cell(1, 1).Range.Text = "產品詳細信息表";                newTable.Cell(1, 1).Range.Bold = 2;//設置單元格中字體為粗體                //合并單元格                newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));                WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中                WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中                    //填充表格內容                newTable.Cell(2, 1).Range.Text = "產品基本信息";                newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//設置單元格內字體顏色                //合并單元格                newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));                WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;                    //填充表格內容                newTable.Cell(3, 1).Range.Text = "品牌名稱:";                newTable.Cell(3, 2).Range.Text = "品牌名稱:";                //縱向合并單元格                newTable.Cell(3, 3).Select();//選中一行                object moveUnit = WdUnits.wdLine;                object moveCount = 5;                object moveExtend = WdMovementType.wdExtend;                WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);                WordApp.Selection.Cells.Merge();                        //插入圖片                string FileName = @"C:\1.jpg";//圖片所在路徑                object Anchor = WordDoc.Application.Selection.Range;                float Width = 200f;//圖片寬度                float Height = 200f;//圖片高度                    //將圖片設置為四周環繞型                WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;                AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);                    newTable.Cell(12, 1).Range.Text = "產品特殊屬性";                newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));                //在表格中增加行                WordDoc.Content.Tables[1].Rows.Add(ref Nothing);            }                 }     }    using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Linq;  using System.Text;  using Word = Microsoft.Office.Interop.Word;  using System.IO;  using System.Reflection;  using Microsoft.Office.Interop.Word;   namespace WordCreateDLL  {     public class AddTable      {         public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)         {             Object Nothing = System.Reflection.Missing.Value;             //文檔中創建表格             Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);             //設置表格樣式             newTable.Borders.OutsideLineStyle = outStyle;             newTable.Borders.InsideLineStyle = intStyle;             newTable.Columns[1].Width = 100f;             newTable.Columns[2].Width = 220f;             newTable.Columns[3].Width = 105f;              //填充表格內容             newTable.Cell(1, 1).Range.Text = "產品詳細信息表";             newTable.Cell(1, 1).Range.Bold = 2;//設置單元格中字體為粗體             //合并單元格             newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));             WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中             WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中              //填充表格內容             newTable.Cell(2, 1).Range.Text = "產品基本信息";             newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//設置單元格內字體顏色             //合并單元格             newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));             WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;              //填充表格內容             newTable.Cell(3, 1).Range.Text = "品牌名稱:";             newTable.Cell(3, 2).Range.Text = "品牌名稱:";             //縱向合并單元格             newTable.Cell(3, 3).Select();//選中一行             object moveUnit = WdUnits.wdLine;             object moveCount = 5;             object moveExtend = WdMovementType.wdExtend;             WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);             WordApp.Selection.Cells.Merge();               //插入圖片             string FileName = @"C:\1.jpg";//圖片所在路徑             object Anchor = WordDoc.Application.Selection.Range;             float Width = 200f;//圖片寬度             float Height = 200f;//圖片高度              //將圖片設置為四周環繞型             WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;             AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);              newTable.Cell(12, 1).Range.Text = "產品特殊屬性";             newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));             //在表格中增加行             WordDoc.Content.Tables[1].Rows.Add(ref Nothing);         }      }  }

四、插入chart

view plaincopy to clipboardprint?  using System;     using System.Collections.Generic;     using System.ComponentModel;     using System.Data;     using System.Linq;     using System.Text;     using Word = Microsoft.Office.Interop.Word;     using System.IO;     using System.Reflection;     using Microsoft.Office.Interop.Word;     using Microsoft.Office.Interop.Graph;     using System.Windows.Forms;     using System.Drawing;             namespace WordCreateDLL     {         public class AddChart         {             public static void AddSimpleChart(Document WordDoc, Word.Application WordApp, Object oEndOfDoc, string [,]data)             {                 //插入chart                   object oMissing = System.Reflection.Missing.Value;                 Word.InlineShape oShape;                 object oClassType = "MSGraph.Chart.8";                 Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                 oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,                     ref oMissing, ref oMissing, ref oMissing,                     ref oMissing, ref oMissing, ref oMissing);                     //Demonstrate use of late bound oChart and oChartApp objects to                 //manipulate the chart object with MSGraph.                 object oChart;                 object oChartApp;                 oChart = oShape.OLEFormat.Object;                 oChartApp = oChart.GetType().InvokeMember("Application",BindingFlags.GetProperty, null, oChart, null);                     //Change the chart type to Line.                 object[] Parameters = new Object[1];                 Parameters[0] = 4; //xlLine = 4                 oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,                     null, oChart, Parameters);                         Chart objChart = (Chart)oShape.OLEFormat.Object;                 objChart.ChartType = XlChartType.xlColumnClustered;                     //綁定數據                 DataSheet dataSheet;                 dataSheet = objChart.Application.DataSheet;                 int rownum=data.GetLength(0);                 int columnnum=data.GetLength(1);                 for(int i=1;i<=rownum;i++ )                     for (int j = 1; j <= columnnum; j++)                     {                         dataSheet.Cells[i,j] =data[i-1,j-1];                               }                                objChart.Application.Update();                 oChartApp.GetType().InvokeMember("Update",                     BindingFlags.InvokeMethod, null, oChartApp, null);                 oChartApp.GetType().InvokeMember("Quit",                     BindingFlags.InvokeMethod, null, oChartApp, null);                     //設置大小                 oShape.Width = WordApp.InchesToPoints(6.25f);                 oShape.Height = WordApp.InchesToPoints(3.57f);                 }         }     }    using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Linq;  using System.Text;  using Word = Microsoft.Office.Interop.Word;  using System.IO;  using System.Reflection;  using Microsoft.Office.Interop.Word;  using Microsoft.Office.Interop.Graph;  using System.Windows.Forms;  using System.Drawing;    namespace WordCreateDLL  {      public class AddChart      {          public static void AddSimpleChart(Document WordDoc, Word.Application WordApp, Object oEndOfDoc, string [,]data)          {              //插入chart                object oMissing = System.Reflection.Missing.Value;              Word.InlineShape oShape;              object oClassType = "MSGraph.Chart.8";              Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;              oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,                  ref oMissing, ref oMissing, ref oMissing,                  ref oMissing, ref oMissing, ref oMissing);               //Demonstrate use of late bound oChart and oChartApp objects to              //manipulate the chart object with MSGraph.              object oChart;              object oChartApp;              oChart = oShape.OLEFormat.Object;              oChartApp = oChart.GetType().InvokeMember("Application",BindingFlags.GetProperty, null, oChart, null);               //Change the chart type to Line.              object[] Parameters = new Object[1];              Parameters[0] = 4; //xlLine = 4              oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,                  null, oChart, Parameters);                Chart objChart = (Chart)oShape.OLEFormat.Object;              objChart.ChartType = XlChartType.xlColumnClustered;               //綁定數據              DataSheet dataSheet;              dataSheet = objChart.Application.DataSheet;              int rownum=data.GetLength(0);              int columnnum=data.GetLength(1);              for(int i=1;i<=rownum;i++ )                  for (int j = 1; j <= columnnum; j++)                  {                      dataSheet.Cells[i,j] =data[i-1,j-1];                         }                          objChart.Application.Update();              oChartApp.GetType().InvokeMember("Update",                  BindingFlags.InvokeMethod, null, oChartApp, null);              oChartApp.GetType().InvokeMember("Quit",                  BindingFlags.InvokeMethod, null, oChartApp, null);               //設置大小              oShape.Width = WordApp.InchesToPoints(6.25f);              oShape.Height = WordApp.InchesToPoints(3.57f);          }      }  }

C#中using word測試程序

view plaincopy to clipboardprint?  private void button3_Click(object sender, EventArgs e)            {                        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);                    ////添加頁眉                String HeaderText = "石化盈科";               // AddHeader.AddSimpleHeader(oWord, HeaderText);                WdParagraphAlignment wdAlign = WdParagraphAlignment.wdAlignParagraphCenter;                WdColor fontcolor = WdColor.wdColorBlue;                float fontsize = 10;                //AddHeader.AddSimpleHeader(oWord, HeaderText, wdAlign);                AddHeader.AddSimpleHeader(oWord, HeaderText, wdAlign,fontcolor,fontsize);                    //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();                    //Insert a paragraph at the end of the document.                Word.Paragraph oPara2;                object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);                oPara2.Range.Text = "Heading 2";                oPara2.Format.SpaceAfter = 6;                oPara2.Range.InsertParagraphAfter();                    //插入文字                Word.Paragraph oPara3;                oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);                oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";                oPara3.Range.Font.Bold = 0;                oPara3.Format.SpaceAfter = 24;                oPara3.Range.InsertParagraphAfter();                    string text = "zhangruichao ";                WdColor textcolor = fontcolor;                float textsize = 12;                AddLine.AddSimpLine(oDoc, oEndOfDoc, oRng, text, textcolor, textsize);                        //插入表格                WdLineStyle OutStyle = WdLineStyle.wdLineStyleThickThinLargeGap;                WdLineStyle InStyle = WdLineStyle.wdLineStyleSingle;                AddTable.AddSimpleTable(oWord, oDoc, 12, 3, OutStyle, InStyle);                    //插入分頁                Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;                object oPageBreak = Word.WdBreakType.wdPageBreak;                wrdRng.Collapse(ref oCollapseEnd);                wrdRng.InsertBreak(ref oPageBreak);                wrdRng.Collapse(ref oCollapseEnd);                wrdRng.InsertAfter("We're now on page 2. Here's my chart:");                wrdRng.InsertParagraphAfter();                    //Insert a chart.                string[,] data=new string[4,5];                    data[0,1] = "第一月";                data[0, 2] = "第二月";                data[0, 3] = "第三月";                data[0, 4] = "第四月";                data[1,0] = "東部";                data[1,1] = "50";                data[1,2] = "50";                data[1,3] = "40";                data[1,4] = "50";                  data[2,0] = "西部";                data[2,1] = "60";                data[2,2] = "60";                data[2,3] = "70";                data[2,4] = "80";                    //data[3,6] = "0";                data[3,0] = "中部";                data[3,1] = "50";                data[3,2] = "50";                data[3,3] = "40";                data[3,4] = "50";                  AddChart.AddSimpleChart(oDoc, oWord, oEndOfDoc, data);                    wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                wrdRng.InsertParagraphAfter();                wrdRng.InsertAfter("THE END.");                    //Close this form.                this.Close();            }

到此,關于“C#中using word的使用方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女