FarPoint动态加载


在设计时先将样式保存为***.xml格式,这样可以在代码中动态加载FarPoint。
cs代码:
private string strPath = Application.StartupPath + @"\AuditXml\";
  //表头(hs指的是在设计FarPoint时自己定义的SheetItem)
  private FarPoint.Win.Spread.NamedStyle fphStyle = new FarPoint.Win.Spread.NamedStyle("hs");
  //标题居中对齐(tcs指的是在设计FarPoint时自己定义的SheetItem)
  private FarPoint.Win.Spread.NamedStyle fptcStyle = new FarPoint.Win.Spread.NamedStyle("tcs");
  //标题居左对齐(tls指的是在设计FarPoint时自己定义的SheetItem)
  private FarPoint.Win.Spread.NamedStyle fptlStyle = new FarPoint.Win.Spread.NamedStyle("tls");
  //单元格样式(ds指的是在设计FarPoint时自己定义的SheetItem)
  private FarPoint.Win.Spread.NamedStyle fpdStyle = new FarPoint.Win.Spread.NamedStyle("ds");

public AuditingContent()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //

   SetHeadStyle( fphStyle );
   SetTitleCStyle( fptcStyle );
   SetTitleLStyle( fptlStyle );
   SetDataStyle( fpdStyle );
  }

#region 设置报表、单元格的样式
  /// <summary>
  /// 设置表头的样式
  /// </summary>
  /// <param name="ns"></param>
  private void SetHeadStyle( FarPoint.Win.Spread.NamedStyle ns )
  {
   FarPoint.Win.Spread.CellType.TextCellType cellType = new FarPoint.Win.Spread.CellType.TextCellType();
   cellType.WordWrap = true;
   ns.CellType = cellType;

   ns.BackColor = Color.LavenderBlush;
   ns.Locked  = true;
   ns.Font = new Font( "黑体", 15 );
   ns.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
   ns.VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
  }

  /// <summary>
  /// 设置居中标题的样式
  /// </summary>
  /// <param name="ns"></param>
  private void SetTitleCStyle( FarPoint.Win.Spread.NamedStyle ns )
  {
   FarPoint.Win.Spread.CellType.TextCellType cellType = new FarPoint.Win.Spread.CellType.TextCellType();
   cellType.WordWrap = true;
   ns.CellType = cellType;

   ns.BackColor = Color.LavenderBlush;
   ns.Locked  = true;
   ns.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
   ns.VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
  }

  /// <summary>
  /// 设置居左标题的样式
  /// </summary>
  /// <param name="ns"></param>
  private void SetTitleLStyle( FarPoint.Win.Spread.NamedStyle ns )
  {
   FarPoint.Win.Spread.CellType.TextCellType cellType = new FarPoint.Win.Spread.CellType.TextCellType();
   cellType.WordWrap = true;
   ns.CellType = cellType;

   ns.BackColor = Color.LavenderBlush;
   ns.Locked  = true;
   ns.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Left;
   ns.VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
  }

  /// <summary>
  /// 设置数据单元格的样式
  /// </summary>
  /// <param name="ns"></param>
  private void SetDataStyle( FarPoint.Win.Spread.NamedStyle ns )
  {
   FarPoint.Win.Spread.CellType.TextCellType cellType = new FarPoint.Win.Spread.CellType.TextCellType();
   cellType.WordWrap = true;
   ns.CellType = cellType;

   ns.Locked  = false;
   ns.HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Right;
   ns.VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
  }

  private void SetSheetStyle( FarPoint.Win.Spread.SheetView sv )
  {
   sv.DefaultStyle.Border = new FarPoint.Win.BevelBorder( FarPoint.Win.BevelBorderType.Raised, Color.WhiteSmoke, Color.DarkGray, 1, true, true, true, true );
   sv.GrayAreaBackColor = Color.Lavender;
  }

  /// <summary>
  /// 显示报表
  /// </summary>
  /// <param name="path">XML文件名称</param>
  /// <param name="fps">报表显示控件</param>
  private void Show_Report(  string XmlName, FarPoint.Win.Spread.FpSpread fps )
  {
   if( !File.Exists( strPath + XmlName ) )
   {
    MessageBox.Show("要加载的报表Xml文件样式不存在,加载报表失败!") ;
    return ;
   }
   try
   {  
    fps.Open( strPath + XmlName );      
  
    fps.NamedStyles.Add( fphStyle );
    fps.NamedStyles.Add( fptcStyle );
    fps.NamedStyles.Add( fptlStyle );
    fps.NamedStyles.Add( fpdStyle );

    SetSheetStyle( fps.ActiveSheet );

    fps.ActiveSheet.GrayAreaBackColor = Color.Lavender;
   }
   catch
   {
    MessageBox.Show("加载报表样式失败!") ;
   }
  }
  #endregion

posted on 2005-11-01 09:13  冷月孤峰  阅读(983)  评论(0)    收藏  举报