예제 #1
0
 public UnicodeCsvClipboardExporter()
 {
     IncludeColumnHeaders = true;
     FormatSettings = new CsvFormatSettings();
     _indentationString = "";
     _baseStream = new ToStringMemoryStream();
 }
예제 #2
0
    public static string FormatCsvData( Type dataType, object dataValue, CsvFormatSettings formatSettings )
    {
      string outputString = null;
      bool checkWhitespace = true;

      if( ( dataValue != null ) && ( !Convert.IsDBNull( dataValue ) ) && ( !( dataValue is Array ) ) )
      {
        if( dataType == null )
          dataType = dataValue.GetType();

        if( dataType == typeof( string ) )
        {
          string textQualifier = formatSettings.TextQualifier.ToString();

          if( textQualifier == "\0" )
          {
            outputString = ( string )dataValue;
          }
          else
          {
            outputString = formatSettings.TextQualifier + ( ( string )dataValue ).Replace( textQualifier, textQualifier + textQualifier ) + formatSettings.TextQualifier;
          }

          checkWhitespace = false;
        }
        else if( dataType == typeof( DateTime ) )
        {
          if( !string.IsNullOrEmpty( formatSettings.DateTimeFormat ) )
          {
            if( formatSettings.Culture == null )
            {
              outputString = ( ( DateTime )dataValue ).ToString( formatSettings.DateTimeFormat, CultureInfo.InvariantCulture );
            }
            else
            {
              outputString = ( ( DateTime )dataValue ).ToString( formatSettings.DateTimeFormat, formatSettings.Culture );
            }
          }
        }
        else if( ( dataType == typeof( double ) ) ||
                 ( dataType == typeof( decimal ) ) ||
                 ( dataType == typeof( float ) ) ||
                 ( dataType == typeof( int ) ) ||
                 ( dataType == typeof( double ) ) ||
                 ( dataType == typeof( decimal ) ) ||
                 ( dataType == typeof( float ) ) ||
                 ( dataType == typeof( short ) ) ||
                 ( dataType == typeof( Single ) ) ||
                 ( dataType == typeof( UInt16 ) ) ||
                 ( dataType == typeof( UInt32 ) ) ||
                 ( dataType == typeof( UInt64 ) ) ||
                 ( dataType == typeof( Int16 ) ) ||
                 ( dataType == typeof( Int64 ) ) )
        {
          string format = formatSettings.NumericFormat;

          if( ( ( dataType == typeof( double ) ) ||
                ( dataType == typeof( decimal ) ) ||
                ( dataType == typeof( float ) ) ) &&
              ( !string.IsNullOrEmpty( formatSettings.FloatingPointFormat ) ) )
            format = formatSettings.FloatingPointFormat;

          if( !string.IsNullOrEmpty( format ) )
          {
            if( formatSettings.Culture == null )
            {
              outputString = string.Format( CultureInfo.InvariantCulture, "{0:" + format + "}", dataValue );
            }
            else
            {
              outputString = string.Format( formatSettings.Culture, "{0:" + format + "}", dataValue );
            }
          }
        }

        if( outputString == null )
        {
          if( formatSettings.Culture == null )
          {
            outputString = string.Format( CultureInfo.InvariantCulture, "{0}", dataValue );
          }
          else
          {
            outputString = string.Format( formatSettings.Culture, "{0}", dataValue );
          }
        }

        // For dates and numbers, as a rule, we never use the TextQualifier. However, the
        // specified format can introduce whitespaces. To better support this case, we add
        // the TextQualifier if needed.
        if( ( checkWhitespace ) && ( formatSettings.TextQualifier != '\0' ) )
        {
          bool useTextQualifier = false;

          // If the output string contains the character used to separate the fields, we
          // don't bother checking for whitespace. TextQualifier will be used.
          if( outputString.IndexOf( formatSettings.Separator ) < 0 )
          {
            for( int i = 0; i < outputString.Length; i++ )
            {
              if( char.IsWhiteSpace( outputString, i ) )
              {
                useTextQualifier = true;
                break;
              }
            }
          }
          else
          {
            useTextQualifier = true;
          }

          if( useTextQualifier )
            outputString = formatSettings.TextQualifier + outputString + formatSettings.TextQualifier;
        }

      }

      return outputString;
    }
