C#中using word相关用法及代码示例

开发 后端
这里将介绍C#中using word相关用法及代码示例,笔者搜集了Microsoft.Office.Interop.Word生产word的一些方法,希望对大家有所帮助。

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

将现有的C#中using word劳动成果放在这里。有时间在加以完善!

一、添加页眉

  1. view plaincopy to clipboardprint?  
  2. using System;     
  3. using System.Collections.Generic;     
  4. using System.ComponentModel;     
  5. using System.Data;     
  6. using System.Linq;     
  7. using System.Text;     
  8. using Word = Microsoft.Office.Interop.Word;     
  9. using System.IO;     
  10. using System.Reflection;     
  11. using Microsoft.Office.Interop.Word;     
  12.     
  13.     
  14. namespace WordCreateDLL     
  15. {     
  16.    public class AddHeader     
  17.     {     
  18.         public static void AddSimpleHeader(Application WordApp,string HeaderText)     
  19.         {     
  20.             //添加页眉     
  21.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;     
  22.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;     
  23.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);     
  24.             WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置左对齐     
  25.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;     
  26.         }     
  27.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)     
  28.         {     
  29.             //添加页眉     
  30.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;     
  31.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;     
  32.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);     
  33.             //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//设置字体颜色     
  34.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//设置左对齐     
  35.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;     
  36.         }     
  37.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)     
  38.         {     
  39.             //添加页眉     
  40.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;     
  41.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;     
  42.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);     
  43.             WordApp.Selection.Font.Color =fontcolor;//设置字体颜色     
  44.             WordApp.Selection.Font.Size = fontsize;//设置字体大小     
  45.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//设置对齐方式     
  46.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;     
  47.         }     
  48.     
  49.     
  50.     }     
  51. }    
  52. using System;  
  53. using System.Collections.Generic;  
  54. using System.ComponentModel;  
  55. using System.Data;  
  56. using System.Linq;  
  57. using System.Text;  
  58. using Word = Microsoft.Office.Interop.Word;  
  59. using System.IO;  
  60. using System.Reflection;  
  61. using Microsoft.Office.Interop.Word;  
  62.  
  63.  
  64. namespace WordCreateDLL  
  65. {  
  66.    public class AddHeader  
  67.     {  
  68.         public static void AddSimpleHeader(Application WordApp,string HeaderText)  
  69.         {  
  70.             //添加页眉  
  71.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;  
  72.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;  
  73.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);  
  74.             WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置左对齐  
  75.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;  
  76.         }  
  77.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign)  
  78.         {  
  79.             //添加页眉  
  80.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;  
  81.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;  
  82.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);  
  83.             //WordApp.Selection.Font.Color = WdColor.wdColorDarkRed;//设置字体颜色  
  84.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//设置左对齐  
  85.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;  
  86.         }  
  87.         public static void AddSimpleHeader(Application WordApp, string HeaderText, WdParagraphAlignment wdAlign,WdColor fontcolor,float fontsize)  
  88.         {  
  89.             //添加页眉  
  90.             WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;  
  91.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;  
  92.             WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(HeaderText);  
  93.             WordApp.Selection.Font.Color =fontcolor;//设置字体颜色  
  94.             WordApp.Selection.Font.Size = fontsize;//设置字体大小  
  95.             WordApp.Selection.ParagraphFormat.Alignment = wdAlign;//设置对齐方式  
  96.             WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;  
  97.         }  
  98.     }  

