csv文件只读取第一行

本文关键字:一行 文件 读取 csv | 更新日期: 2023-09-27 18:26:23

我正试图通过csv上传一系列客户端信息,我在开始时遇到了一些问题,但我之前的帖子得到了回复,所以我可以开始读取数据,但它只读取第一行。只是想知道有没有人有什么想法。我已经在下面包含了代码

    private void btnUpload_Click(object sender, EventArgs e)
    {
         //Browse for file
        OpenFileDialog ofd = new OpenFileDialog();
        //Only show .csv files
        ofd.Filter = "Microsoft Office Excel Comma Separated Values File|*.csv";
        DialogResult result = ofd.ShowDialog();
        //If the user selects a valid file 
        if (result == DialogResult.OK)
        {
            //File is delimited by a comma
            char[] laClientDelim = { ',' };
            //New object for string manipulation
            objStringManipulation = new StringManipulation();
            // Parse the csv file
            List<string[]> lsClientList = objStringManipulation.parseCSV(ofd.FileName, laClientDelim);
            foreach (string[] laClient in lsClientList)
            {
                //Create new object for manipulating the database
                objSqlCommands = new SqlCommands("Client", "ClientName");

                string[] records = File.ReadAllLines(ofd.FileName); // read the file completely line by line
                char splitChar = ',';
                int splitCharCount = 0;
                int k = 0;

                    string[] fields = records[k].Split(splitChar);  // reads all the single values per line. 'splitChar' should be the delimiter.
                    splitCharCount++;
                    if (splitCharCount >= 4 && splitCharCount <= 10)
                    {
                        var stuff = from l in File.ReadLines(ofd.FileName)
                                    let x = l.Split(new[] { ',', ' ' },  StringSplitOptions.RemoveEmptyEntries)
                                              .Skip(1)
                                             .Select(s => char.Parse(s))
                                    select new
                                    {
                                        Client = x,
                                        ClientName = x
                                    };
                    }

                    //Inserts the client info into datbase
                objSqlCommands.sqlCommandInsertorUpdate("INSERT", records[k]);//laClient[0]);
                k++;
                    //Refreshs the Client table on display from the 
                    this.clientTableAdapter.Fill(this.kIIDImplementationCalcDataSet.Client);
                    //MAKE SURE TO ONLY ADD IN CLIENT AND CLIENT NAME 
                    //update the view 
                    dgvClientlst.Update() ; 


            }
        }
    }

csv文件只读取第一行

您的循环基本上是这样的:

foreach (string[] laClient in lsClientList)
{
   int k = 0;
   string[] records = File.ReadAllLines(ofd.FileName);
   string[] fields = records[k].Split(splitChar);
   k++;
}

对于每个laClient,您的"k"值永远不会超过0。您需要对每一行进行内部循环。

我知道我的答案并不完全是你想要的,但如果你将.csv文件转换为.xls文件,那么你可以很容易地操作它。我已经多次这样做了,如果你愿意,我可以为你提供代码说明。

扩展Jonesy的答案(因为我还不能发表评论),在循环外声明变量k。每次都会重置为零。

int k = 0;
foreach (string[] laClient in lsClientList)
{
    string[] records = File.ReadAllLines(ofd.FileName);
    string[] fields = records[k].Split(splitChar);
    k++;
}