예제 #3
0
        public static string FormatCsvData(Type dataType, object dataValue, CsvFormatSettings formatSettings)
        {
            string outputString    = null;
            bool   checkWhitespace = true;

            if ((dataValue != null) && (!Convert.IsDBNull(dataValue)) && (!(dataValue is Array)))
            {
                if (dataType == null)
                {
                    dataType = dataValue.GetType();
                }

                if (dataType == typeof(string))
                {
                    string textQualifier = formatSettings.TextQualifier.ToString();

                    if (textQualifier == "\0")
                    {
                        outputString = ( string )dataValue;
                    }
                    else
                    {
                        outputString = formatSettings.TextQualifier + (( string )dataValue).Replace(textQualifier, textQualifier + textQualifier) + formatSettings.TextQualifier;
                    }

                    checkWhitespace = false;
                }
                else if (dataType == typeof(DateTime))
                {
                    if (!string.IsNullOrEmpty(formatSettings.DateTimeFormat))
                    {
                        if (formatSettings.Culture == null)
                        {
                            outputString = (( DateTime )dataValue).ToString(formatSettings.DateTimeFormat, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            outputString = (( DateTime )dataValue).ToString(formatSettings.DateTimeFormat, formatSettings.Culture);
                        }
                    }
                }
                else if ((dataType == typeof(double)) ||
                         (dataType == typeof(decimal)) ||
                         (dataType == typeof(float)) ||
                         (dataType == typeof(int)) ||
                         (dataType == typeof(double)) ||
                         (dataType == typeof(decimal)) ||
                         (dataType == typeof(float)) ||
                         (dataType == typeof(short)) ||
                         (dataType == typeof(Single)) ||
                         (dataType == typeof(UInt16)) ||
                         (dataType == typeof(UInt32)) ||
                         (dataType == typeof(UInt64)) ||
                         (dataType == typeof(Int16)) ||
                         (dataType == typeof(Int64)))
                {
                    string format = formatSettings.NumericFormat;

                    if (((dataType == typeof(double)) ||
                         (dataType == typeof(decimal)) ||
                         (dataType == typeof(float))) &&
                        (!string.IsNullOrEmpty(formatSettings.FloatingPointFormat)))
                    {
                        format = formatSettings.FloatingPointFormat;
                    }

                    if (!string.IsNullOrEmpty(format))
                    {
                        if (formatSettings.Culture == null)
                        {
                            outputString = string.Format(CultureInfo.InvariantCulture, "{0:" + format + "}", dataValue);
                        }
                        else
                        {
                            outputString = string.Format(formatSettings.Culture, "{0:" + format + "}", dataValue);
                        }
                    }
                }

                if (outputString == null)
                {
                    if (formatSettings.Culture == null)
                    {
                        outputString = string.Format(CultureInfo.InvariantCulture, "{0}", dataValue);
                    }
                    else
                    {
                        outputString = string.Format(formatSettings.Culture, "{0}", dataValue);
                    }
                }

                // For dates and numbers, as a rule, we never use the TextQualifier. However, the
                // specified format can introduce whitespaces. To better support this case, we add
                // the TextQualifier if needed.
                if ((checkWhitespace) && (formatSettings.TextQualifier != '\0'))
                {
                    bool useTextQualifier = false;

                    // If the output string contains the character used to separate the fields, we
                    // don't bother checking for whitespace. TextQualifier will be used.
                    if (outputString.IndexOf(formatSettings.Separator) < 0)
                    {
                        for (int i = 0; i < outputString.Length; i++)
                        {
                            if (char.IsWhiteSpace(outputString, i))
                            {
                                useTextQualifier = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        useTextQualifier = true;
                    }

                    if (useTextQualifier)
                    {
                        outputString = formatSettings.TextQualifier + outputString + formatSettings.TextQualifier;
                    }
                }
            }

            return(outputString);
        }