Link to home
Start Free TrialLog in
Avatar of Jasmin01
Jasmin01Flag for South Africa

asked on

C# writing stored procedure results to Excel file

Hi.

I have a stored procedure that executes the query attached (Sample Query).  The result of the query is similar to this:

Name      Acc_No    Pol_No     Date_Sold       Product
David      121822      P001      2012-06-12      Prod001
Tim              121822      P054      2012-04-14       Prod009
Sarah      121822      P066      2012-06-26      Prod001

What we are looking for is people with different names but the same account number, as in the example above.  I then need to note the earliest date sold (per account num), as that will be the correct account number and the rest will have to be corrected.

I need to put the results in an Excel spreadsheet that looks like the attached (Sample format Excel).  I have the query correct, I just have no idea how to get the results printed to Excel in that format.
Sample-Format-Excel.xlsx
SampleQuery.txt
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Instead of using C# to do this why just do the whole thing from excel. VBA is very similar to VB.Net and it will make doing what you are after mush easier.

Here is a link to get you started

http://www.excelguru.ca/content.php?135-Retrieve-Data-From-A-Database-To-Excel-Using-SQL

There are lots of excel experts on this site who can assist you further if needed.

Michael
Avatar of Jasmin01

ASKER

It has to be in C# because it will be added to a C# project once it has been tested.  Unfortunately, I cannot use anything else.
Avatar of Norie
Norie

How are you running the procedure from C# and where are you storing the results?
At the moment, I only have the sql query.  I need to take those results from the query and somehow export it to Excel.
You don't have any code to connect to the database and run the query?

What database is it?
It's a SQL database.
Jasmin

Is that SQL Server?
Its Sql Server 2008.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial