export data of object to excel

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi

i have a programm where i excecute som sql-commands. The received data i put in objects and i do some operations.
then i want to export the data of these objects to excel (or maybe to a csv-file)
how can i do this in c-sharp

thanks
jac
 
Doing this with a CSV file is relatively easy. I assume you have an object
with the data to be written. Here is a code template you can modify:

using (StreamWriter sw = File.CreateText("MyData.csv")
foreach (DataRow dr in MyDataTable)
sw.WriteLine(dr["Name"] + ", " + dr["Phone"] + ", " + dr["Email"]);

That's all there is to it! Of course, you'll replace the data row, data
table, and fields with your specific data structure. Good luck!

- Noah Coad -
Microsoft MVP

jac said:
hi,

i have a programm where i excecute som sql-commands. The received data i
put in objects and i do some operations.
 
Hey Noah,
Why do you prefer to use:
StreamWriter sw = File.CreateText("MyData.csv");
Instead of:
StreamWriter sw = new StreamWriter("MyData.csv");
Just curious...
Eric

Noah Coad said:
Doing this with a CSV file is relatively easy. I assume you have an object
with the data to be written. Here is a code template you can modify:

using (StreamWriter sw = File.CreateText("MyData.csv")
foreach (DataRow dr in MyDataTable)
sw.WriteLine(dr["Name"] + ", " + dr["Phone"] + ", " + dr["Email"]);

That's all there is to it! Of course, you'll replace the data row, data
table, and fields with your specific data structure. Good luck!

- Noah Coad -
Microsoft MVP

jac said:
hi,

i have a programm where i excecute som sql-commands. The received data i
put in objects and i do some operations.
then i want to export the data of these objects to excel (or maybe to a csv-file).
how can i do this in c-sharp?

thanks,
jac
 
Just preference really. I frequently use the File object for various types
of files, including text, so it is what I most commonly use. Both achive
the same result. The StreamWriter provides more options when instantiating.
I believe File.CreateText just returns a "new StreamWriter(File)".

- Noah Coad -
Microsoft MVP

Eric Weinschenk said:
Hey Noah,
Why do you prefer to use:
StreamWriter sw = File.CreateText("MyData.csv");
Instead of:
StreamWriter sw = new StreamWriter("MyData.csv");
Just curious...
Eric

Noah Coad said:
Doing this with a CSV file is relatively easy. I assume you have an object
with the data to be written. Here is a code template you can modify:

using (StreamWriter sw = File.CreateText("MyData.csv")
foreach (DataRow dr in MyDataTable)
sw.WriteLine(dr["Name"] + ", " + dr["Phone"] + ", " + dr["Email"]);

That's all there is to it! Of course, you'll replace the data row, data
table, and fields with your specific data structure. Good luck!

- Noah Coad -
Microsoft MVP

jac said:
hi,

i have a programm where i excecute som sql-commands. The received data
i
put in objects and i do some operations.
then i want to export the data of these objects to excel (or maybe to
a
csv-file).
how can i do this in c-sharp?

thanks,
jac
 
Back
Top