Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Windows Forms
  4. Convert SQL DataTable to Excel/CSV

Convert SQL DataTable to Excel/CSV

Scheduled Pinned Locked Moved Windows Forms
csharpdatabasehelp
8 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nt_virus
    wrote on last edited by
    #1

    Hello. I want to convert Tables of SQL Database to an Excel and CSV file through C#.NET Windows Application.. I tried reading few articles on internet.. and I m puzzled by seeing such codes and cant find a way to make it work.. Can anyone of you guys please help me in this.. I really need it .. Waiting for your replies...

    L L N 3 Replies Last reply
    0
    • N nt_virus

      Hello. I want to convert Tables of SQL Database to an Excel and CSV file through C#.NET Windows Application.. I tried reading few articles on internet.. and I m puzzled by seeing such codes and cant find a way to make it work.. Can anyone of you guys please help me in this.. I really need it .. Waiting for your replies...

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      nt_virus wrote:

      Can anyone of you guys please help me in this

      Help you with what? You didn't ask a question you just asked for help? Are you asking for someone to do the work for you? That's not really how this site works. There are some that do work that way like Rentacoder.com

      1 Reply Last reply
      0
      • N nt_virus

        Hello. I want to convert Tables of SQL Database to an Excel and CSV file through C#.NET Windows Application.. I tried reading few articles on internet.. and I m puzzled by seeing such codes and cant find a way to make it work.. Can anyone of you guys please help me in this.. I really need it .. Waiting for your replies...

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        nt_virus wrote:

        and I m puzzled by seeing such codes and cant find a way to make it work..

        We can help if you get stuck, or provide a link to an article that sets you off in the right direction.

        nt_virus wrote:

        I really need it ..

        Within what timespan? Writing it yourself is an option, but if you don't know the language then it will take some effort and time. Start out with writing a small prototype that reads data from a SQL Server table. Exporting the data back to Excel goes somewhat along the same route. If you get stuck with a particular piece o' code, or want explanation about something that you're puzzled about, check back here. Copy & paste the code into a new post, without the "please" and "I need it", and you'll get some helpfull answers. Could be done in a couple o' days :)

        I are troll :)

        1 Reply Last reply
        0
        • N nt_virus

          Hello. I want to convert Tables of SQL Database to an Excel and CSV file through C#.NET Windows Application.. I tried reading few articles on internet.. and I m puzzled by seeing such codes and cant find a way to make it work.. Can anyone of you guys please help me in this.. I really need it .. Waiting for your replies...

          N Offline
          N Offline
          nt_virus
          wrote on last edited by
          #4

          Hello. Thanks for the replies. The below code works, it is made on Console Application.. but how to get the column names .. I m just getting only data..

          namespace SqldataTable_to_Excel
          {
          class Program
          {
          static void Main(string[] args)
          {
          string path = @"C:\db.xls";
          if (!File.Exists(path))
          {
          // Create a file to write to.
          using (StreamWriter sw = File.CreateText(path))
          {
          //creating the file contents
          SqlConnection cn = new SqlConnection("Data Source=NT_VIRUS;Initial Catalog=test;Integrated Security=True");
          SqlCommand cmd = new SqlCommand("SELECT * FROM planets", cn);
          try
          {
          cn.Open();
          SqlDataReader dr = cmd.ExecuteReader();
          while (dr.Read())
          {
          sw.WriteLine(dr["name"].ToString() + "\t" + dr["age"].ToString());
          }

                              Console.WriteLine("Database has been exported.");
                          }
                          catch (Exception excpt)
                          {
                              Console.WriteLine(excpt.Message);
                          }
                      }
                  }
              }
          }
          
          L 1 Reply Last reply
          0
          • N nt_virus

            Hello. Thanks for the replies. The below code works, it is made on Console Application.. but how to get the column names .. I m just getting only data..

            namespace SqldataTable_to_Excel
            {
            class Program
            {
            static void Main(string[] args)
            {
            string path = @"C:\db.xls";
            if (!File.Exists(path))
            {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
            //creating the file contents
            SqlConnection cn = new SqlConnection("Data Source=NT_VIRUS;Initial Catalog=test;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("SELECT * FROM planets", cn);
            try
            {
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
            sw.WriteLine(dr["name"].ToString() + "\t" + dr["age"].ToString());
            }

                                Console.WriteLine("Database has been exported.");
                            }
                            catch (Exception excpt)
                            {
                                Console.WriteLine(excpt.Message);
                            }
                        }
                    }
                }
            }
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Good job :cool: There is more than one way to get the column-names. You could read the data into a DataTable, instead of using the dataReader. A DataTable also holds some information on the metadata of the table, such as columnnames. You could also ask SQL Server to give you the same metadata. Execute the SQL-statement below (with the correct tablename) to get a list of columnnames, their types and length :)

            SELECT sysobjects.name AS TableName,
            syscolumns.name AS ColumnName,
            datatype=systypes.name,
            length=syscolumns.length
            FROM sysobjects
            JOIN syscolumns ON sysobjects.id = syscolumns.id
            JOIN systypes ON syscolumns.xtype=systypes.xtype
            WHERE sysobjects.name = 'AfvinkCode'

            I are troll :)

            N 1 Reply Last reply
            0
            • L Lost User

              Good job :cool: There is more than one way to get the column-names. You could read the data into a DataTable, instead of using the dataReader. A DataTable also holds some information on the metadata of the table, such as columnnames. You could also ask SQL Server to give you the same metadata. Execute the SQL-statement below (with the correct tablename) to get a list of columnnames, their types and length :)

              SELECT sysobjects.name AS TableName,
              syscolumns.name AS ColumnName,
              datatype=systypes.name,
              length=syscolumns.length
              FROM sysobjects
              JOIN syscolumns ON sysobjects.id = syscolumns.id
              JOIN systypes ON syscolumns.xtype=systypes.xtype
              WHERE sysobjects.name = 'AfvinkCode'

              I are troll :)

              N Offline
              N Offline
              nt_virus
              wrote on last edited by
              #6

              Thanks but I didn't understand .. how to apply this in my code.. could you please elaborate mate..

              C 1 Reply Last reply
              0
              • N nt_virus

                Thanks but I didn't understand .. how to apply this in my code.. could you please elaborate mate..

                C Offline
                C Offline
                Curtis Schlak
                wrote on last edited by
                #7

                Ok, you really don't seem to know what you're doing. While CodeProject will help you when you get stuck on a problem or want advice, I don't think that anyone will hold your hand to walk you through the creation of your application. That's what books, schools, online posts (though you're confused by them), and mentors are for. Eddy gave you SQL! If you don't know how to invoke it, then you have a lot more learning to do than I (or anyone else, I think) would want to provide to you. First, figure out what you want to do. Then, try to learn something about it. Then, come back here and ask a better question than what distills to "Thanks for the code snippet, can you please write the rest of the code for me, too?"

                "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                L 1 Reply Last reply
                0
                • C Curtis Schlak

                  Ok, you really don't seem to know what you're doing. While CodeProject will help you when you get stuck on a problem or want advice, I don't think that anyone will hold your hand to walk you through the creation of your application. That's what books, schools, online posts (though you're confused by them), and mentors are for. Eddy gave you SQL! If you don't know how to invoke it, then you have a lot more learning to do than I (or anyone else, I think) would want to provide to you. First, figure out what you want to do. Then, try to learn something about it. Then, come back here and ask a better question than what distills to "Thanks for the code snippet, can you please write the rest of the code for me, too?"

                  "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  :)

                  I are troll :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups