Monday, August 13, 2012

Export to CSV File

Code Snippet
  1. <%@ Page Language="C#" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  4.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head>
  7.     <title>Export To CSV</title>
  8. <script runat="server">
  9.     public static void ExportCSV(System.Data.DataTable data, string fileName)
  10.     {
  11.         try
  12.         {
  13.             HttpContext context = HttpContext.Current;
  14.  
  15.             context.Response.Clear();
  16.             context.Response.ContentType = "text/csv";
  17.             context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
  18.  
  19.             //rite column header names
  20.             for (int i = 0; i < data.Columns.Count; i++)
  21.             {
  22.                 if (i > 0)
  23.                 {
  24.                     context.Response.Write(",");
  25.                 }
  26.                 context.Response.Write(data.Columns[i].ColumnName);
  27.             }
  28.             context.Response.Write(Environment.NewLine);
  29.             //Write data
  30.             foreach (System.Data.DataRow row in data.Rows)
  31.             {
  32.                 for (int i = 0; i < data.Columns.Count; i++)
  33.                 {
  34.                     if (i > 0)
  35.                     {
  36.                         context.Response.Write(",");
  37.                     }
  38.                     context.Response.Write(row[i].ToString());
  39.                 }
  40.                 context.Response.Write(Environment.NewLine);
  41.             }
  42.             context.Response.End();
  43.         }
  44.         catch (Exception ex)
  45.         {
  46.  
  47.         }
  48.  
  49.     }
  50.     
  51.     
  52.      private System.Data.DataTable CreateDataTable()
  53.     {
  54.         System.Data.DataTable dataTable = new System.Data.DataTable("Employee");
  55.  
  56.         dataTable.Columns.Add(new System.Data.DataColumn("ID", typeof(int)));
  57.         dataTable.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
  58.         dataTable.Columns.Add(new System.Data.DataColumn("Address", typeof(string)));
  59.  
  60.         System.Data.DataRow dataRow;
  61.  
  62.         dataRow = dataTable.NewRow();
  63.         dataRow["ID"] = "1";
  64.         dataRow["Name"] = "sandeep";
  65.         dataRow["Address"] = "Ncode";
  66.         dataTable.Rows.Add(dataRow);
  67.  
  68.         return dataTable;
  69.     }
  70.  
  71.      protected void btnExportDataTableToCSV_Click(object sender, EventArgs e)
  72.      {
  73.          ExportCSV(CreateDataTable(), "FileName");
  74.      }
  75.  
  76. </script>
  77.  
  78. </head>
  79. <body>
  80.  
  81.     <h3>FileUpload.SaveAs Method Example</h3>
  82.  
  83.     <form id="Form1" runat="server">
  84.  
  85.        <asp:Button ID="btnExportDataTableToCSV" runat="server" OnClick="btnExportDataTableToCSV_Click" Text="Export DataTable to CSV" />
  86.  
  87.     </form>
  88.  
  89. </body>
  90. </html>

0 comments:

Post a Comment