• 添加引用

Microsoft Word  *.0 Object Library

Microsoft Graph *.0 Object Library

  • 变量说明

Object oMissing = System.Reflection.Missing.Value;

object oEndOfDoc = "\\endofdoc";

  • 操作类说明

Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

  • 操作说明

1、添加页眉

if (WordApp.ActiveWindow.ActivePane.View.Type == Microsoft.Office.Interop.Word.WdViewType.wdNormalView || WordApp.ActiveWindow.ActivePane.View.Type == Microsoft.Office.Interop.Word.WdViewType.wdOutlineView)

{

WordApp.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;

}

WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;

string sHeader = "页眉内容";

WordApp.Selection.HeaderFooter.LinkToPrevious = false;

WordApp.Selection.HeaderFooter.Range.Text = sHeader;

WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;

//WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐

WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置左对齐

WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

2、设置文档的行间距

WordApp.Selection.ParagraphFormat.LineSpacing = 15f;

3、插入表格

//移动焦点并换行

object count = 14;

object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;

WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移动焦点

WordApp.Selection.TypeParagraph();//插入段落

//文档中创建表格

Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);

//设置表格样式

newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;

newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

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 = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中

WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

//填充表格内容

newTable.Cell(2, 1).Range.Text = "产品基本信息";

newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色

//合并单元格

newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));

WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

//填充表格内容

newTable.Cell(3, 1).Range.Text = "品牌名称:";

newTable.Cell(3, 2).Range.Text = "BrandName";

//纵向合并单元格

newTable.Cell(3, 3).Select();//选中一行

object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;

object moveCount = 5;

object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;

WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);

WordApp.Selection.Cells.Merge();

//在表格中增加行

WordDoc.Content.Tables[1].Rows.Add(ref oMissing);

4、落款

WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”

WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

5、插入文字并设置格式

//在文档开始插入段落.

Microsoft.Office.Interop.Word.Paragraph oPara1;

oPara1 = WordDoc.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();

//在文档尾部插入段落

Microsoft.Office.Interop.Word.Paragraph oPara2;

object oRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

oPara2 = WordDoc.Content.Paragraphs.Add(ref oRng);

oPara2.Range.Text = "Heading 2";

oPara2.Format.SpaceAfter = 6;

oPara2.Range.InsertParagraphAfter();

6、插入图表(OLEObject方式)

#region

object oEndOfDoc = "\\endofdoc";

Microsoft.Office.Interop.Word.Range wrdRng;

//Insert a chart.

Microsoft.Office.Interop.Word.InlineShape oShape;

object oClassType = "MSGraph.Chart";

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

Microsoft.Office.Interop.Graph.Chart objChart = (Microsoft.Office.Interop.Graph.Chart)oShape.OLEFormat.Object;

objChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xlColumnClustered;

Microsoft.Office.Interop.Graph.DataSheet dataSheet;

dataSheet = objChart.Application.DataSheet;

dataSheet.Cells[1, 2] = "第一季度";

dataSheet.Cells[1, 3] = "第二季度";

dataSheet.Cells[1, 4] = "第三季度";

dataSheet.Cells[1, 5] = "第四季度";

dataSheet.Cells[2, 1] = "东部";

dataSheet.Cells[2, 2] = "50";

dataSheet.Cells[2, 3] = "40";

dataSheet.Cells[2, 4] = "50";

dataSheet.Cells[2, 5] = "50";

dataSheet.Cells[3, 1] = "西部";

dataSheet.Cells[3, 2] = "60";

dataSheet.Cells[3, 3] = "70";

dataSheet.Cells[3, 4] = "80";

dataSheet.Cells[3, 5] = "60";

dataSheet.Cells[4, 1] = "中部";

dataSheet.Cells[4, 2] = "50";

dataSheet.Cells[4, 3] = "40";

dataSheet.Cells[4, 4] = "50";

dataSheet.Cells[4, 5] = "50";

objChart.Application.Update();

//Update the chart image and quit MSGraph.

oChartApp.GetType().InvokeMember("Update", BindingFlags.InvokeMethod, null, oChartApp, null);

oChartApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, oChartApp, null);

//... If desired, you can proceed from here using the Microsoft Graph

//Object model on the oChart and oChartApp objects to make additional

//changes to the chart.

//Set the width of the chart.

oShape.Width = WordApp.InchesToPoints(6.25f);

oShape.Height = WordApp.InchesToPoints(3.57f);

//Add text after the chart.

wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

wrdRng.InsertParagraphAfter();

wrdRng.InsertAfter("THE END.");

#endregion

7、插入图表(Chart对象方式)

Microsoft.Office.Interop.Word.Chart wdChart =WordDoc.InlineShapes.AddChart(Microsoft.Office.Core.XlChartType.xlColumnClustered, ref oMissing).Chart;

Microsoft.Office.Interop.Word.ChartData chartData = wdChart.ChartData;

Microsoft.Office.Interop.Excel.Workbook dataWorkbook =(Microsoft.Office.Interop.Excel.Workbook)chartData.Workbook;

dataWorkbook.Application.Visible = false;

Microsoft.Office.Interop.Excel.Worksheet dataSheet = (Microsoft.Office.Interop.Excel.Worksheet)dataWorkbook.Worksheets[1];

