Thursday, April 25, 2013

Fastest Way to convert String to Csv


private DataTable StrTOCSV(string content)
        {
            DataTable tbl = new DataTable();
            if (content.Length > 0)
            {
                string[] rows = content.Split('\n' );
                if (rows.Length > 0)
                {
                    //Create Columns in Datatable.
                    string[] cols = rows[0].Split(new [] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    if (cols.Length > 0)
                    {
                        foreach (var item in cols)
                        {
                            tbl.Columns.Add(item);
                        }
                    }
                    //Add rows in datatable
                    for (int i = 1; i < rows.Length; i++)
                    {
                        string[] coldata = rows[i].Split(new [] { "," }, StringSplitOptions.RemoveEmptyEntries);

                        DataRow dr = tbl.NewRow();

                        for (int j = 0; j < cols.Length; j++)
                        {
                            if (coldata.Length > 0)
                            {
                                if (j <= coldata.Length)
                                {
                                    dr[j] = coldata[j];
                                }
                            }
                        }
                        tbl.Rows.Add(dr);

                    }

                }

            }
            return tbl;
        }

15 comments:

  1. Ankur Bhargav'S C Bolg >>>>> Download Now

    >>>>> Download Full

    Ankur Bhargav'S C Bolg >>>>> Download LINK

    >>>>> Download Now

    Ankur Bhargav'S C Bolg >>>>> Download Full

    >>>>> Download LINK 9T

    ReplyDelete