• 添加引用

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. Android开发方向

    运行Android平台的硬件只是手机.平台电脑等便携式设备,这些设备的计算能力.数据存储能力都是有限的, 不太可能在Android平台上部署大型企业级应用,因此Android应用可能以纯粹客户端应用的 ...

  2. android 页面停几秒后跳转

      <span style="white-space:pre">      </span>//实现等待几秒后跳转,方法一 new Handler().pos ...

  3. mysql数据库版本引发的问题

    改前: 改后:

  4. Android 启动APP时黑屏白屏的三个解决方案

    你会很奇怪,为什么有些app启动时,会出现一会儿的黑屏或者白屏才进入Activity的界面显示,但是有些app却不会如QQ手机端,的确这里要做处理一下.这里先了解一下为什么会出现这样的现象,其实很简单 ...

  5. Java的线程模型

    并发不一定要依赖多线程(如PHP中很常见的多进程并发),但是在Java里面谈论并发,大多数都与线程脱不开关系. 线程是比进程更轻量级的调度执行单位,线程的引入,可以把一个进程的资源分配和执行调度分开, ...

  6. 递推DP URAL 1225 Flags

    题目传送门 /* 1 r; 2 b; 3 w 2不能在最前面,所以dp[1] = 2; dp[2] = 2: 13 or 31 dp[i] = dp[i-1] + dp[i-2]; 只加1或3时,总数 ...

  7. C# 中的可变参数方法(VarArgs)

    首先需要明确一点:这里提到的可变参数方法,指的是具有 CallingConventions.VarArgs 调用约定的方法,而不是包含 params 参数的方法.可以通过MethodBase.Call ...

  8. C# 新技巧(一)

    概述:所有代码均来自MVC源码的阅读.实际上,也是框架开发中常用的技巧. 1.使用Empty模式处理空对象 return Enumerable.Empty<ModelValidationResu ...

  9. BZOJ1767 : [Ceoi2009]harbingers

    设d[i]表示i到1的距离 f[i]=w[i]+min(f[j]+(d[i]-d[j])*v[i])=w[i]+d[i]*v[i]+min(-d[j]*v[i]+f[j]) 对这棵树进行点分治,每次递 ...

  10. 一份spring配置文件及其详解

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/axu20/archive/2009/10/14/4668188.aspx 1.基本配置:<?xml versio ...