2013-08-30 47 views
0

我有一個web服務將數據編譯在一起,我想將它存儲在一個CSV文件中,這樣用戶就可以得到一個下載鏈接來獲取它。這是我所寫一個CSV到FTP

  string fileName = "Test.csv"; 
      string ftpAddress = "MYDOMAIN.COM"; 
      string username = "MYUSERNAME"; 
      string password = "MYPASSWORD"; 

      List<string[]> done = new List<string[]>(); 
      string[] firstline = new string[2]; 

      firstline[0] = "hello this is "; 
      firstline[1] = "a test"; 
      done.Add(firstline); 
      string[] secondline = new string[2]; 

      secondline[0] = "hello this is a "; 
      secondline[1] = "second test"; 
      done.Add(secondline); 
      string delimiter = ","; 
      StringBuilder sb = new StringBuilder(); 


      for (int index = 0; index < 2; index++) 
       sb.AppendLine(string.Join(delimiter, done[index])); 

      string str = sb.ToString(); 
      byte[] buffer = new byte[str.Length * sizeof(char)]; 
      System.Buffer.BlockCopy(str.ToCharArray(), 0, buffer, 0, buffer.Length); 



       WebRequest request = WebRequest.Create("ftp://" + ftpAddress + fileName); 
       request.ContentType = 

       request.Method = WebRequestMethods.Ftp.UploadFile; 
       request.Credentials = new NetworkCredential(username, password); 
       Stream reqStream = request.GetRequestStream(); 
       reqStream.Write(buffer, 0, buffer.Length); 


       reqStream.Close(); 

的CSV保存正確的,但我沒有得到單獨的列讀取

「您好,這是一個測試」
「您好,這是一個,第二個測試」

而不是

「您好,這是」,「測試」
「您好,這是一個」,「第二次測試」

如何得到任何想法逗號分隔列?

+0

那麼在你的代碼中,你期待它把引號放進去嗎? –

+0

我不想要報價,但第一行全部在單元格a1中,而不是在a1和a2之間分割。我解釋它的壞方法 –

+0

專注於你想要*在文件*中。畢竟這只是一個文本文件。一旦確定了文本文件需要在所使用的內容中正確打開的內容,然後使代碼編寫適當的文本文件非常簡單。目前還不清楚爲什麼您認爲FTP部分是相關的。 –

回答

0

我已經改變了你的代碼,以滿足您的要求:

 string fileName = "Test.csv"; 
     string ftpAddress = "MYDOMAIN.COM"; 
     string username = "MYUSERNAME"; 
     string password = "MYPASSWORD"; 


     List<string[]> done = new List<string[]>(); 
     string[] firstline = new string[2]; 

     firstline[0] = "hello this is "; 
     firstline[1] = "a test"; 
     done.Add(firstline); 
     string[] secondline = new string[2]; 

     secondline[0] = "hello this is a "; 
     secondline[1] = "second test"; 
     done.Add(secondline); 
     string delimiter = @""","""; 
     StringBuilder sb = new StringBuilder(); 

     foreach (var lineArray in done)   
      sb.AppendLine(string.Format(@"""{0}""", string.Join(delimiter, lineArray))); 

     string str = sb.ToString(); 
     byte[] buffer = new byte[str.Length * sizeof(char)]; 
     System.Buffer.BlockCopy(str.ToCharArray(), 0, buffer, 0, buffer.Length); 

     WebRequest request = WebRequest.Create("ftp://" + ftpAddress + fileName); 
     request.ContentType = 

     request.Method = WebRequestMethods.Ftp.UploadFile; 
     request.Credentials = new NetworkCredential(username, password); 
     Stream reqStream = request.GetRequestStream(); 
     reqStream.Write(buffer, 0, buffer.Length); 

     reqStream.Close(); 

變量str現在包含:

「您好,這是」, 「測試」

「您好,這是一個「,」第二個測試「

如果您使用半角分號而不是逗號,excel正確顯示文件。

string delimiter = @""";""";