0

我有一个将数据编译在一起的网络服务,我想将其存储在 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 保存正确,但我没有得到它读取的单独列

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

而不是

"你好这是","一个测试"
"你好这是一个","第二个测试"

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

4

1 回答 1

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 = @""";""";
于 2013-08-30T08:46:40.090 回答