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

本文的运行环境 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 > the Type of HLTable's Type</param>
         ///<param >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 ></param>
         public void SetFontSize(float Size)
         {
              _Table.Range.Font.Size=Size;
         }
         ///<summary>
         /// set the HLTable's Paragraph 'sFont
         ///</summary>
         ///<param ></param>
         public void SetFontBold(int Bold)
         {
              _Table.Range.Font.Bold =Bold;
         }
    
         ///<summary>
         /// set the Table 's text Aligh and VerticalAlignment
         ///</summary>
         ///<param >Alignment</param>
         ///<param >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 ></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 中是这么实现的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值