2013-08-02 2 views
0

CSV를 통해 일련의 고객 정보를 업로드하려고합니다.이 문제는 eginning에서 다소 문제가 있었지만 이전 게시물에 대한 답변을 얻었으므로 데이터를 읽을 수있었습니다. 첫 번째 행만 읽습니다. 누군가가 아이디어를 가지고 있는지 궁금해.csv 파일의 첫 번째 줄만 읽기

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

    string[] fields = records[k].Split(splitChar); 
    k++; 
} 

귀하의 'K'값은 결코 각 laClient 0 과거를하지 않습니다 : 당신의 루프이 본질적으로 같다

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() ; 





      } 

     } 
    } 
+0

변수의 범위를 이해한다고 생각합니다. 변수 K는 for 루프 내에서 정의되므로 각 루프에서 0으로 반복해서 인스턴스화됩니다. 루프 밖에서 K를 선언하십시오. – Guanxi

답변

1

아래의 코드를 포함 시켰습니다. 각 줄마다 내부적으로 루프해야합니다.

+0

* facepalm * 똑똑한 덕분에, 고쳐 줘서 고마워요. – user2546071

0

내 대답은 정확히 당신이 찾고있는 것이 아니지만 .csv 파일을 .xls 파일로 변환하면 매우 쉽게 조작 할 수 있습니다. 여러 번이 작업을 수행했으며 원하는 경우 코드를 사용하여 지침을 제공 할 수 있습니다.

0

Jonesy의 대답을 확장하면 (아직 주석을 달 수 없기 때문에) 변수 k을 루프 외부에 선언하십시오. 매번 0으로 재설정됩니다.

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

    string[] fields = records[k].Split(splitChar); 
    k++; 
} 
+0

K를 루프 바깥에 놓으면 첫 번째 라인 이상을 얻는 문제가 해결됩니다.하지만 이제는 작동하지 않는 선택 문제에 직면 해 있습니다. 지정된 하위 문자열 대신 모든 데이터 – user2546071

+0

Duh. foreach 블록 내에서 레코드와 행을 반복해야합니다. 제가 시연했듯이 결코 멍청한 실수를 저 지르지는 않습니다. –

관련 문제