二、插入图片

  1. view plaincopy to clipboardprint?  
  2. using System;     
  3. using System.Collections.Generic;     
  4. using System.ComponentModel;     
  5. using System.Data;     
  6. using System.Linq;     
  7. using System.Text;     
  8. using Word = Microsoft.Office.Interop.Word;     
  9. using System.IO;     
  10. using System.Reflection;     
  11. using Microsoft.Office.Interop.Word;     
  12.     
  13. namespace WordCreateDLL     
  14. {     
  15.   public class AddPic     
  16.     {     
  17.       public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)     
  18.       {     
  19.           //插入图片     
  20.           string FileName = @FName;//图片所在路径     
  21.           object LinkToFile = false;     
  22.           object SaveWithDocument = true;     
  23.           object Anchor = An;     
  24.           WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);     
  25.           WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//图片宽度     
  26.           WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//图片高度     
  27.           //将图片设置为四周环绕型     
  28.           Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();     
  29.           s.WrapFormat.Type = wdWrapType;     
  30.       }     
  31.     
  32.     }     
  33. }    
  34. using System;  
  35. using System.Collections.Generic;  
  36. using System.ComponentModel;  
  37. using System.Data;  
  38. using System.Linq;  
  39. using System.Text;  
  40. using Word = Microsoft.Office.Interop.Word;  
  41. using System.IO;  
  42. using System.Reflection;  
  43. using Microsoft.Office.Interop.Word;  
  44.  
  45. namespace WordCreateDLL  
  46. {  
  47.   public class AddPic  
  48.     {  
  49.       public static void AddSimplePic(Document WordDoc, string FName, float Width, float Height, object An, WdWrapType wdWrapType)  
  50.       {  
  51.           //插入图片  
  52.           string FileName = @FName;//图片所在路径  
  53.           object LinkToFile = false;  
  54.           object SaveWithDocument = true;  
  55.           object Anchor = An;  
  56.           WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);  
  57.           WordDoc.Application.ActiveDocument.InlineShapes[1].Width = Width;//图片宽度  
  58.           WordDoc.Application.ActiveDocument.InlineShapes[1].Height = Height;//图片高度  
  59.           //将图片设置为四周环绕型  
  60.           Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();  
  61.           s.WrapFormat.Type = wdWrapType;  
  62.       }  
  63.  
  64.     }  

