Connecting Tech Pros Worldwide Help | Site Map

Pushing query result in an excel or csv file.

  #1  
Old June 22nd, 2009, 03:27 PM
Member
 
Join Date: Jun 2009
Location: Bangalore
Posts: 41
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
  #2  
Old June 24th, 2009, 12:13 PM
Expert
 
Join Date: Jan 2008
Location: York
Posts: 178

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?
  #3  
Old June 24th, 2009, 03:32 PM
Administrator
 
Join Date: Sep 2006
Posts: 12,084

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


Also your database probably already supports a command that exports data as csv.
  #4  
Old June 24th, 2009, 04:24 PM
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,569

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.
  #5  
Old June 25th, 2009, 07:23 AM
Administrator
 
Join Date: Sep 2006
Posts: 12,084

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.
  #6  
Old June 25th, 2009, 02:20 PM
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,569

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.
  #7  
Old July 1st, 2009, 12:25 PM
Member
 
Join Date: Jun 2009
Location: Bangalore
Posts: 41

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