Connecting Tech Pros Worldwide Forums | Help | Site Map

Pushing query result in an excel or csv file.

Member
 
Join Date: Jun 2009
Location: Bangalore
Posts: 45
#1: Jun 22 '09
Hi Guys,

Is it possible to have query result in a excel or csv file instead of having it in a datareader or dataset. please let me konw one or the other way to do it.

Thanks,
Manik

Expert
 
Join Date: Jan 2008
Location: York
Posts: 179
#2: Jun 24 '09

re: Pushing query result in an excel or csv file.


Well if it's in some sort of a dataset, I'm assuming you can itterate over rows and columns?

If so you could just built a little CSVWriter of your own?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Jun 24 '09

re: Pushing query result in an excel or csv file.


Also your database probably already supports a command that exports data as csv.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,695
#4: Jun 24 '09

re: Pushing query result in an excel or csv file.


There is no framework element that will automatically dump your query into a CSV from code. But as Ian suggested, just iterate through your results and make your own.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Jun 25 '09

re: Pushing query result in an excel or csv file.


Quote:

Originally Posted by insertAlias View Post

There is no framework element that will automatically dump your query into a CSV from code. But as Ian suggested, just iterate through your results and make your own.

But using the built in database commands would be much faster as the commands are already well optimized for that and there will be no need to write any code. Generally it is better to do with code only what the database cannot do.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,695
#6: Jun 25 '09

re: Pushing query result in an excel or csv file.


Right, I was assuming that he had some need to do it programatically, possibly part of a larger process. If not, your way is definitely best.
Member
 
Join Date: Jun 2009
Location: Bangalore
Posts: 45
#7: Jul 1 '09

re: Pushing query result in an excel or csv file.


It was very easy, sharing it for others. You can put your datagrid in the excel file with the same grid feel.
Expand|Select|Wrap|Line Numbers
  1. myDataGrid.DataSource = ds;
  2. myDataGrid.DataBind();
  3. dbConn_data.Close();
  4. myDataGrid.Visible=true;
  5.  //Adding Grid data in Excel sheet -start    
  6. Response.Clear(); 
  7. Response.AddHeader("content-disposition", "inline;filename=SurveyReport.xls");
  8. Response.Charset = "";
  9. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  10. Response.ContentType = "application/vnd.xls";
  11. System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
  12. myDataGrid.RenderControl(htmlWrite);
  13. Response.Write(stringWrite.ToString());
  14. Response.End();
Reply