三、插入表格

  1. view plaincopy to clipboardprint?  
  2. using System;     
  3. using System.Collections.Generic;     
  4. using System.ComponentModel;     
  5. using System.Data;     
  6. using System.Linq;     
  7. using System.Text;     
  8. using Word = Microsoft.Office.Interop.Word;     
  9. using System.IO;     
  10. using System.Reflection;     
  11. using Microsoft.Office.Interop.Word;     
  12.     
  13. namespace WordCreateDLL     
  14. {     
  15.    public class AddTable     
  16.     {     
  17.        public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)     
  18.        {     
  19.            Object Nothing = System.Reflection.Missing.Value;     
  20.            //文档中创建表格     
  21.            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);     
  22.            //设置表格样式     
  23.            newTable.Borders.OutsideLineStyle = outStyle;     
  24.            newTable.Borders.InsideLineStyle = intStyle;     
  25.            newTable.Columns[1].Width = 100f;     
  26.            newTable.Columns[2].Width = 220f;     
  27.            newTable.Columns[3].Width = 105f;     
  28.     
  29.            //填充表格内容     
  30.            newTable.Cell(1, 1).Range.Text = "产品详细信息表";     
  31.            newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体     
  32.            //合并单元格     
  33.            newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));     
  34.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中     
  35.            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中     
  36.     
  37.            //填充表格内容     
  38.            newTable.Cell(2, 1).Range.Text = "产品基本信息";     
  39.            newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//设置单元格内字体颜色     
  40.            //合并单元格     
  41.            newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));     
  42.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;     
  43.     
  44.            //填充表格内容     
  45.            newTable.Cell(3, 1).Range.Text = "品牌名称:";     
  46.            newTable.Cell(3, 2).Range.Text = "品牌名称:";     
  47.            //纵向合并单元格     
  48.            newTable.Cell(3, 3).Select();//选中一行     
  49.            object moveUnit = WdUnits.wdLine;     
  50.            object moveCount = 5;     
  51.            object moveExtend = WdMovementType.wdExtend;     
  52.            WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);     
  53.            WordApp.Selection.Cells.Merge();     
  54.     
  55.     
  56.            //插入图片     
  57.            string FileName = @"C:\1.jpg";//图片所在路径     
  58.            object Anchor = WordDoc.Application.Selection.Range;     
  59.            float Width = 200f;//图片宽度     
  60.            float Height = 200f;//图片高度     
  61.     
  62.            //将图片设置为四周环绕型     
  63.            WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;     
  64.            AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);     
  65.     
  66.            newTable.Cell(12, 1).Range.Text = "产品特殊属性";     
  67.            newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));     
  68.            //在表格中增加行     
  69.            WordDoc.Content.Tables[1].Rows.Add(ref Nothing);     
  70.        }     
  71.     
  72.     
  73.     }     
  74. }    
  75. using System;  
  76. using System.Collections.Generic;  
  77. using System.ComponentModel;  
  78. using System.Data;  
  79. using System.Linq;  
  80. using System.Text;  
  81. using Word = Microsoft.Office.Interop.Word;  
  82. using System.IO;  
  83. using System.Reflection;  
  84. using Microsoft.Office.Interop.Word;  
  85.  
  86. namespace WordCreateDLL  
  87. {  
  88.    public class AddTable  
  89.     {  
  90.        public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle)  
  91.        {  
  92.            Object Nothing = System.Reflection.Missing.Value;  
  93.            //文档中创建表格  
  94.            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, numrows, numcolumns, ref Nothing, ref Nothing);  
  95.            //设置表格样式  
  96.            newTable.Borders.OutsideLineStyle = outStyle;  
  97.            newTable.Borders.InsideLineStyle = intStyle;  
  98.            newTable.Columns[1].Width = 100f;  
  99.            newTable.Columns[2].Width = 220f;  
  100.            newTable.Columns[3].Width = 105f;  
  101.  
  102.            //填充表格内容  
  103.            newTable.Cell(1, 1).Range.Text = "产品详细信息表";  
  104.            newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体  
  105.            //合并单元格  
  106.            newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));  
  107.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中  
  108.            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中  
  109.  
  110.            //填充表格内容  
  111.            newTable.Cell(2, 1).Range.Text = "产品基本信息";  
  112.            newTable.Cell(2, 1).Range.Font.Color =WdColor.wdColorDarkBlue;//设置单元格内字体颜色  
  113.            //合并单元格  
  114.            newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));  
  115.            WordApp.Selection.Cells.VerticalAlignment =WdCellVerticalAlignment.wdCellAlignVerticalCenter;  
  116.  
  117.            //填充表格内容  
  118.            newTable.Cell(3, 1).Range.Text = "品牌名称:";  
  119.            newTable.Cell(3, 2).Range.Text = "品牌名称:";  
  120.            //纵向合并单元格  
  121.            newTable.Cell(3, 3).Select();//选中一行  
  122.            object moveUnit = WdUnits.wdLine;  
  123.            object moveCount = 5;  
  124.            object moveExtend = WdMovementType.wdExtend;  
  125.            WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);  
  126.            WordApp.Selection.Cells.Merge();  
  127.  
  128.  
  129.            //插入图片  
  130.            string FileName = @"C:\1.jpg";//图片所在路径  
  131.            object Anchor = WordDoc.Application.Selection.Range;  
  132.            float Width = 200f;//图片宽度  
  133.            float Height = 200f;//图片高度  
  134.  
  135.            //将图片设置为四周环绕型  
  136.            WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;  
  137.            AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType);  
  138.  
  139.            newTable.Cell(12, 1).Range.Text = "产品特殊属性";  
  140.            newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));  
  141.            //在表格中增加行  
  142.            WordDoc.Content.Tables[1].Rows.Add(ref Nothing);  
  143.        }  
  144.     }  

