Click here to Skip to main content
15,891,837 members
Articles / Desktop Programming / Windows Forms

Excel Generator with Column Designer

Rate me:
Please Sign up or sign in to vote.
4.56/5 (27 votes)
8 Oct 2009CPOL2 min read 62.7K   2.2K   114  
A fully customizable and extensible C# library that makes it easy to generate Excel files for a given DataSet, with column layout design support.
/// FileName                : SpreadsheetColumn.cs
/// Author                  : Somnath Mondal
/// Created Date            : 10/03/2007
/// 
/// Modification History    :
/// **********************************************************************************************
/// Date        Author                          Description
/// **********************************************************************************************
/// 10/03/2007  Somnath Mondal.                 Created
/// **********************************************************************************************
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Somsoft.ReportDesigner
{
	#region Enums
	public enum SpreadsheetColumnType
	{
		String,
		Number,
		Date,
		DateTime,
		Boolean
	}
	#endregion
	/// <summary>
	///Specifies the appearance and text formatting and behavior of a Spreadsheet column.
	/// </summary>
	[DefaultPropertyAttribute("Name")]
    
    public class SpreadsheetColumn 
	{
		#region constant
		private const string INVALID_PROPERTY_VALUE = "Invalid property value";
		private const string PROPERTY_TITLE = "Designer Properties Window";
		#endregion

		#region Private Variables

		/// <summary>
		/// SpreadsheetColumnCollection used for this class to obtain references to its parent class
		/// </summary>
		private SpreadsheetColumnCollection  container;

		/// <summary>
		/// The name used to map the column style to a data member. The default value is String.Empty.
		/// </summary>
		private string mappingName=string.Empty;

		/// <summary>
		/// The text displayed in the header section of the column. The default value is String.Empty.
		/// </summary>
		private string headerText=string.Empty;

		/// <summary>
		/// Specifies width of the column. The default value is 10.
		/// </summary>
		private int _Width =10;

		/// <summary>
		/// A string displayed in a column containing a DBNull.Value. Default value is String.Empth.
		/// </summary>
		private string nullText = string.Empty;

		/// <summary>
		/// A string displayed in a column containing a True value. Default value is "Y".
		/// </summary>
		private string trueValue = "Y";

		/// <summary>
		/// A string displayed in a column containing a False value. Default value is "N".
		/// </summary>
		private string falseValue = "N";

		/// <summary>
		/// Name of the column.
		/// </summary>
		private string name = string.Empty;

		/// <summary>
		/// Specifies determines whether a column can be seen.
		/// </summary>
		private bool visible = true;

		/// <summary>
		/// Specifies the data type for this column. Default value is String.
		/// </summary>
		private SpreadsheetColumnType dataType = SpreadsheetColumnType.String;

		//public delegate void ValueChangedHandler (string propertyName, object value);
		/// <summary>
		/// This event is thrown after the property value has changed.
		/// </summary>
		public event EventHandler< ChangeEventArgs > OnValueChanged;

		//public delegate void ValueValidatedHandler (string propertyName, object value, ref bool cancel);
		/// <summary>
		/// This event is thrown before the property value has changed.
		/// </summary>
		public event EventHandler<ChangeEventArgs> OnValueChanging;

		//public delegate void ErrorHandler (string propertyName, object value, string message);
		/// <summary>
		/// This event is thrown when any error occurs while setting property value.
		/// </summary>
		public event EventHandler<ErrorEventArgs> OnError;
		#endregion

		#region Public Properties
		/// <summary>
		/// Gets or sets boolean value that represent Name of the column
		/// </summary>
		/// <remarks>
		/// Name requires valid string and this should be unique in Column collection
		/// </remarks>
		[DescriptionAttribute("Indicates the name used by designer to identify the column")]
		public string Name
		{
			get
			{
				return name;
			}
			set
			{
				if(value.Length==0)
				{
					if(OnError!=null)
					{
                        ErrorEventArgs arg = new ErrorEventArgs();
                        arg.PropertyName = "Name";
                        arg.PropertyValue = value;
                        arg.Message = INVALID_PROPERTY_VALUE;
						OnError(this, arg); 
						return;
					}
					else
					{
						MessageBox.Show(INVALID_PROPERTY_VALUE, PROPERTY_TITLE,MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1); 
						return;
					}
				}
				if(this.Container !=null &&  this.Container.IsDuplicateKey(value))
				{
					if(OnError!=null)
					{
                        ErrorEventArgs arg = new ErrorEventArgs();
                        arg.PropertyName = "Name";
                        arg.PropertyValue = value;
                        arg.Message = INVALID_PROPERTY_VALUE;
                        OnError(this, arg);
						return;
					}
					else
					{
						MessageBox.Show(INVALID_PROPERTY_VALUE, PROPERTY_TITLE,MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1); 
						return;
					}
				}
                //bool hasCancel = false;
				if(OnValueChanging!=null)
				{
                    ChangeEventArgs arg = new ChangeEventArgs();
                    arg.PropertyName = "Name";
                    arg.PropertyValue = value;
                    //arg.Message = INVALID_PROPERTY_VALUE;
                    //OnValueChanging("Name", value, ref hasCancel);
                    OnValueChanging(this, arg);
					if(arg.Cancel)
					{
						return;
					}
				}
				name = value;
                if (OnValueChanged != null)
                {
                    ChangeEventArgs arg = new ChangeEventArgs();
                    arg.PropertyName = "Name";
                    arg.PropertyValue = value;
                    //arg.Message = INVALID_PROPERTY_VALUE;
                    //OnValueChanging("Name", value, ref hasCancel);
                    OnValueChanged(this, arg);
                    //OnValueChanged("Name", value);
                }
			}
		}
		/// <summary>
		/// Gets or sets boolean value that represent column visibility
		/// </summary>
		[DescriptionAttribute("Determines whether the column is visible or hidden.")]
		public bool Visible
		{
			get
			{
				return visible;
			}
			set
			{
				visible = value;
			}
		}
		/// <summary>
		/// Gets or sets the SpreadsheetColumnType that represent the data type of the column
		/// </summary>
		[DescriptionAttribute("Determines datatype used for displaying column value.")]
		public SpreadsheetColumnType DataType
		{
			get
			{
				return dataType;
			}
			set
			{
				dataType = value;
			}
		}
		/// <summary>
		/// Gets or sets the text that is displayed when the column contains a null reference 
		/// </summary>
		[DescriptionAttribute("Text that is displayed when the column contains a null reference.")]
		public string NullText
		{
			get
			{
				return nullText;
			}
			set
			{
				nullText = value;
			}
		}
		/// <summary>
		/// Gets or sets the text that is displayed when the column contains a True value 
		/// </summary>
		[DescriptionAttribute("Text that is displayed when the column contains a True value.")]
		public string TrueValue
		{
			get
			{
				return trueValue;
			}
			set
			{
				trueValue = value;
			}
		}
		/// <summary>
		/// Gets or sets the text that is displayed when the column contains a False value 
		/// </summary>
		[DescriptionAttribute("Text that is displayed when the column contains a False value.")]
		public string FalseValue
		{
			get
			{
				return falseValue;
			}
			set
			{
				falseValue = value;
			}
		}
		/// <summary>
		/// Gets or sets the text that used to map the column style to a data member. The default value is String.Empty.
		/// </summary>
		[Browsable(true)]
		[TypeConverter(typeof(MappingNameConverter))]
		[DescriptionAttribute("Indicates the property to display the column in report.")]
		public string  MappingName
		{
			get
			{
				string fieldName = "";
				if (mappingName != null)
				{
                    fieldName = mappingName;
				}
				else
				{
					if (GlobalFields.ListOfFields.Length > 0)
					{
						//Sort the list before displaying it
						Array.Sort(GlobalFields.ListOfFields);

                        fieldName = GlobalFields.ListOfFields[0];
					}
				}

                return fieldName;
			}
			set
			{
				mappingName = value;
			}
		}
		/// <summary>
		/// Gets or sets the text that displayed in the header section of the column. The default value is String.Empty.
		/// </summary>
		[DescriptionAttribute("Indicates the property that displayed in the header section of the column.")]
		public string  HeaderText
		{
			get
			{
				return headerText;
			}
			set
			{
				headerText = value;
			}
		}
		/// <summary>
		/// Gets or sets the value that represent the column width.
		/// </summary>
		[DescriptionAttribute("The width of column in point.")]
		public int Width
		{
			get
			{
				return _Width;

			}
			set
			{
				_Width = value;
			}
		}
		/// <summary>
		/// Gets or sets the value that used for this class to obtain references to its parent class
		/// </summary>
		[Browsable(false)]
		internal SpreadsheetColumnCollection Container
		{
			get
			{
				return container;
			}
			set
			{
				container = value;
			}
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions