转载 https://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746.html
Microsoft.Office.Interop.Word 创建word

一、添加页眉

    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 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, stringHeaderText, 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, stringHeaderText, WdParagraphAlignment wdAlign, WdColor fontcolor, floatfontsize){ //添加页眉 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 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 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 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);}}
}

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, refNothing, 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; objectmoveExtend = WdMovementType.wdExtend; WordApp.Selection.MoveDown(refmoveUnit, 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

    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);}}
}

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();}

Microsoft.Office.Interop.Word 创建word相关推荐

  1. Net使用Microsoft.Office.Interop.Excel;创建Excel文件(插入数据、修改格式、生成图表)的方法,以及Excel查看加密

    具体使用方法可以参照文章: http://blog.csdn.net/tuoxie5431/article/details/3937752 Microsoft.Office.Interop.Excel ...

  2. C#制作Office插件时引用Microsoft.Office.Interop.Word后获取运行实例

    自动化 Office 的客户端程序既可以创建 Office 程序的新实例,也可以获取对已在运行的实例的引用. Microsoft 通常建议您创建一个新实例,而不是附加到正在运行的实例. 但是,在某些情 ...

  3. 关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。...

    控制word表格单元格内部文字样式.我要将数据导出到word当中,对于word表格一个单元格中的一段文字,要设置不同的样式,比如第一行文字作为标题要居中,加粗,第二行为正常的正文. 代码如下 publ ...

  4. C# 使用Microsoft.Office.Interop将Excel、Word转换成PDF遇到的问题总结

    首先应用中引入Microsoft.Office.Interop.Excel.Microsoft.Office.Interop.Word两个dll,将嵌入式互操作类型设为False, WORD转换成PD ...

  5. 通过C#Microsoft.Office.Interop.Word理解互操作性

    目录 一.概念 二.应用 三.Word操作示例 四.总结 五.拓展 一.概念 C# 中的互操作性指的是C#托管代码和非托管代码之间实现互操作性的方法.(参考微软文档:互操作性概述) 参考:百度百科-- ...

  6. C#,使用office组件Microsoft.Office.Interop.Word,将网页内容下载为word的demo及权限配置要点。

    在做网页开发的时候,经常会遇到下载需求,以下这种方式是保存为原生的word格式.非直接将HTML另存为. 1. 首先是下载为word 的代码. 引用: using MSWord = Microsoft ...

  7. C# Microsoft.Office.Interop.Word比较VBA

    Microsoft.Office.Interop.Word 和 VBA 这俩货绝对是一样的 需要编辑格式如下 使用 此处下划线 .方法实现与HomeKey.EndKey类似的效果. 查了若干资料后 逻 ...

  8. 【C#】C#使用Microsoft.Office.Interop.Word操作Word文档,替换文本

    最近要做一个批量打印文档功能,于是找了Microsoft.Office.Interop.Word这个插件. 基本步骤是这样的,创建Word模板文档,C#调用模板文档替换对应的文本,保存新文档. 下面仅 ...

  9. Word VSTO Error:Interop type 'Microsoft.Office.Interop.OneNote.Application' cannot be embedded...

    来到公司连做了几个基本MS Office Word的开发,看来我与Office有点渊源 哈哈 今天我在做Word VSTO项目是后发生了编译错误:"Interop type 'Microso ...

最新文章

  1. 哪个才是解决回归问题的最佳算法?线性回归、神经网络还是随机森林?
  2. platform_driver_probe与platform_driver_register的区别
  3. ”这个动作需要从没有授权的软件源来安装软件包“解决办法
  4. C++ 标准库类型 vector
  5. Java13-day04【Integer、int和String的相转、自动装箱和拆箱、Date、SimpleDateFormat、Calendar、异常、try...catch、throws】
  6. php 判断是否文件,php 判断是否一个文件的函数is_file()应用举例
  7. JEECG参与2017年度最受欢迎开源项目投票,请投上宝贵一票
  8. java 输入流可以合并吗_Java 使用IO流实现大文件的分割与合并实例详解
  9. python调用jar包的模块_使用 Python 的 JPype 模块调用 Jar 包
  10. 第一篇博客---JavaSE基础部分回顾总结01概述、基础语法
  11. 20170826滴滴算法工程师笔试
  12. CAN FD安全通信
  13. SREng 使用指南(三)系统修复的详细解说
  14. QCOM和其他常见芯片平台术语缩写
  15. POI设置导出的EXCEL锁定指定的单元格
  16. PAT乙级-1055 集体照 (25分)
  17. Android之提示MIME type application/octet-stream cannot be inserted into **/images expected
  18. php中美元符号是什么意思,js程序中美元符号$是什么
  19. win10关闭任务栏窗口预览
  20. AtCoder2362 - Splatter Painting - DFS+思维

热门文章

  1. chrome添加主题_如何在Chrome中添加和删除主题
  2. Linux中部署Spring Cloud项目
  3. navigationBar的控制
  4. 信道检测手机软件 ios_【安卓+iOS】卧龙影视,老牌观影神器上架苹果TF版,支持双端,限时下载!...
  5. 546家企业被列为建议支持的国家级专精特新“小巨人”企业
  6. arm-3-中断体系结构
  7. 海思开发板FFmpeg+Nginx,推流RTMP播放(优秀教程收集+实操整理)
  8. python海龟作图好看图案_编程入门 06:Python 海龟绘图
  9. 今天玩了一款游戏,真不错哦,英文的
  10. 转载:word2019写公式序号