Esempio n. 1
0
        public virtual void Write(IGetSetterCollection data, Services.Csv.WriteCsvOption option)
        {
            using (var writer = new CsvWriter(File.CreateText(this.DocumentFile), new CsvHelper.Configuration.Configuration {
                Delimiter = option.Delimiter
            }))
            {
                if (!option.IgnoreHeader && data.Header != null)
                {
                    foreach (var item in data.Header)
                    {
                        writer.WriteField(item);
                    }
                }
                writer.NextRecord();

                foreach (var row in data)
                {
                    foreach (var item in row)
                    {
                        var val = item;

                        //Apply NumericFormat before write
                        if (option.NumericFormat.IsNotNullOrEmpty() && (item.IsNotNumericType() || item.IsNumericType()))
                        {
                            val = string.Format(option.NumericFormat, item);
                        }

                        //Apply DateFormat before write
                        if (option.DateFormat.IsNotNullOrEmpty() && (item is DateTime || item is DateTimeOffset))
                        {
                            val = string.Format(option.DateFormat, item);
                        }

                        writer.WriteField(val);
                    }

                    writer.NextRecord();
                }
            }
        }
Esempio n. 2
0
 public override void Write(IGetSetterCollection data, bool ignoreHeader = false)
 => Write(data, new Services.Csv.WriteCsvOption {
     IgnoreHeader = ignoreHeader
 });