四、插入chart

  1. view plaincopy to clipboardprint?  
  2. using System;     
  3. using System.Collections.Generic;     
  4. using System.ComponentModel;     
  5. using System.Data;     
  6. using System.Linq;     
  7. using System.Text;     
  8. using Word = Microsoft.Office.Interop.Word;     
  9. using System.IO;     
  10. using System.Reflection;     
  11. using Microsoft.Office.Interop.Word;     
  12. using Microsoft.Office.Interop.Graph;     
  13. using System.Windows.Forms;     
  14. using System.Drawing;     
  15.     
  16.     
  17. namespace WordCreateDLL     
  18. {     
  19.     public class AddChart     
  20.     {     
  21.         public static void AddSimpleChart(Document WordDoc, Word.Application WordApp, Object oEndOfDoc, string [,]data)     
  22.         {     
  23.             //插入chart       
  24.             object oMissing = System.Reflection.Missing.Value;     
  25.             Word.InlineShape oShape;     
  26.             object oClassType = "MSGraph.Chart.8";     
  27.             Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;     
  28.             oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,     
  29.                 ref oMissing, ref oMissing, ref oMissing,     
  30.                 ref oMissing, ref oMissing, ref oMissing);     
  31.     
  32.             //Demonstrate use of late bound oChart and oChartApp objects to     
  33.             //manipulate the chart object with MSGraph.     
  34.             object oChart;     
  35.             object oChartApp;     
  36.             oChart = oShape.OLEFormat.Object;     
  37.             oChartApp = oChart.GetType().InvokeMember("Application",BindingFlags.GetProperty, null, oChart, null);     
  38.     
  39.             //Change the chart type to Line.     
  40.             object[] Parameters = new Object[1];     
  41.             Parameters[0] = 4; //xlLine = 4     
  42.             oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,     
  43.                 null, oChart, Parameters);     
  44.     
  45.     
  46.             Chart objChart = (Chart)oShape.OLEFormat.Object;     
  47.             objChart.ChartType = XlChartType.xlColumnClustered;     
  48.     
  49.             //绑定数据     
  50.             DataSheet dataSheet;     
  51.             dataSheet = objChart.Application.DataSheet;     
  52.             int rownum=data.GetLength(0);     
  53.             int columnnum=data.GetLength(1);     
  54.             for(int i=1;i<=rownum;i++ )     
  55.                 for (int j = 1; j <= columnnum; j++)     
  56.                 {      
  57.                    dataSheet.Cells[i,j] =data[i-1,j-1];     
  58.           
  59.                 }     
  60.                
  61.             objChart.Application.Update();     
  62.             oChartApp.GetType().InvokeMember("Update",     
  63.                 BindingFlags.InvokeMethod, null, oChartApp, null);     
  64.             oChartApp.GetType().InvokeMember("Quit",     
  65.                 BindingFlags.InvokeMethod, null, oChartApp, null);     
  66.     
  67.             //设置大小     
  68.             oShape.Width = WordApp.InchesToPoints(6.25f);     
  69.             oShape.Height = WordApp.InchesToPoints(3.57f);     
  70.     
  71.         }     
  72.     }     
  73. }    
  74. using System;  
  75. using System.Collections.Generic;  
  76. using System.ComponentModel;  
  77. using System.Data;  
  78. using System.Linq;  
  79. using System.Text;  
  80. using Word = Microsoft.Office.Interop.Word;  
  81. using System.IO;  
  82. using System.Reflection;  
  83. using Microsoft.Office.Interop.Word;  
  84. using Microsoft.Office.Interop.Graph;  
  85. using System.Windows.Forms;  
  86. using System.Drawing;  
  87.  
  88.  
  89. namespace WordCreateDLL  
  90. {  
  91.     public class AddChart  
  92.     {  
  93.         public static void AddSimpleChart(Document WordDoc, Word.Application WordApp, Object oEndOfDoc, string [,]data)  
  94.         {  
  95.             //插入chart    
  96.             object oMissing = System.Reflection.Missing.Value;  
  97.             Word.InlineShape oShape;  
  98.             object oClassType = "MSGraph.Chart.8";  
  99.             Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;  
  100.             oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,  
  101.                 ref oMissing, ref oMissing, ref oMissing,  
  102.                 ref oMissing, ref oMissing, ref oMissing);  
  103.  
  104.             //Demonstrate use of late bound oChart and oChartApp objects to  
  105.             //manipulate the chart object with MSGraph.  
  106.             object oChart;  
  107.             object oChartApp;  
  108.             oChart = oShape.OLEFormat.Object;  
  109.             oChartApp = oChart.GetType().InvokeMember("Application",BindingFlags.GetProperty, null, oChart, null);  
  110.  
  111.             //Change the chart type to Line.  
  112.             object[] Parameters = new Object[1];  
  113.             Parameters[0] = 4; //xlLine = 4  
  114.             oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,  
  115.                 null, oChart, Parameters);  
  116.  
  117.  
  118.             Chart objChart = (Chart)oShape.OLEFormat.Object;  
  119.             objChart.ChartType = XlChartType.xlColumnClustered;  
  120.  
  121.             //绑定数据  
  122.             DataSheet dataSheet;  
  123.             dataSheet = objChart.Application.DataSheet;  
  124.             int rownum=data.GetLength(0);  
  125.             int columnnum=data.GetLength(1);  
  126.             for(int i=1;i<=rownum;i++ )  
  127.                 for (int j = 1; j <= columnnum; j++)  
  128.                 {   
  129.                    dataSheet.Cells[i,j] =data[i-1,j-1];  
  130.        
  131.                 }  
  132.             
  133.             objChart.Application.Update();  
  134.             oChartApp.GetType().InvokeMember("Update",  
  135.                 BindingFlags.InvokeMethod, null, oChartApp, null);  
  136.             oChartApp.GetType().InvokeMember("Quit",  
  137.                 BindingFlags.InvokeMethod, null, oChartApp, null);  
  138.  
  139.             //设置大小  
  140.             oShape.Width = WordApp.InchesToPoints(6.25f);  
  141.             oShape.Height = WordApp.InchesToPoints(3.57f);  
  142.         }  
  143.     }  

