Export data to csv

   public void ExportToCSV()
        {
            string now = DateTime.Now.ToString().Replace(" ", "").Replace("-", "_").Replace(":", "").Replace("/", "_").Replace("\\", "");
            string strFilePath = Server.MapPath("ExcelFiles") + "\\Analytics_" + now + ".csv";

            AccountModel account = AccountBusinessLogic.Read(UserSession.CurrentAccountID);

            //try
            //{
            DateTime sDate;
            DateTime eDate;
            if(!string.IsNullOrEmpty(account.TimeZone))
            {

            sDate =ServerSettings.SetTimeByTimeZone( DateTime.ParseExact(Convert.ToString(hdfStartDate.Value), "MM/dd/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture), account.TimeZone);
            eDate = ServerSettings.SetTimeByTimeZone(DateTime.ParseExact(Convert.ToString(hdfEndDate.Value), "MM/dd/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture), account.TimeZone);
            }
            else
            {
             sDate =DateTime.ParseExact(Convert.ToString(hdfStartDate.Value), "MM/dd/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            eDate = DateTime.ParseExact(Convert.ToString(hdfEndDate.Value), "MM/dd/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);
            }

            txtStartDate.Text = hdfStartDate.Value;
            txtEndDate.Text = hdfEndDate.Value;



            DataTable Dt = SweepsTakesFormSettingsBusinessLogic.GetSweepStakesAnalyticsRecord(sweepStakedID, sDate, eDate, isUnique).Tables[0];


            if (Dt.Rows.Count > 0)
            {
                // Create the CSV file to which grid data will be exported.

                StreamWriter sw = new StreamWriter(strFilePath, false);

                //First we will write the headers.

                int iColCount = Dt.Columns.Count;

                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(Dt.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);

                // Now write all the rows.

                foreach (DataRow dr in Dt.Rows)
                {
                    if (!string.IsNullOrEmpty(account.TimeZone))
                    {
                        dr["CreateDatetime"] = ServerSettings.GetTimeByTimeZone(Common.CDate(dr["CreateDatetime"]), account.TimeZone);
                    }
                   
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                          
                            sw.Write(dr[i].ToString());
                        }
                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }
                    sw.Write(sw.NewLine);
                }
                sw.Close();
                WriteAttachment("Analytics_" + now + ".csv", "text/csv", Server.MapPath("ExcelFiles//"));
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "close", "javascript:alert('No Records to Export');", true);
            }



            //}
            //catch
            //{

            //    ClientScript.RegisterStartupScript(this.GetType(), "close", "javascript:alert('No Records to Export');", true);
            //}

        }
        public static void WriteAttachment(string FileName, string FileType, string FilePath)
        {
            HttpResponse Response = System.Web.HttpContext.Current.Response;
            Response.ClearHeaders();
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
            Response.ContentType = FileType;
            Response.WriteFile(FilePath + FileName);
            Response.End();
        }

Comments