.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Export dataset data into various types

Posted By:Dhivakaran       Posted Date: November 29, 2010    Points: 50    Category: C#    URL: http://www.dotnetspark.com  

Here I'm going to display how to export dataset data into various file types like excel, pdf, text etc...
 

Step1:

Create one class into your project and add below methods within your class.the below method I have passed three parameters. one is "result"in this argument we need to pass the dataset and second parameter name is "strName" in this argument we can specify(pass) the file name. and last parameter we can specify file type like excel pdf, text,etc...

 private void ExportToSelectedFormat(DataSet result, string strName,string strFormat)
       {
           HttpResponse response = HttpContext.Current.Response;
           response.Clear();
           response.Charset = "";
         
           switch (strFormat)
           {
               case "MSEXCEL":
                   response.ContentType = "application/vnd.xls";
                   response.AddHeader("Content-Disposition", "attachment;filename=\"" + strName + ".xls\"");
                   break;
               case "PDF":
                   response.ContentType = "application/pdf";
                   response.AddHeader("Content-Disposition", "attachment;filename=\"" + strName + ".pdf\"");
                   break;
               case "MSWORD":
                   response.ContentType = "application/vnd.word";
                   response.AddHeader("Content-Disposition", "attachment;filename=\"" + strName + ".word\"");
                   break;
               case "TEXT":
                   response.ContentType = "text/plain";
                   response.AddHeader("Content-Disposition", "attachment;filename=\"" + strName + ".txt\"");
                   break;
               case "CSV":
                   response.ContentType = "text/csv";
                   response.AddHeader("Content-Disposition", "attachment;filename=\"" + strName + ".csv\"");
                   break;  
           }
           if (result != null)
           {
               using (StringWriter sw = new StringWriter())
               {
                   using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                   {
                       // instantiate a datagrid
                       DataGrid dg = new DataGrid();
                       if (result != null && result.Tables[0].Rows.Count > 0)
                       {
                           dg.DataSource = result;
                           dg.DataBind();
                           dg.RenderControl(htw);
                       }
                       htw.WriteBreak();
                       response.Write(sw.ToString());
                       response.End();
                   }
               }
           }

       }

Steps 2:

Place button in your form "Export" and within button click eventCreate one dataset object and assign some data into dataset as below code.

dataset ds=new dataset();

ds=databasecontent();

// then pass this ds to exporttotext method like below

ExportToSelectedFormat(ds,"Sample","MSEXCEL");

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend