本文的运行环境  dot FrameWork 1.1 ,Office 2003

开发环境  Vss2003 C#

前言

在 Excel中生成多个动态表格的报表是非常复杂的,因为在生成每一行数据的时候,我们都要考虑每一列由哪几个单元格组合而成。因为多个表格之间是关联的,遇到增加和删除表格的列的时候,整个报表的生成就要重新的调整。可扩展性不强。在遇到这样的报表的时候,我们可以通过Word来生成。在Word中表格与表格之间是没有关联的

本文是利用Word.Dll的动态的生成多个表格的报表。

目录

1.0    两个重要的对象Range 和Selection

2.0    生成表格

2.1    涉及到的表格的对象及相关的功能,逻辑关系。

2.2 将表格的对象的常用的功能进行重新封装。

2.3 生成表格

3.0 在.net中调用宏说明

4.0 总结

1.0两个重要的对象Range Selection

   Range 和Selection 提供了一个操作范围,并提供了对这个范围的操作包含字体样式,段落的对齐方式,边框显示的方式等等。在Word编程中大部分情况下我们都要接触和使用到这两个对象。

下面详细说明这两个对象的定义和一些常用的属性和方法。

Range

定义

该对象代表文档中的一个连续范围。每一个 Range对象由一起始和一终止字符位置定义。我们经常先定义Range ,然后对Range中的内容进行操作。

常用的属性

Font                ------- 字符格式, Range中的文字格式属性。

Bold                ------- 字体或范围的格式设置为加粗格式

Borders             ------- 指定对象的所有边框

ParagraphFormat      -------指定区域、所选范围、查找与替换操作或样式中的段落设置

常用的方法

InsertAfter(Text)

将指定文本插入某一区域或选定内容的后面。

Select 方法

选定指定的对象。

Selection

该对象代表窗口或窗格中的当前所选内容。所选内容代表文档中被选定(或突出显示的)的区域,若文档中没有所选内容,则代表插入点。每个文档窗格只能有一个活动的 Selection对象,并且整个应用程序中只能有一个活动的 Selection对象。

Selection

Font                ------- 字符格式, Range中的文字格式属性。

Bold                ------- 字体或范围的格式设置为加粗格式

Borders             ------- 指定对象的所有边框

ParagraphFormat      -------指定区域、所选范围、查找与替换操作或样式中的段落设置

常用的方法

InsertAfter(Text)

将指定文本插入某一区域或选定内容的后面。

MoveRight 方法

expression.MoveRight(Unit, Count, Extend)

将所选内容向右移动,并返回移动距离的单位数。

Unit       WdUnits,可选。所选内容的移动单位。

Count      所选内容移动距离的单位数。

Extend     可以是 wdMove 或 wdExtend。如果为 wdMove,则所选内容折叠到结束位置,并向右移动。如果为 wdExtend,则所选内容向右扩展。默认值是 wdMove

RangeSelection两个对象都是一个范围对象,并提供了好多同样的处理范围的方法和属性,在这里编程中我还是更多的使用Range来生成报表中的样式。

2.0  生成表格

在Word中生成表格,本质上就是在Document中生成Table对象,并对Table添加内容和样式。下面首先介绍跟生成表格有关的几个对象。

2.1涉及到的表格的对象及相关的功能,逻辑关系。

  

Table     该对象代表一个单独的表格。

Columns  由 Column 对象所组成的集合,该集合中的对象代表表格中的列。

Rows     由 Row 对象所组成的集合,该集合中的对象代表指定的选定部分、区域或表格中的表格行。

Column   代表单个表格列

Row      代表表格的一行。

Cell       代表单个表格单元格。

2.2 将表格的对象的常用的功能进行重新封装。

对于Office中的对象,我的处理方式是,把这些对象和常用的功能封装其来,生成C#对象

这样的话 我们直接通过对封装后生成的对象进行操作,来生成需要的Word表格。这样 一 可以并于理解和调用  二可以快速的开发

下面是封装的对象和内容

命名空间 WordReport.HLWord

HLTable   接口  定义了表格的接口   (注 HL 是我公司的名称,并于和已经定义的Table区别开来)

HLTableClass 实际的类继承了HLTable 接口

下面是定义的代码

namespace WordReport.HLWord

{

///<summary>

/// HLTableClass is  the Class  that Contained the Functions of operate The Word's Table 's Style such as paragraph ,font ,height ,width ,et

///</summary>

public  class HLTableClass:HLTable

{

private Word.Table _Table=null;

private HLRows _HLRows=null;

public HLTableClass(Word.Table CurTable) //初始化是参数为需要操作的表格Word.Table

{

_Table=CurTable;

_HLRows=new HLRowsClass(_Table.Rows);

}

//表格的列对象集合下面介绍

public HLRows HlRows

{

get{return _HLRows ;}

}

#region HLTable 成员

///<summary>

/// 获取本对象的操作的Word中的表

///</summary>

///<returns></returns>

public Word.Table BaseTable()

{

return _Table;

}

///<summary>

/// Set The HLTable 's LineSpace

///</summary>

///<param name="LineSpaceType"> the Type of HLTable's Type</param>

///<param name="Size">The HLTable LineSpacing </param>

public void SetLineSpace(Word.WdLineSpacing LineSpaceType,float Size)

{

_Table.Range.ParagraphFormat.LineSpacingRule=LineSpaceType;

_Table.Range.ParagraphFormat.LineSpacing =Size;

}

///<summary>

/// set the HLTable's Paragraph 'sFont

///</summary>

///<param name="Size"></param>

public void SetFontSize(float Size)

{

_Table.Range.Font.Size=Size;

}

///<summary>

/// set the HLTable's Paragraph 'sFont

///</summary>

///<param name="Size"></param>

public void SetFontBold(int Bold)

{

_Table.Range.Font.Bold =Bold;

}

///<summary>

/// set the Table 's text Aligh and VerticalAlignment

///</summary>

///<param name="Alignment">Alignment</param>

///<param name="VerticalAlignment">VerticalAlignment</param>

public void SetPositionAlign(Word.WdParagraphAlignment Alignment, Word.WdCellVerticalAlignment VerticalAlignment)

{

_Table.Range.Cells.VerticalAlignment=VerticalAlignment;

_Table.Range.ParagraphFormat.Alignment=Alignment;

}

///<summary>

/// set the table 'sBorderStyle

///</summary>

///<param name="LineStyle"></param>

public void SetBorderStyle(Word.WdLineStyle LineStyle)

{

_Table.Borders[Word.WdBorderType.wdBorderTop].LineStyle=LineStyle;

_Table.Borders[Word.WdBorderType.wdBorderLeft].LineStyle=LineStyle;

_Table.Borders[Word.WdBorderType.wdBorderRight].LineStyle=LineStyle;

_Table.Borders[Word.WdBorderType.wdBorderBottom].LineStyle=LineStyle;

_Table.Borders[Word.WdBorderType.wdBorderHorizontal].LineStyle=LineStyle;

_Table.Borders[Word.WdBorderType.wdBorderVertical].LineStyle=LineStyle;

}

//设置第 ColumnIndex 列的宽度

public void ColumnWidth(int ColumnIndex, Word.WdPreferredWidthType WidthType, float Values)

{

_Table.Columns[ColumnIndex].PreferredWidthType=WidthType;

_Table.Columns[ColumnIndex].PreferredWidth=Values;

}

//设置第 RowIndex 行的行高

public void RowHeight(int RowIndex, Word.WdRowHeightRule HeightRule, float Values)

{

_Table.Rows[RowIndex].HeightRule=HeightRule;

_Table.Rows[RowIndex].Height=Values;

}

//设置表的所有行的行高

public void RowHeight( Word.WdRowHeightRule HeightRule, float Values)

{

_Table.Rows.HeightRule=HeightRule;

_Table.Rows.Height=Values;

}

// 给行为RowIndex 列为ColmnIndex的单元格赋值 Values

public void CellText(int RowIndex,int ColmnIndex,  string Values)

{

_Table.Cell(RowIndex,ColmnIndex).Range.InsertAfter(Values);

}

//合并单元格  从第RowIndex行 ,第ColumnIndex列的单元格开始,合并Length个单元格

public void MergeCell(int RowIndex,int ColumnIndex, int Lenght)

{

for(int index=1;index<=Lenght;index++)

{

_Table.Cell(RowIndex,ColumnIndex).Merge(_Table.Cell(RowIndex,ColumnIndex+1));

}

}

//取单元格对象HLCell 后面有介绍

public HLCell GetCell( int RowIndex,int ColumnIndex)

{

Word.Cell CurCell =_Table.Cell(RowIndex,ColumnIndex);

HLCell CurHLCell=new HLCellClass(CurCell);

return CurHLCell;

}

//给表格加载默认的样式 居中对齐 ,字体的设置 等等

public void LoadDefaultStyle()

{

//

SetBorderStyle(Word.WdLineStyle.wdLineStyleSingle);

SetFontSize(10F);

SetPositionAlign(Word.WdParagraphAlignment.wdAlignParagraphCenter,Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter);

SetLineSpace(Word.WdLineSpacing.wdLineSpaceExactly ,10F);

}

#endregion

}

///<summary>

/// the interface of HLTable ,change the Word Table 's VBA Code to the C# Code

///</summary>

public interface HLTable

{

Word.Table BaseTable();

//base Table Style

void SetLineSpace(Word.WdLineSpacing LineSpaceType,float Size);

void SetFontSize(float Size);

void SetFontBold(int Bold);

void SetPositionAlign(Word.WdParagraphAlignment Alignment,Word.WdCellVerticalAlignment VerticalAlignment);

void SetBorderStyle(Word.WdLineStyle LineStyle);

void ColumnWidth(int ColumnIndex,Word.WdPreferredWidthType WidthType,float Values);

void RowHeight(int RowIndex,Word.WdRowHeightRule HeightRule,float Values);

void RowHeight(Word.WdRowHeightRule HeightRule,float Values);

//Set CellValues into table's cell

void CellText(int RowIndex,int ColmnIndex ,string Values);

//

void MergeCell(int RowIndex,int ColumnIndex,int Lenght);

HLCell GetCell(int RowIndex  ,int ColumnIndex);

HLRows HlRows{get;}

// show default Style of table

void LoadDefaultStyle();

}

}

