|
/// 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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.