• 添加引用

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. Striiv Myland 攻略

    推荐一款IOS平台上的运动APP:Striiv.可以记录平时的步数,路程,卡路里,运动时间.如果购买相应的硬件,还可以记录每天爬了多少台阶(这是专门为爬楼者用的么...). 其中比较吸引我的是里面有个 ...

  2. select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET(转)

    select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型, 原型: int select(int maxfd,fd_set *rds ...

  3. XML基础总结2

    在上篇的博客中,我们系统的介绍了一下xml与html之间的异同以及一部分xml的特性或者说是优点,接下来,我们就xml文档的一些语法规则具体向大家阐述一下: 1.xml文档形成了一种"树结构 ...

  4. hadoop本地库无法加载

    (1)设置环境变量 export HADOOP_HOME=/your/own/hadoop/home  export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME ...

  5. 写好的mapreduce程序,编译,打包,得到最后的jar包! 验证jar包 ! 整体流程

    创建一个bin目录,用于存放编译.java文件产生的.class等结果,然后编译! 编译结果! 打包操作! 打包结果! 验证打包生成的jar包,是否正常,验证成功!!!!!!!!!!!! 结果正确!! ...

  6. javascript优化--04高质量编码

    库和API的设计: 在参数设计中保持好的习惯:如顺序,width,height;top,right,bottom,left;如命名: 将undefined看作没有值而不要表示非特定的值: 在允许0,空 ...

  7. Xamarin.Android提示aapt退出,代码为255

    Xamarin.Android提示aapt退出,代码为255 错误信息:”aapt.exe”已退出,代码为255.出现这种问题,通常是由于该项目所使用Android SDK不完整.通过SDK Mana ...

  8. 贪心 URAL 1303 Minimal Coverage

    题目传送门 /* 题意:最少需要多少条线段能覆盖[0, m]的长度 贪心:首先忽略被其他线段完全覆盖的线段,因为选取更长的更优 接着就是从p=0开始,以p点为标志,选取 (node[i].l < ...

  9. Codeforces 219D Choosing Capital for Treeland(树形DP)

    题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...

  10. TYVJ P1008 传球游戏

      时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2008复赛普及组第三题 描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着 ...