Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export data to a text file 1

Status
Not open for further replies.

netangel

Programmer
Feb 7, 2002
124
PT
Is it possible to export the contents of a datatable into a text file, or even better into a csv file?

I need to export a select statment from the database directy to a file, so users can open it in Excel, and work on it as they want.

I've tried to export it as a xml file, but Excel couldn't handle it. I thought it was strange, but couldn't get it to work.

Any ideas?

NetAngel
 
Hey NA,

Couple questions:
- what type of db is it (access, sql server, etc.)?
- does this have to happen within asp.net, or can it be something the db does on its own?

D

 
I can't rely on the database.

Our ASP app works with several database motors: SQLServer, Sybase, Oracle, Access, and with several connection types (ODBC, Access link table, direct connection), depending on the client size and preferences.

Most clients use a Access file linked to Sybase database and the application links to that Access file via ODBC (don't ask me, I don't agree with that configuration. It was already working with other applications when I started working here, and I just have to follow old habits).

So I need to set a page with a textarea to insert some sql statment, and a button that generates a txt or cvs file.
I can do it writing line by line from a datatable to the file, but I believe there's got to be a better way.
NetAngel
 
ah, ok. Thats quite the db mess, isnt it?!

I havn't done it myself, but I've seen posts that have, so:

Have you looked into the Excel object library in .NET, and how you can create an xHell file in code and save it using hte system.IO namespace classes?

Also, to write to a textfile would be much of the same thing: using the IO libraries to write to a file.

I'll see if I can find some examples of how to pull it off.

D
 
Thanks. It was the sort of code I was writing, and seeing it on a Microsoft Support site, ensured me that my way wasn't so wrong after all.

Still, I believe that there is a better way, not having to loop thru all the rows of my table.
NetAngel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top