Frage

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)

War es hilfreich?

Lösung

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]);;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top