I'm trying to create a web application, in which I can get a result set from SQL Server and export it to a csv or Excel file, whichever is easier or has better performance. The SQL server is installed on a separate server, which is not accessible for public. The web server, in which the application has been installed, has access to the SQL Server, but they are in different domains. Heretofore, when I had SQL Server installed on the same server that my application was, I was able to use OPENROWSET in SQL Server to copy the result directly into an Excel file, using Microsoft OLEDB engine. But since I separated the SQL Server from web server (because of security issues), it is not functional anymore. The OPENROWSET statement cannot recognize the folders in the web server. Hence I was thinking that I should create a function in my web application (in c#) to fetch data from SQL Server and export it into a Excel or CSV file. I know how to read records from SQL Server, but I don't know how to write it into a file (Excel/CSV). I was wondering if there is a solution, or any sample that I can use. I've already found the csvHelper from NuGet, but I don't know how to use it for my purpose. Please give me your advice. Thank you.
Asked
Active
Viewed 1,763 times
0
-
1CSV is just a text file - just use standard text writing routines. Where does the file have to end up? on the SQL Server, on the web server or does it need to be sent to the web user? – Nick.Mc Dec 19 '13 at 23:28
-
1This can be helpful http://stackoverflow.com/questions/1963719/c-sharp-sqlserver-retrieving-results-and-place-in-a-csv-format – trailmax Dec 19 '13 at 23:28
-
1Take a look at the article [Are there any CSV readers/write libraries in C#][1] [1]: http://stackoverflow.com/q/1941392/964043 – dmarietta Dec 19 '13 at 23:42
-
The file is going to stay on the web server, hence I think the text writing would be the easiest way. Thank you both for your helpful inputs. – Fred Dec 19 '13 at 23:46
1 Answers
0
If you're using a gridview at all here's some tested code for exporting said Gridview to Excel.
Export Gridview Data to Excel in ASP.NET Linq data bind
It may or may not be the exact thing you're looking for but perhaps it can lead you in the right direction.

Community
- 1
- 1

Techie Joe
- 847
- 2
- 14
- 32