Exporting a Dataset into Excel Spreadsheet

Hi there,

I need to export a dataset i get back from an SQL server stored procedure into an excel spreadsheet. Ive looked on the net but cant find much. Can anyone help me.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
One of the way of doing that is:
DataSet DS = somevalueDataSet();
XmlDataDocument xdd = new XmlDataDocument(DS);
XslTransform  xt = new XslTransform();
string workingdir = AppDomain.CurrentDomain.BaseDirectory + "/" + "xslSheet.xsl";
string dirPath = AppDomain.CurrentDomain.BaseDirectory +  "/" + "GeneratedFR";
string filePath = AppDomain.CurrentDomain.BaseDirectory +  "/" + "GeneratedFR" + "/" + "Test1.xls";
if (!Directory.Exists(dirPath))
FileStream fs = new FileStream(filePath, FileMode.Create);

You can write a separate page for excel export.
You can build all the html code using StringBuilder which is little bit faster for large data)as you do in asp page and follow this.

OracleConnection dbCon      = myUtils.openAppsDB();
                  DataSet ds      = myUtils.GetDataSetFromAppsOrPPPP(Sql,"APPS",dbCon);
                  System.Text.StringBuilder sbBody = new System.Text.StringBuilder();  
                  sbBody.Append("<TABLE width='100%' border=1 cellspacing=2 cellpadding=2>");
                  sbBody.Append("<TR valign=Top>");
                  sbBody.Append("<td align=Center valign=Middle rowspan=2><B>PO&nbsp;No</B></td>");
                  sbBody.Append("<td align=Center valign=Middle rowspan=2><B>PO&nbsp;Date</B></td>");
                  foreach(DataTable myTable in ds.Tables)
                        if (myTable.Rows.Count > 0)
                              foreach(DataRow myRow in myTable.Rows)
                                    sbBody.Append("<TR valign=Top>");
                                    sbBody.Append("<TD valign=Top NOWRAP>"+ myRow["PO_NUMBER"].ToString() +"</TD>");
                                    sbBody.Append("<TD valign=Top>"+ myRow["PO_CREATION_DATE"].ToString() +"</TD>");

                              sbBody.Append("<TR><TD>No records found with your your search criteria.Please try again</TD></TR>");
                  Response.Buffer = true;                  
                  Response.ContentType = "application/vnd.ms-excel";                  
                  this.EnableViewState = false;                  
                  Response.End ();            

You can also save this to ~ (or ,)separated file and can opne in excel as follows
      Response.ContentType = "text/plain";(instead of "application/vnd.ms-excel"; from above)
                  Response.AddHeader ("Content-Disposition","inline;filename=BuyerPartlist.txt");
Volkan VardarSoftware Team LeaderCommented:
this sample code is easy and very useful:

good luck
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.