문제

Help me please write every value to different cell, I try to write data in the CSV file but when I open this file in Excel the value showing in one line (cell). Did different various separators between the values "." and ",". But the results in Office 2010 and 2012 the same... And one more interesting think, in Office 2007 that work's with (*.xls)...

string s = "";
s += "Nick-Name, Category, Web-Pages, ICQ, Skype, Mail, Phone\r";
for (int i = 0; i < listOfUsers.Count; i+=3)
{
   s += nickName[i] + "," + category[i] + "," + webList[i] + "," + ICQList[i] + "," + skypeList[i] + "," + mailList[i] + "," + phoneList[i] + ";\r";
}
System.IO.File.WriteAllText(@"" + folder + "Parse_people_list.csv", s, System.Text.Encoding.UTF8);

P1
(source: pixs.ru)

P2
(source: pixs.ru)

도움이 되었습니까?

해결책

Try delimiting with tabs instead - Excel handles tab-delimited data better than comma-delimited since commas within the data may cause issues.

You can also use StringBuilder.AppendLine to improve performance and avoid adding line breaks manually:

StringBuilder s = new StringBuilder();
s.AppendLine("Nick-Name\tCategory\tWeb-Pages\tICQ\tSkype\tMail\tPhone");
for (int i = 0; i < listOfUsers.Count; i+=3)
{
   s.AppendLine(
        nickName[i]  + "\t"
      + category[i]  + "\t"
      + webList[i]   + "\t"
      + ICQList[i]   + "\t"
      + skypeList[i] + "\t"
      + mailList[i]  + "\t"
      + phoneList[i]);;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top