Пример #1
0
 /// <summary>
 /// Writes csv content to a stream
 /// </summary>
 /// <param name="csvFile">CsvFile</param>
 /// <param name="stream">Stream</param>
 /// <param name="encoding">Encoding</param>
 public void WriteCsv(ElencyCsvFile csvFile, Stream stream, Encoding encoding)
 {
     stream.Position = 0;
     _streamWriter   = new StreamWriter(stream, encoding ?? Encoding.Default);
     WriteToStream(csvFile, _streamWriter);
     _streamWriter.Flush();
     stream.Position = 0;
 }
Пример #2
0
        /// <summary>
        /// Writes the Csv File
        /// </summary>
        /// <param name="csvFile">CsvFile</param>
        /// <param name="writer">TextWriter</param>
        private void WriteToStream(ElencyCsvFile csvFile, TextWriter writer)
        {
            if (csvFile.Headers.Count > 0)
            {
                WriteRecord(csvFile.Headers, writer);
            }

            csvFile.Records.ForEach(record => WriteRecord(record.Fields, writer));
        }
Пример #3
0
        /// <summary>
        /// Writes csv content to a file
        /// </summary>
        /// <param name="csvFile">CsvFile</param>
        /// <param name="filePath">File path</param>
        /// <param name="encoding">Encoding</param>
        public void WriteCsv(ElencyCsvFile csvFile, string filePath, Encoding encoding)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            using (StreamWriter writer = new StreamWriter(filePath, false, encoding ?? Encoding.Default))
            {
                WriteToStream(csvFile, writer);
                writer.Flush();
                writer.Close();
            }
        }
Пример #4
0
        /// <summary>
        /// Writes csv content to a string
        /// </summary>
        /// <param name="csvFile">CsvFile</param>
        /// <param name="encoding">Encoding</param>
        /// <returns>Csv content in a string</returns>
        public string WriteCsv(ElencyCsvFile csvFile, Encoding encoding)
        {
            string content = string.Empty;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(memoryStream, encoding ?? Encoding.Default))
                {
                    WriteToStream(csvFile, writer);
                    writer.Flush();
                    memoryStream.Position = 0;

                    using (StreamReader reader = new StreamReader(memoryStream, encoding ?? Encoding.Default))
                    {
                        content = reader.ReadToEnd();
                        writer.Close();
                        reader.Close();
                        memoryStream.Close();
                    }
                }
            }

            return(content);
        }
Пример #5
0
 /// <summary>
 /// Writes csv content to a file
 /// </summary>
 /// <param name="csvFile">CsvFile</param>
 /// <param name="filePath">File path</param>
 public void WriteCsv(ElencyCsvFile csvFile, string filePath)
 {
     WriteCsv(csvFile, filePath, null);
 }
Пример #6
0
 /// <summary>
 /// Writes csv content to a stream
 /// </summary>
 /// <param name="csvFile">CsvFile</param>
 /// <param name="stream">Stream</param>
 public void WriteCsv(ElencyCsvFile csvFile, Stream stream)
 {
     WriteCsv(csvFile, stream, null);
 }