Microsoft.Office.Interop.Excel.Range tRange = dataSheet.Cells.get_Range("A1", "B5");

Microsoft.Office.Interop.Excel.ListObject tbl1 = dataSheet.ListObjects[1];

tbl1.Resize(tRange);

((Microsoft.Office.Interop.Excel.Range)dataSheet.Cells.get_Range("A2",oMissing)).FormulaR1C1 = "Bikes";

((Microsoft.Office.Interop.Excel.Range)dataSheet.Cells.get_Range("A3",oMissing)).FormulaR1C1 = "Accessories";

((Microsoft.Office.Interop.Excel.Range)dataSheet.Cells.get_Range("A4",oMissing)).FormulaR1C1 = "Repairs";

((Microsoft.Office.Interop.Excel.Range)dataSheet.Cells.get_Range("A5",oMissing)).FormulaR1C1 = "Clothing";

((Microsoft.Office.Interop.Excel.Range)dataSheet.Cells.get_Range("B2",oMissing)).FormulaR1C1 = "1000";

((Microsoft.Office.Interop.Excel.Range)dataSheet.Cells.get_Range("B3",oMissing)).FormulaR1C1 = "2500";

((Microsoft.Office.Interop.Excel.Range)dataSheet.Cells.get_Range("B4",oMissing)).FormulaR1C1 = "4000";

((Microsoft.Office.Interop.Excel.Range)dataSheet.Cells.get_Range("B5",oMissing)).FormulaR1C1 = "3000";

wdChart.ChartTitle.Font.Italic = true;

wdChart.ChartTitle.Font.Size = 18;

wdChart.ChartTitle.Font.Color = Color.Black.ToArgb();

wdChart.ChartTitle.Text = "2007 Sales";

wdChart.ChartTitle.Format.Line.Visible =

Microsoft.Office.Core.MsoTriState.msoTrue;

wdChart.ChartTitle.Format.Line.ForeColor.RGB =Color.Black.ToArgb();

wdChart.ApplyDataLabels(Microsoft.Office.Interop.Word.XlDataLabelsType.xlDataLabelsShowLabel,oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,

oMissing);

C# vba 操作 Word的更多相关文章

  1. Excel VBA 操作 Word(入门篇)

    原文地址 本文的对象是:有一定Excel VBA基础,对Word VBA还没有什么认识,想在Excel中通过VBA操作Word还有困难的人.   一.新建Word引用 需要首先创建一个对 Word A ...

  2. VBA操作word生成sql语句

    项目开始一般都是用word保存下数据库的文档 但是从表单一个一个的建表实在是很困难乏味,查查资料 1.可以生成一个html或者xml,检索结构生成sql.但是这个方式也蛮麻烦 2.查到vba可以操作w ...

  3. 转发:VB程序操作word表格(文字、图片)

    很多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,google.baidu搜爆了,都没有找到我需要的东西.到是搜索到了很多问这个问题的记录.没办法,索性 ...

  4. python操作word(改课文格式)【最终版】

    python操作word的一些方法,前面写了一些感悟,有点跑题,改了下题目,方便能搜索到.心急的可以直接拉到最后看代码,我都加了比较详细的注释. 从8.3号早上9点,到8.8号下午5点半下班,终于把这 ...

  5. VBS 操作Word

    VBS 操作Word   1.新建Word文档 '使用Add方法 Dim ObjWD,ObjDOC Set ObjWD=CreateObject("Word.application" ...

  6. python操作word入门

    1.安装pywin32 http://sourceforge.net/projects/pywin32 在files里去找适合你的python版本.截止此文,最新版本是pywin32-219快捷路径: ...

  7. C#中操作Word(1)—— word对象模型介绍

    一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页 ...

  8. C#操作Word的超详细总结

    本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置 ...

  9. C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

随机推荐

  1. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. UVa 11995:I Can Guess the Data Structure!(数据结构练习)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  3. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. JAVA反射机制(转)

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它的任意一个方法和属性: 这种动态获取的信息以及动态调用对象的方法的功能称为java语言 ...

  5. GridView块布局

    <GridView android:id="@+id/gridview" android:layout_width="match_parent" andr ...

  6. Oracle创建表空间和表

    创建表空间和表ORACLE物理上是由磁盘上的以下几种文件:数据文件和控制文件和LOGFILE构成的oracle中的表就是一张存储数据的表.表空间是逻辑上的划分.方便管理的.数据表空间 (Tablesp ...

  7. CodeForces 222B Cosmic Tables

    Cosmic Tables Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  8. Memcached启停脚本小结

    编写配置文件 编写启动脚本 vim /etc/rc.d/init.d/memcached startesac and $<!= 0); } elsif (open PIDHANDLE," ...

  9. 递推DP 赛码 1005 Game

    题目传送门 /* 递推DP:官方题解 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩 ...

  10. Open Xml SDK Word模板开发最佳实践(Best Practice)

    1.概述 由于前面的引文已经对Open Xml SDK做了一个简要的介绍. 这次来点实际的——Word模板操作. 从本质上来讲,本文的操作都是基于模板替换思想的,即,我们通过替换Word模板中指定元素 ...