• 添加引用

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. SVN服务器搭建和使用(一)(转载)

    转载地址:http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html Subversion是优秀的版本控制工具,其具体的的优点 ...

  2. Unity3D入门之GUI基础以及常用GUI控件使用(2)

    1.GUI基础 (1)GUI部分是每帧擦除重绘的,只应该在OnGUI中绘制GUI,按钮:GUILayout.Button(“Hello”); 只读标签:GUILayout.Label() (2)修改控 ...

  3. zxing实现二维码生成和解析

    转自:http://kesun-shy.iteye.com/blog/2154169 二维码的生成与解析.有多种途径.我选择用大品牌,google老大的zxing. gitHub链接是(我用的3.0. ...

  4. 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数

    [bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...

  5. cocos2dx游戏开发——微信打飞机学习笔记(十)——碰撞检测的搭建

    一.七说八说        大家都发现了= =,做了那么多,发现就是摆设,完全没有打飞机的感觉,没有实现碰撞的监测.比如说呢,子弹和敌机,玩家与敌机就是需要有碰撞检测的说,然后在这篇我想会很长很长的教 ...

  6. ThinkPHP中where()方法的使用

    where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询.表达式查询.快捷查询.区间查询.组合查询在内的查询操作.where方法 ...

  7. Android 编程下 App Install Location

    从 API 8 开始(参考官方文档:App Install Location | Android Developers),你可以将你的应用安装在外部储存中(例如,安装到设备的 SD 卡上).这是一个可 ...

  8. HDU2204 Eddy's爱好(容斥原理)

    题目问[1,n]有几个数是$m^k (k>1)$形式. 如果这样考虑,m已知k未知,对于每一个m统计其k的数量即$\lfloor log_mn \rfloor$个,再容斥,然而m太多了,完全不可 ...

  9. 2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  10. ural 1221. Malevich Strikes Back!

    1221. Malevich Strikes Back! Time limit: 1.0 secondMemory limit: 64 MB After the greatest success of ...