Posts
265
Comments
80
Trackbacks
4
Dynamische Farbübergabe an Aspose-Cell-Styles

Hier ein Beispiel wie man mittels eines Dictionarys dynamisch Hintergrundfarben an vordefinierte Styles übergeben kann:

class ReportStyles {

   public Styles _StyleSheet;        // 
   public Dictionary<StyleKey, Style> _Styles = new Dictionary<StyleKey, Style>();
   private const string _Font = "Arial";
   private const int _FontZize = 10;

   // search in the dictionary for a defined style
   public Style GetStyle( ReportContentTypes Type, Color Color ) {
      Style style;
      Styles styles = _StyleSheet;

      if( !_Styles.TryGetValue( new StyleKey( Type, Color ), out style ) ) {

         switch( Type ) {

            case ReportContentTypes.Headline:
               // Style for the Headlines
               style = styles[styles.Add()];
               style.Font.Name                = _Font;
               style.Font.Size                = _FontZize;
               style.IsTextWrapped            = true;
               style.Font.IsBold              = true;
               style.VerticalAlignment        = TextAlignmentType.Center;
               style.HorizontalAlignment      = TextAlignmentType.Center;
               break;

            case ReportContentTypes.Text:
               // Style for Text-Content
               style = styles[styles.Add()];
               style.Font.Name                = _Font;
               style.Font.Size                = _FontZize;
               style.IsTextWrapped            = true;
               style.Font.IsBold              = false;
               style.VerticalAlignment        = TextAlignmentType.Top;
               style.HorizontalAlignment      = TextAlignmentType.Left;
               break;

            case ReportContentTypes.TextRight:
               // Style for Text-Content
               style = styles[styles.Add()];
               style.Font.Name                = _Font;
               style.Font.Size                = _FontZize;
               style.IsTextWrapped            = true;
               style.Font.IsBold              = false;
               style.VerticalAlignment        = TextAlignmentType.Top;
               style.HorizontalAlignment      = TextAlignmentType.Right;
               break;
         }

         // Set 
         style.Font.Name       = _Font;
         style.Font.Size       = _FontZize;

         style.ForegroundColor = Color;
         style.Pattern         = Color == Color.Transparent ? BackgroundType.None : BackgroundType.Solid;

         // Add new Style to the Dictionary
         _Styles[new StyleKey( Type, Color )] = style;
      }

      return style;
   }

}


Mit folgendem Aufruf kann diesen vordefinierten Styles beliebige, d.h. in Excel darstellbare Farben, übergeben:

Style styleHeadline    = reportStyles.GetStyle( ReportContentTypes.Headline,
                Color.Transparent );

Eine übersicht der in Excel darstellbaren Farben findet man hier:
http://blogs.dotnetgerman.com/christian/PermaLink,guid,8b899020-c57b-470e-90de-6a5d4ba8d9ad.aspx

posted on Wednesday, June 20, 2007 6:42 AM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 6 and 4 and type the answer here: