13 Mart 2018

C# CSV Yazmak

 
using System;
using System.Data.SqlClient;
using System.IO;
using System.Text;

namespace CsvDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection con = new SqlConnection("bağlantı cümlesi"))
            {
                string commandText = "SELECT TOP 1000 *  FROM KIMLIK";
                using (SqlCommand cmd = new SqlCommand(commandText, con))
                {
                    try
                    {
                        con.Open();
                        using (var reader = cmd.ExecuteReader())
                        {
                            var csv = new CsvUtilities();
                            csv.ExportCsv("test.csv", reader);
                        }
                    }
                    finally
                    {
                        if (con.State != System.Data.ConnectionState.Closed)
                            con.Close();
                    }
                }
            }
        }

        public class CsvUtilities
        {
            public CsvUtilities(string delimiter = ";")
            {
                this.delimiter = delimiter;
            }
            string delimiter;
            private string SetCsvEscapeCharacters(string data)
            {
                if (data.Contains("\""))
                {
                    data = data.Replace("\"", "\"\"");
                }

                if (data.Contains(delimiter))
                {
                    data = String.Format("\"{0}\"", data);
                }

                if (data.Contains(System.Environment.NewLine))
                {
                    data = String.Format("\"{0}\"", data);
                }
                return data;
            }

            public bool ExportCsv(string path, SqlDataReader reader, bool append = true)
            {
                if (reader.HasRows)
                {
                    using (var sw = new StreamWriter(path, append, Encoding.UTF8))
                    {
                        //Sütun adlarını yaz
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            sw.Write(SetCsvEscapeCharacters(reader.GetName(i)));
                            if (i < reader.FieldCount - 1)
                                sw.Write(delimiter);
                        }
                        sw.Write(Environment.NewLine);
                        sw.Flush();

                        //Satırları yaz
                        while (reader.Read())
                        {
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                sw.Write(SetCsvEscapeCharacters(reader.GetValue(i).ToString()));
                                if (i < reader.FieldCount - 1)
                                    sw.Write(delimiter);
                            }
                            sw.Write(Environment.NewLine);
                            sw.Flush();
                        }
                    }
                    return true;
                }
                return false;
            }
        }    
    }
}