用于解析CSV值的脚本

时间:2013-10-24 04:39:35

标签: c# asp.net .net parsing csv

我正在使用此代码来解析值并将它们存储在List中。第一行的名称存储得很好。但是在存储值时,只会保存第二行。我不确定我需要做什么编辑,以便它也解析所有其他行。

请参阅下面的图片和代码。

enter image description here

List<string> names = new List<string>();                   // List to store Key names
List<string> values = new List<string>();                  // List to store key values

using (StreamReader stream = new StreamReader(filePath))
{
    names = stream.ReadLine().Split(',').ToList();         // Seperate key names and store them in a List
    values = stream.ReadLine().Split(',').ToList();        // Seperate key values and store them in a list
}

2 个答案:

答案 0 :(得分:3)

看看这样的事情是否更好:

    // List to store Key names
    List<string> names = new List<string>();                                                 
    // List to store key values
    List<List<string>> values = new List<string>();                                               

    using (StreamReader stream = new StreamReader(filePath))
    {
        if(!stream.EndOfStream)
        {
            // Seperate key names and store them in a List
            names = stream.ReadLine().Split(',').ToList();
        }                     
        while(!stream.EndOfStream)
        {
            // Seperate key values and store them in a list
            values.Add(stream.ReadLine().Split(',').ToList());                 
        }
    }

这会将您的值列表更改为字符串列表的列表,以便每行都有一个字符串列表

虽然这可能不是解析.csv的最佳方法,但如果您的数据一致并且文件格式非常一致,那么您可以这样做。一旦你尝试使用奇数值,带引号的字符串,带逗号的字符串等,你就需要一种不同的方法。

答案 1 :(得分:0)

我已经编写了网格视图的代码,将其更改为列表。我认为它会有所帮助

protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string s = FileUpload1.FileName.Trim();
            if (s.EndsWith(".csv"))
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/data/" + s));
                string[] readText = File.ReadAllLines(Server.MapPath("~/data/" + s));
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
               // Array.Sort(readText);

                for (int i = 0; i < readText.Length; i++)
                {
                    if (i == 0)
                    {
                        string str = readText[0];
                        string[] header = str.Split(',');

                        dt.TableName = "sal";
                        foreach (string k in header)
                        {
                            dt.Columns.Add(k);
                        }
                    }
                    else
                    {
                        DataRow dr = dt.NewRow();

                        string str1 = readText[i];
                        if (readText[i] == ",,,,")
                        {
                            break;
                        }


                        string[] rows = str1.Split(',');
                        if (dt.Columns.Count == rows.Length)
                        {

                            for (int z = 0; z < rows.Length; z++)
                            {
                                if (rows[z] == "")
                                {
                                    rows[z] = null;

                                }

                                dr[z] = rows[z];
                            }
                            dt.Rows.Add(dr);


                        }
                        else
                        {
                            Label1.Text = "please select valid format";
                        }
                    }


                }

                //Iterate through the columns of the datatable to set the data bound field dynamically.
                ds.Merge(dt);
                Session["tasktable"] = dt;
                foreach (DataColumn col in dt.Columns)
                {
                    BoundField bf = new BoundField();

                    bf.DataField = col.ToString();
                    bf.HeaderText = col.ColumnName;
                    if (col.ToString() == "Task")
                    {
                        bf.SortExpression = col.ToString();
                    }
                    GridView1.Columns.Add(bf);

                }
                GridView1.DataSource = ds;
                GridView1.DataBind();


            }
            else
            {
                Label1.Text = "please select a only csv format";
            }

        }
        else
        {
            Label1.Text = "please select a file";
        }
        }