Click here to Skip to main content
15,064,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a 2 list ,which i want to append the list values to .CSV ...when i open the .csv file which looks very similar to excel..My issue is am not binding list values to different cells ..list values are binding to one cell itself.

if i use
file.Write(listitem)
it will append to one cell

else
file.WriteLine(listitem)
it will bind list items vertically in .csv file

how can write each list1 item values horizontally in .csvfile(which looks similar to excel) then append next list2 items in newline


foreach (string s in list)
                    {
                        file.Write(string.Join(" ", s));

                    }
                    file.WriteLine();
</pre>


can you suggest me any idea to achieve this?
Posted
Updated 24-Jul-13 2:23am
v4

1 solution

Concatenate them together. This will write item1 followed by a comma and then item 2 and then the newline character.

C#
file.WriteLine(listitem1 & "," & listeitem2)
   
Comments
keerth516 24-Jul-13 8:19am
   
you didnt get what i was trying to say, I have list1 contains 5items,want to write these 5items to different cells of .csv file instead it writing 5items to one cell itself. I want write each list items in one cell and then shift to adjacent cell and write second item. can u please suggest any other idea to achieve this.
ZurdoDev 24-Jul-13 8:22am
   
Why won't this work? You said you wanted CSV.
keerth516 24-Jul-13 8:32am
   
yes here when i create a .csv file ..it looks similar as excel sheet.
I could bind all the list items in one cell itself, not each item in different cell...can u pls suggest me any idea how can i write item1 to cell1 then item2 to adjacent cell.
ZurdoDev 24-Jul-13 8:41am
   
What I suggested will put them into different cells. A CSV is a list of values separated by commas. When you open that in Excel, you tell it to use the comma as the delimiter and then it will put them into different cells. I don't see where you are stuck on this.
keerth516 24-Jul-13 8:56am
   
sorry i was having some other issue which i could n't see it..yes your suggestion helped me..thanks
ZurdoDev 24-Jul-13 9:06am
   
No problem. Glad to know it helped.
keerth516 24-Jul-13 22:13pm
   
hi ryanb31 i have commas in list item this has to be taken as one string instead its splitting and assigning to next cell one more question even the '\r\n" in list item going to nextline...how can i avoid not to go mext and remains as single string with commas. any idea
ZurdoDev 24-Jul-13 22:19pm
   
I believe if you put the values in " " Excel will keep it as one.
keerth516 25-Jul-13 1:05am
   
no it didnt work...any other idea!!?
ZurdoDev 25-Jul-13 7:45am
   
It will, but when you open in Excel tell it to use " as the text qualifier.
keerth516 26-Jul-13 0:51am
   
hi ryanb31 ..thanks again...i did this way item="keerth516,24,b'lore" while writting to csv file "\""+item+"\"" ,then its working fine even for carriage return,newline

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month