Exemplo n.º 1
0
        public static void Write(RowsetBase rowset,
                                 TextWriter wri,
                                 CSVWritingOptions options = null,
                                 FieldFilterFunc filter    = null)
        {
            if (rowset == null)
            {
                return;
            }
            if (options == null)
            {
                options = CSVWritingOptions.Default;
            }

            var defs = getAcceptableDefs(rowset.Schema, options.LoadAllFields, filter);

            if (options.IncludeHeader)
            {
                writeHeader(defs, wri, options);
            }

            foreach (var item in rowset.AsReadonlyIList)
            {
                var doc = item as Doc;
                if (doc == null)
                {
                    continue;
                }

                writeRow(doc, defs, wri, options);
            }
        }
Exemplo n.º 2
0
 public static void WriteToFile(RowsetBase rowset,
                                string fileName,
                                CSVWritingOptions options = null,
                                Encoding encoding         = null,
                                FieldFilterFunc filter    = null)
 {
     using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
         Write(rowset, fs, options, encoding, filter);
 }
Exemplo n.º 3
0
 public static void Write(RowsetBase rowset,
                          Stream stream,
                          CSVWritingOptions options = null,
                          Encoding encoding         = null,
                          FieldFilterFunc filter    = null)
 {
     using (var wri = new StreamWriter(stream, encoding ?? UTF8Encoding.UTF8))
     {
         Write(rowset, wri, options, filter);
     }
 }
Exemplo n.º 4
0
 public static byte[] WriteToBuffer(RowsetBase rowset,
                                    CSVWritingOptions options = null,
                                    Encoding encoding         = null,
                                    FieldFilterFunc filter    = null)
 {
     using (var ms = new MemoryStream())
     {
         Write(rowset, ms, options, encoding, filter);
         return(ms.ToArray());
     }
 }
Exemplo n.º 5
0
 public static void Write(Doc doc,
                          Stream stream,
                          CSVWritingOptions options = null,
                          Encoding encoding         = null,
                          FieldFilterFunc filter    = null)
 {
     using (var wri = new StreamWriter(stream, encoding ?? Encoding.UTF8))
     {
         Write(doc, wri, options, filter);
     }
 }
Exemplo n.º 6
0
        public CSVWritingOptions(CSVWritingOptions other)
        {
            if (other == null)
            {
                return;
            }

            this.FieldDelimiter = other.FieldDelimiter;
            this.NullValue      = other.NullValue;
            this.LoadAllFields  = other.LoadAllFields;
            this.IncludeHeader  = other.IncludeHeader;
        }
Exemplo n.º 7
0
        public static string Write(RowsetBase rowset,
                                   CSVWritingOptions options = null,
                                   FieldFilterFunc filter    = null)
        {
            var sb = new StringBuilder();

            using (var wri = new StringWriter(sb))
            {
                Write(rowset, wri, options, filter);
            }

            return(sb.ToString());
        }
Exemplo n.º 8
0
        private static void writeValue(object value, TextWriter wri, CSVWritingOptions options)
        {
            if (value == null)
            {
                wri.Write(options.NullValue);
                return;
            }

            if (value is string)
            {
                wri.Write(escape((string)value, options.FieldDelimiter));
                return;
            }

            if (value is bool)
            {
                wri.Write(((bool)value) ? "true" : "false");
                return;
            }

            if (value is int || value is long)
            {
                wri.Write(((IConvertible)value).ToString(System.Globalization.CultureInfo.InvariantCulture));
                return;
            }

            if (value is double || value is float || value is decimal)
            {
                wri.Write(escape(((IConvertible)value).ToString(System.Globalization.CultureInfo.InvariantCulture),
                                 options.FieldDelimiter));
                return;
            }

            if (value is DateTime)
            {
                wri.Write(((DateTime)value).ToString(System.Globalization.CultureInfo.InvariantCulture));
                return;
            }

            if (value is TimeSpan)
            {
                var ts = (TimeSpan)value;
                wri.Write(ts.Ticks);
                return;
            }

            wri.Write(escape(value.ToString(), options.FieldDelimiter));
        }
Exemplo n.º 9
0
        public static void Write(Doc doc,
                                 TextWriter wri,
                                 CSVWritingOptions options = null,
                                 FieldFilterFunc filter    = null)
        {
            if (doc == null)
            {
                return;
            }
            if (options == null)
            {
                options = CSVWritingOptions.Default;
            }

            var defs = getAcceptableDefs(doc.Schema, options.LoadAllFields, filter);

            if (options.IncludeHeader)
            {
                writeHeader(defs, wri, options);
            }

            writeRow(doc, defs, wri, options);
        }
Exemplo n.º 10
0
        private static void writeRow(Doc doc, IEnumerable <Schema.FieldDef> defs, TextWriter wri, CSVWritingOptions options)
        {
            var first   = true;
            var newline = false;

            foreach (var def in defs)
            {
                if (!first)
                {
                    wri.Write(options.FieldDelimiter);
                }
                var val = doc.GetFieldValue(def);
                writeValue(val, wri, options);

                first   = false;
                newline = true;
            }

            if (newline)
            {
                wri.WriteLine();
            }
        }
Exemplo n.º 11
0
        private static void writeHeader(IEnumerable <Schema.FieldDef> defs, TextWriter wri, CSVWritingOptions options)
        {
            var first   = true;
            var newline = false;

            foreach (var def in defs)
            {
                if (!first)
                {
                    wri.Write(options.FieldDelimiter);
                }
                wri.Write(def.Name);

                first   = false;
                newline = true;
            }

            if (newline)
            {
                wri.WriteLine();
            }
        }