Thursday 27 February 2014

DownLoad CSV file in Asp.net

 public void ExportToCsv(DataTable table, string name)
        {
            try
            {
                if (table.Rows.Count > 0)
                {
                    HttpContext context = HttpContext.Current;
                    context.Response.Clear();
                    foreach (DataColumn column in table.Columns)
                    {
                        context.Response.Write(column.ColumnName + ",");

                    }
                   context.Response.Write(Environment.NewLine);

                    foreach (DataRow row in table.Rows)
                    {

                        for (int i = 0; i < table.Columns.Count; i++)
                        {

                            context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");

                        }

                        context.Response.Write(Environment.NewLine);

                    }

                    context.Response.ContentType = "text/csv";
                    context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
                    context.Response.End();

                }
            }
}

No comments:

Post a Comment

रूस-यूक्रेन संकट लाइव: भारतीयों को 'उपलब्ध किसी भी साधन' के माध्यम से कीव को तत्काल छोड़ने के लिए कहा

  रूस यूक्रेन संकट लाइव: कीव में भारतीय दूतावास ने मंगलवार को जारी एक एडवाइजरी में भारतीयों को  'किसी भी उपलब्ध साधन' के माध्यम से क...