Writing a CSV to ftp
本文关键字:ftp to CSV Writing | 更新日期: 2023-09-27 18:29:33
我有一个将数据编译在一起的Web服务,我想将其存储在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保存正确,但我没有得到它读取的单独列
"你好,这是一个测试"
"喂,这是第二个测试"
而不是
"你好,这是","一个测试"
"你好,是一个","第二个测试"
关于如何用逗号分隔列,有什么想法吗?
我已经更改了您的代码以满足您的要求:
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 = @""";""";