Thursday, 15 November 2012

Convert DataSet to CSV


Convert DataSet to CSV



public void CreateCSVfile(DataTable dtable, string strFilePath)
        {
          
            StreamWriter sw = new StreamWriter(strFilePath, false);
            int icolcount = dtable.Columns.Count;
            for (int i = 0; i < icolcount; i++)
            {
                sw.Write(dtable.Columns[i]);
                if (i < icolcount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
            foreach (DataRow drow in dtable.Rows)
            {
                for (int i = 0; i < icolcount; i++)
                {
                    if (!Convert.IsDBNull(drow[i]))
                    {
                        sw.Write(drow[i].ToString());
                    }
                    if (i < icolcount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
            }
            sw.Close();
        }
 
 
Alternatively Refer:
 
http://forums.asp.net/t/1500560.aspx/1?How+To+Convert+A+DataSet+To+CSV  

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top