C#中using word测试程序

  1. view plaincopy to clipboardprint?  
  2. private void button3_Click(object sender, EventArgs e)     
  3.        {     
  4.     
  5.     
  6.            object oMissing = System.Reflection.Missing.Value;     
  7.            object oEndOfDoc = "\\endofdoc"/* \endofdoc is a predefined bookmark */    
  8.     
  9.            //Start Word and create a new document.     
  10.            Word.Application oWord;     
  11.            Word.Document oDoc;     
  12.            oWord = new Word.Application();     
  13.            oWord.Visible = true;     
  14.            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,     
  15.                ref oMissing, ref oMissing);     
  16.     
  17.            ////添加页眉     
  18.            String HeaderText = "石化盈科";     
  19.           // AddHeader.AddSimpleHeader(oWord, HeaderText);     
  20.            WdParagraphAlignment wdAlign = WdParagraphAlignment.wdAlignParagraphCenter;     
  21.            WdColor fontcolor = WdColor.wdColorBlue;     
  22.            float fontsize = 10;     
  23.            //AddHeader.AddSimpleHeader(oWord, HeaderText, wdAlign);     
  24.            AddHeader.AddSimpleHeader(oWord, HeaderText, wdAlign,fontcolor,fontsize);     
  25.     
  26.            //Insert a paragraph at the beginning of the document.     
  27.            Word.Paragraph oPara1;     
  28.            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);     
  29.            oPara1.Range.Text = "Heading 1";     
  30.            oPara1.Range.Font.Bold = 1;     
  31.            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.     
  32.            oPara1.Range.InsertParagraphAfter();     
  33.     
  34.            //Insert a paragraph at the end of the document.     
  35.            Word.Paragraph oPara2;     
  36.            object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;     
  37.            oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);     
  38.            oPara2.Range.Text = "Heading 2";     
  39.            oPara2.Format.SpaceAfter = 6;     
  40.            oPara2.Range.InsertParagraphAfter();     
  41.     
  42.            //插入文字     
  43.            Word.Paragraph oPara3;     
  44.            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;     
  45.            oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);     
  46.            oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";     
  47.            oPara3.Range.Font.Bold = 0;     
  48.            oPara3.Format.SpaceAfter = 24;     
  49.            oPara3.Range.InsertParagraphAfter();     
  50.     
  51.            string text = "zhangruichao ";     
  52.            WdColor textcolor = fontcolor;     
  53.            float textsize = 12;     
  54.            AddLine.AddSimpLine(oDoc, oEndOfDoc, oRng, text, textcolor, textsize);     
  55.     
  56.     
  57.            //插入表格     
  58.            WdLineStyle OutStyle = WdLineStyle.wdLineStyleThickThinLargeGap;     
  59.            WdLineStyle InStyle = WdLineStyle.wdLineStyleSingle;     
  60.            AddTable.AddSimpleTable(oWord, oDoc, 12, 3, OutStyle, InStyle);     
  61.     
  62.            //插入分页     
  63.            Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;     
  64.            object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;     
  65.            object oPageBreak = Word.WdBreakType.wdPageBreak;     
  66.            wrdRng.Collapse(ref oCollapseEnd);     
  67.            wrdRng.InsertBreak(ref oPageBreak);     
  68.            wrdRng.Collapse(ref oCollapseEnd);     
  69.            wrdRng.InsertAfter("We're now on page 2. Here's my chart:");     
  70.            wrdRng.InsertParagraphAfter();     
  71.     
  72.            //Insert a chart.     
  73.            string[,] data=new string[4,5];     
  74.     
  75.            data[0,1] = "第一月";     
  76.            data[0, 2] = "第二月";     
  77.            data[0, 3] = "第三月";     
  78.            data[0, 4] = "第四月";     
  79.            data[1,0] = "东部";     
  80.            data[1,1] = "50";     
  81.            data[1,2] = "50";     
  82.            data[1,3] = "40";     
  83.            data[1,4] = "50";     
  84.              data[2,0] = "西部";     
  85.            data[2,1] = "60";     
  86.            data[2,2] = "60";     
  87.            data[2,3] = "70";     
  88.            data[2,4] = "80";     
  89.     
  90.            //data[3,6] = "0";     
  91.            data[3,0] = "中部";     
  92.            data[3,1] = "50";     
  93.            data[3,2] = "50";     
  94.            data[3,3] = "40";     
  95.            data[3,4] = "50";     
  96.              AddChart.AddSimpleChart(oDoc, oWord, oEndOfDoc, data);     
  97.     
  98.            wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;     
  99.            wrdRng.InsertParagraphAfter();     
  100.            wrdRng.InsertAfter("THE END.");     
  101.     
  102.            //Close this form.     
  103.            this.Close();     
  104.        }   

