Click here to Skip to main content
15,886,788 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.6K   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                : CustomConvert.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.Generic;
using System.Text;
using System.Reflection;

namespace Somsoft.ReportDesigner
{
    internal static class CustomConvert
    {
        /// <summary>
        /// Convert any string to a specic enum value.
        /// </summary>
        public static object ToEnum(Type type, string inString)
        {
            foreach (FieldInfo fi in type.GetFields())
                if (fi.Name == inString)
                    return fi.GetValue(null);
            // We use null because
            // enumeration values
            // are static
            throw new Exception(string.Format("Can't convert {0} to{1}", inString, type.ToString()));
        }
        /// <summary>
        /// Convert the input value into required boolean format
        /// </summary>
        public static string ToBoolean(object data, SpreadsheetColumn mapColumn)
        {
            string tag = string.Empty;
            if (Convert.IsDBNull(data))
            {
                tag = mapColumn.NullText;
                return tag;
            }

            if (data.ToString().Length == 0)
            {
                tag = mapColumn.NullText;
                return tag;
            }
            bool result = false;
            try
            {
                result = Convert.ToBoolean(data);
            }
            catch
            { // do nothing
            }

            tag = result ? mapColumn.TrueValue : mapColumn.FalseValue;

            return tag;
        }
        /// <summary>
        /// Convert the input value into required date format
        /// </summary>
        public static string ToDate(object data, SpreadsheetColumn mapColumn, string dateSeperator, OutputType outputType)
        {
            string tag = string.Empty;
            if (Convert.IsDBNull(data))
            {
                tag = mapColumn.NullText;
                return tag;
            }

            if (data.ToString().Length == 0)
            {
                tag = mapColumn.NullText;
                return tag;
            }
            try
            {
                DateTime dt = DateTime.Parse(data.ToString());
                string month = dt.Month.ToString();
                string day = dt.Day.ToString();
                if (day.Length == 1)
                    day = "0" + day;
                if (month.Length == 1)
                    month = "0" + month;

                switch (outputType)
                {
                    case OutputType.Html:
                        tag = day + dateSeperator + month + dateSeperator + dt.Year;
                        break;
                    case OutputType.Excel:
                        tag = dt.Year + dateSeperator + month + dateSeperator + day;
                        break;
                }
            }
            catch
            { // do nothing
            }

            return tag;
        }
        /// <summary>
        /// Convert the input value into required DateTime format
        /// </summary>
        public static string ToDateTime(object data, SpreadsheetColumn mapColumn, string dateSeperator)
        {
            string tag = string.Empty;
            if (Convert.IsDBNull(data))
            {
                tag = mapColumn.NullText;
                return tag;
            }

            if (data.ToString().Length == 0)
            {
                tag = mapColumn.NullText;
                return tag;
            }
            try
            {
                string dateTimeString = data.ToString();
                DateTime dt = DateTime.Parse(dateTimeString);
                string month = dt.Month.ToString();
                string day = dt.Day.ToString();
                if (day.Length == 1)
                    day = "0" + day;
                if (month.Length == 1)
                    month = "0" + month;

                tag = day + dateSeperator + month + dateSeperator + dt.Year + " " + dt.Hour + "h" + dt.Minute;
            }
            catch
            { // do nothing
            }

            return tag;
        }
        /// <summary>
        /// Convert the Html string into Excel string
        /// </summary>
        public static string ToExcelText(string htmlText)
        {

            // Just to replace TR with Row
            htmlText = htmlText.Replace("<tr>", "<Row ss:AutoFitHeight=\"1\" >\n");
            htmlText = htmlText.Replace("</tr>", "</Row>\n");

            //replace the cell tags
            htmlText = htmlText.Replace("<td>", "<Cell><Data ss:Type=\"String\">");
            htmlText = htmlText.Replace("</td>", "</Data></Cell>\n");

            return htmlText;
        }

    }
}

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