将CSV写入ftp

时间:2013-08-30 08:09:53

标签: c# web-services csv ftp

我有一个将数据编译在一起的网络服务,我想将其存储在CSV中,以便为用户提供下载链接以获取它。这就是我所拥有的

            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保存正确但我没有得到它读取的单独列

“你好,这是一个测试”,“你好,这是第二次测试”

而不是

“你好,这是”,“测试”
“你好,这是一个”,“第二次测试”

关于如何让逗号分隔列的任何想法?

1 个答案:

答案 0 :(得分: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现在包含:

  

“你好,这是”,“测试”

     

“你好这是一个”,“第二次测试”

如果您使用semikolon而不是逗号excel正确显示文件。

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