这个HLTable的对象主要的功能是将Word.Table 的功能封装起来,我们可以直接调用HLTable来实现对Word.Table表格的操作,而不管具体在Word中是这么实现的

下面是HLCell对象 它的功能就像是Excel中对单元格的操作

代码

namespace WordReport.HLWord

{

///<summary>

///表格对象 ,封装了Word。Cell的部分主要的功能

///</summary>

public class HLCellClass:HLCell

{

private Word.Cell  _Cell=null;

public HLCellClass(Word.Cell CurCell) //初始化是 赋值需要操作的Word.Cell对象

{

_Cell=CurCell;

}

#region HLCell 成员

public Word.Cell BaseCell()

{

return _Cell;

}

public void SetLineSpace(Word.WdLineSpacing LineSpaceType,float Size)

{

_Cell.Range.ParagraphFormat.LineSpacingRule=LineSpaceType;

_Cell.Range.ParagraphFormat.LineSpacing=Size;

}

public void SetFontSize(float Size)

{

_Cell.Range.Font.Size =Size;

}

///<param name="Size"></param>

public void SetFontBold(int Bold)

{

_Cell.Range.Font.Bold =Bold;

}

public void SetPositionAlign(Word.WdParagraphAlignment Alignment, Word.WdCellVerticalAlignment VerticalAlignment)

{

_Cell.VerticalAlignment=VerticalAlignment;

_Cell.Range.ParagraphFormat.Alignment =Alignment;

}

public void SetBorderStyle(Word.WdLineStyle LineStyle,Word.WdLineWidth lineWidth)

{

_Cell.Borders[Word.WdBorderType.wdBorderLeft].LineStyle=LineStyle;

_Cell.Borders[Word.WdBorderType.wdBorderRight].LineStyle=LineStyle;

_Cell.Borders[Word.WdBorderType.wdBorderTop].LineStyle=LineStyle;

_Cell.Borders[Word.WdBorderType.wdBorderBottom].LineStyle=LineStyle;

_Cell.Borders[Word.WdBorderType.wdBorderLeft].LineWidth=lineWidth;

_Cell.Borders[Word.WdBorderType.wdBorderRight].LineWidth=lineWidth;

_Cell.Borders[Word.WdBorderType.wdBorderTop].LineWidth=lineWidth;

_Cell.Borders[Word.WdBorderType.wdBorderBottom].LineWidth=lineWidth;

}

public void values(string Values)

{

_Cell.Range.InsertAfter(Values);

}

#endregion

}

///<summary>

/// the interface of HLCell ,change the Word Cell 's VBA Code to the C# Code

///</summary>

public interface HLCell

{

Word.Cell  BaseCell();

//base Table Style

void SetLineSpace(Word.WdLineSpacing LineSpaceType,float Size);

void SetFontSize(float Size);

void SetFontBold(int Bold);

void SetPositionAlign(Word.WdParagraphAlignment Alignment,Word.WdCellVerticalAlignment VerticalAlignment);

void SetBorderStyle(Word.WdLineStyle LineStyle,Word.WdLineWidth lineWidth);

//Set CellValues into table's cell

void values(string Values);

}

}

下面还有 行数组对象 HLRows 和行对象HLRow ,具体的功能就不细说拉下面是代码部分。

///<summary>

/// HLRows 的摘要说明。

///</summary>

public class HLRowsClass:HLRows

{

private Word.Rows _Rows=null;

public HLRowsClass(Word.Rows CurRow)

{

_Rows=CurRow;

}

#region HLRows 成员

public HLRow this[int i]

{

get

{

return  new HLRowClass(_Rows[i]);

}

}

#endregion

}

public interface HLRows

{

HLRow this[int i]{get;}

}

///<summary>

/// HLRow 的摘要说明。

///</summary>

public class HLRowClass:HLRow

{

private Word.Row _Row =null;

public HLRowClass(Word.Row CurRow)

{

_Row=CurRow;

}

#region HLRow 成员

public void SetRowHeight(Word.WdRowHeightRule HeightRule, float Values)

{

_Row.HeightRule =HeightRule;

_Row.Height=Values;

}

public void SetLineSpace(Word.WdLineSpacing LineSpaceType, float Size)

{

_Row.Range.ParagraphFormat.LineSpacingRule=LineSpaceType;

_Row.Range.ParagraphFormat.LineSpacing=Size;

}

public void SetFontSize(float Size)

{

_Row.Range.Font.Size=Size;

}

public void SetFontBold(int Bold)

{

_Row.Range.Font.Bold =Bold;

}

#endregion

}

public interface HLRow

{

void SetRowHeight( Word.WdRowHeightRule HeightRule, float Values);

void SetLineSpace(Word.WdLineSpacing LineSpaceType,float Size);

void SetFontSize(float Size);

void SetFontBold(int Bold);

}

2.3 生成表格

我用了个TableBuilder 类的一个方法来负责生成表格

public static HLTable CreateHLTable(DataTable DataTable_Source ,Word.Document CurDoc,Word.Range CurRange)

CurRange    生成表格的位置

CurDoc      生成表格所在的文档

DataTable_Source 表格的数据源

需要生成的样式如下:

表名

列名1

列名2

列名3

列名4

列名5

数据一

   数据一

    数据一

数据一

数据一

数据二

数据二

数据二

数据二

数据二

数据三

数据三

数据三

数据三