【编辑推荐】

  1. C#类和结构简单介绍
  2. C# explicti和implicit详解
  3. C#编写ActiveX控件详细介绍
  4. C# StringBuilder和String浅析
  5. C#别名指示符学习经验
责任编辑:彭凡 来源: CSDN
相关推荐

2009-08-27 16:00:59

C#中using用法

2011-05-23 13:27:53

2011-07-06 10:47:52

C#using

2009-08-26 14:01:33

C# using用法

2009-09-01 16:49:56

C#文件上传下载

2009-08-26 18:10:44

C# using的用法

2009-08-26 17:21:05

C# using

2009-09-07 05:50:59

C# Timer用法

2010-03-05 11:04:00

C调用Python函数

2009-08-24 08:56:55

C#反射

2016-12-20 11:12:11

C代码自测开发

2009-08-27 16:53:05

C# using作用

2009-09-02 14:06:14

C#文件传送

2023-11-08 08:01:40

Spring购物车代码

2009-08-25 15:50:13

C#连接远程数据库

2009-09-08 17:20:01

C#排序算法

2009-12-24 09:16:11

C#泛型

2010-04-26 11:37:25

Oracle merg

2024-04-01 09:13:20

C++函数递增

2009-08-14 13:52:18

C#判断数据类型
点赞
收藏

51CTO技术栈公众号