Click here to Skip to main content
15,893,381 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.8K   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                : SpreadsheetColumnCollection.cs
/// Author                  : Somnath Mondal
/// Created Date            : 10/03/2007
/// 
/// Modification History    :
/// **********************************************************************************************
/// Date        Author                          Description
/// **********************************************************************************************
/// 10/03/2007  Somnath Mondal.                 Created
/// **********************************************************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace Somsoft.ReportDesigner
{
	/// <summary>
	/// Contains the collection of 
	/// </summary>

    public class SpreadsheetColumnCollection : CollectionBase, IEnumerable<SpreadsheetColumn>  
	{

		public SpreadsheetColumnCollection()
		{
		}

		public SpreadsheetColumn this [int index]
		{
			get
			{
				return (SpreadsheetColumn) List[index];
			}
			set
			{
				List[index] = value;
			}
		}
        public int Add(SpreadsheetColumn spreadsheetColumn)
		{
            spreadsheetColumn.Container = this;
            return List.Add(spreadsheetColumn);
		}
        public void AddRange(SpreadsheetColumn[] spreadsheetColumns)
		{
			int i=0;
            while (i <= spreadsheetColumns.Length)
			{
                this.Add(spreadsheetColumns[i]);
				i++;
			}
		}

        public void AddRange(SpreadsheetColumnCollection spreadsheetColumns)
		{
			int i=0;
            while (i < spreadsheetColumns.Count)
			{
                this.Add(spreadsheetColumns[i]);
				i++;
			}
		}

        public bool Contains(SpreadsheetColumn spreadsheetColumn) 
		{
            return List.Contains(spreadsheetColumn);
		}

		public void CopyTo(SpreadsheetColumn[] array, int index)
		{
			List.CopyTo(array, index);
		}

		public int IndexOf(SpreadsheetColumn spreadsheetColumn)
		{
			return List.IndexOf(spreadsheetColumn);
		}
        public void Insert(int index, SpreadsheetColumn spreadsheetColumn)
		{
            List.Insert(index, spreadsheetColumn);
		}
        public void Remove(SpreadsheetColumn spreadsheetColumn)
		{
            List.Remove(spreadsheetColumn);
		}
		public bool IsDuplicateKey(string name)
		{
			int count=0;
			foreach(SpreadsheetColumn col in List)
			{
				if(col.Name.ToUpper() == name.ToUpper())
					count++;
			}
			return count>0;
		}
        // This method will violate the Microsoft Design rule (UsePropertiesWhereAppropriate), 
        // but should not be a property.
        // This method has an observable side effect. 
        // Calling the method twice in succession creates different results.
        public string GetNewId()
		{
			int count=List.Count;
			string keyName = string.Empty ;
			while(true)
			{
				count++;
				keyName = "Column" + count;
				bool found = false;
				foreach(SpreadsheetColumn col in List)
				{
					if(col.Name.ToUpper() == keyName.ToUpper())
					{
						found=true;
						break;
					}
				}
				if(!found)
					break;
			}
			return keyName;
		}


        #region IEnumerable<int> Members

        public new IEnumerator<SpreadsheetColumn> GetEnumerator()
        {
            foreach (SpreadsheetColumn data in InnerList)
            {
                yield return data;
            }
        }
        //public new SpreadsheetColumnEnumerator GetEnumerator()
        //{
        //    return new SpreadsheetColumnEnumerator(this);

        //}

        #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