数据三

 

 

TableBuilder的代码:

///<summary>

/// The Builder which Create the Table by DataTable

///</summary>

public  abstract class  TableBuilder

{

private static object missing =System.Reflection.Missing.Value;

public  TableBuilder()

{

}

///<summary>

/// Create the HLTable by the parameters DataTable_Source,CurDoc,CurRange

///</summary>

///<param name="DataSource"></param>

///<param name="CurDoc"></param>

///<param name="CurRange"></param>

///<returns></returns>

public static HLTable CreateHLTable(DataTable DataTable_Source ,Word.Document CurDoc,Word.Range CurRange)

{

int ColumnsNum =DataTable_Source.Columns.Count ;

int RowsNum =DataTable_Source.Rows.Count +2;

if(DataTable_Source.Rows.Count ==0)

RowsNum=3;

HLTable  TableNew=new HLTableClass(CurDoc.Tables.Add(CurRange,RowsNum,ColumnsNum,ref missing,ref missing));

// define the style of Created Table

TableNew.LoadDefaultStyle();

// the table Title Show

TableNew.HlRows[1].SetRowHeight(Word.WdRowHeightRule.wdRowHeightExactly ,20F);

TableNew.MergeCell(1,1,ColumnsNum-1);

TableNew.GetCell(1,1).SetFontBold(1);

TableNew.CellText(1,1,DataTable_Source.TableName);

//the ColumnName Show

for(int index=1;index<=ColumnsNum;index++)

{

TableNew.CellText(2,index,DataTable_Source.Columns[index-1].ColumnName);

}

TableNew.HlRows[2].SetRowHeight(Word.WdRowHeightRule.wdRowHeightExactly ,20F);

// show the data

if (DataTable_Source.Rows.Count >0)

{

for(int Rowindex=1;Rowindex<=DataTable_Source.Rows.Count;Rowindex++)

{

for(int Columnindex=1;Columnindex<=ColumnsNum;Columnindex++)

{         TableNew.CellText(Rowindex+2,Columnindex,DataTable_Source.Rows[Rowindex-1][Columnindex-1].ToString());     TableNew.HlRows[Rowindex+2].SetRowHeight(Word.WdRowHeightRule.wdRowHeightExactly ,20F);

}

}

}

else

{

TableNew.HlRows[3].SetRowHeight(Word.WdRowHeightRule.wdRowHeightExactly ,20F);

}

return TableNew;

}

}

3.0   在.net中调用宏说明

在实际我们录制的宏中的方法和C#提供的接口是不一样的 如下的例子:

Selection.MoveRight Unit:=wdCharacter, Count:=1

这是在宏中的Selection 向右移一位的方法

而在C#中提供的方法是这样的

Selection.MoveRight(ref object,ref object,ref object);

怎样在C# 中调用上面的宏的方法呢

下面就是在C#的实际调用的方法

object Start=Type.Missing ;

object End =Type.Missing ;

Start=Word.WdUnits.wdCharacter ;

End=1;

Doc.ActiveWindow.Selection.MoveRight(ref Start,ref End,ref missing);

 

4.0   总结  

转载于:https://www.cnblogs.com/macroxu-1982/archive/2006/12/26/573856.html

