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

原文地址:http://www.cnblogs.com/macroxu-1982/archive/2006/12/26/573856.html

本文的运行环境  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

 

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

 

 

2.0  生成表格

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

 

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

   Tableframework.JPG

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>

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

     /// </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   总结  

posted @ 2009-09-16 16:43  mysun  阅读(539)  评论(0编辑  收藏  举报