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
?
Range和Selection兩個對象都是一個范圍對象,并提供了好多同樣的處理范圍的方法和屬性,在這里編程中我還是更多的使用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生成表格报表篇)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯:2023年第一季度微信月活13.1
- 下一篇: 新年好!