Office编程在dot Net环境中总结(Word生成表格报表篇)相关推荐

  1. 关于安装Office之后,右键新建菜单中没有Word、PPT、Excel选项

    文章目录 前言 一.个人感觉比较好用的方法: 二.该问题产生的原因 总结 前言   今天刚重装完系统,发现安装Office之后,右键新建菜单中没有Word.PPT.Excel选项.百度了下,相关问题的 ...

  2. 【转】Java多线程编程(十)-并发编程原理(分布式环境中并发问题)

    转载地址:http://blog.csdn.net/leicool_518/article/details/42268947 在分布式环境中,处理并发问题就没办法通过操作系统和JVM的工具来解决,那么 ...

  3. 安装好office套件以后,右键新建中没有Word、Excel、PPT等怎么办

    在新安装好office套件以后,如果右击鼠标新建过程中没有出现例如word.excel.ppt等格式的文件,那么可以尝试重启电脑试一下,如果不行的话,可以尝试修复一下 具体操作流程如下 第一步:打开控 ...

  4. apache 隐藏php版本,PHP+Apache环境中怎么隐藏Apache版本

    PHP+Apache环境中怎么隐藏Apache版本 发布时间:2021-02-08 09:57:43 来源:亿速云 阅读:104 作者:小新 小编给大家分享一下PHP+Apache环境中怎么隐藏Apa ...

  5. 强化学习系列文章(二十七):VPG+Beta分布在CartPoleContinuous环境中的应用

    强化学习系列文章(二十七):VPG+Beta分布在CartPoleContinuous环境中的应用 在第七篇笔记(https://blog.csdn.net/hhy_csdn/article/deta ...

  6. cad高程如何提取到cass软件_从CAD平面图中提取坐标生成数据表

    功能:在CAD中提取点的三维坐标(提取当前坐标系中坐标)直接生成Excel表,并在CAD图中生成坐标数据表,加了标点号和输出的选项.   如何安装: 1,在CAD平面图中→工具→宏→加载工程→加载下载 ...

  7. 计算机新建里没有word,电脑新建中没有word和excel怎么回事

    很多朋友在安装好Office之后,发现右键新建中没有Word.Excel.PowerPoint等选项,这个时候该怎么办呢?这里为大家分享一种简单的解决方法. 本次操作以Dell电脑为例,具体操作步骤如 ...

  8. Visual C# .Net 环境中编程实现浮动工具栏

    Visual C# .Net 环境中编程实现浮动工具栏 郭胜涛 mailtogst@163.com 原帖地址:http://blog.csdn.net/mailtogst/archive/2007/0 ...

  9. jar java classpath_win7中java编程工具安装 java环境变量设置

    win7中java编程工具安装 java环境变量设置 Question:编译是显示'javac'不是内部或外部命令,也不是可运行的程序或批处理文件 解决: 在[系统变量]里编辑java_home.cl ...

最新文章

  1. GNU源码安装借用YUM排除故障
  2. .NET Core 跨平台 串口通讯 ,Windows/Linux 串口通讯
  3. PHP正则表达式提取超链接及其标题
  4. Nginx反向代理服务器获取不到端口的问题的解决办法
  5. 【laravel5.6】 laravel 接口 接管 自定义异常类
  6. BigDecimal的add方法
  7. 初中理化生实验室仪器配置
  8. 世界500强面试题----反应能力
  9. Python处理 JSON 数据
  10. log4j 日志书写格式_Log4J日志配置详解
  11. 迅捷pdf转换器完美解决如何将pdf转换成word问题
  12. ghost win7旗舰版系统安装图文教程
  13. react-native 使用高德SDK取得位置信息
  14. No exports main defined
  15. Java航班预订统计leetcode_1109
  16. 没娱乐、没性生活,中关村程序员要被逼疯了
  17. OpenAI 人工智能绘图工具 DALLE 好用吗?
  18. 常识——windows的tensorflow安装gpu版本,cuda算力只有3.0也可以
  19. 简书地址http://www.jianshu.com/users/9fb081407820/latest_articles
  20. 数独游戏(Sudoku Game)

热门文章

  1. nginx的状态是failed的解决方案
  2. ubuntu18.10运行95版仙剑
  3. ubuntu18.10下面从视频中提取音频
  4. eclipse关联本地maven仓库和配置
  5. jsp调用controller方法_SpringMVC五大核心组件及调用过程
  6. TypeScript 发布 3.4 首个 RC 预览版
  7. iOS 11.4.1 正式版越狱
  8. 老男孩教育50期左婷婷-day03-xhell连接服务器-远程连接排错-基础命令
  9. 世界最小晶体管问世 栅极长度仅一纳米
  10. VMM2012应用指南之1-实验环境概述与准备