Example #1
0
        public static void WriteToCSV(DataTable dt, ExportType exportType, string viewName)
        {
            StringBuilder sb = new StringBuilder();
            string attachment = String.Format("attachment; filename={0}.csv", exportType);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");

            DBAdminDA DA = new DBAdminDA();
            using (DataTable dtc = DA.GetColumns(viewName)) {
                foreach (DataRow dr in dtc.Rows) {
                    AddColumn(dr["COLUMN_NAME"].ToString(), sb, false);
                }
                HttpContext.Current.Response.Write(sb.ToString());
                HttpContext.Current.Response.Write(Environment.NewLine);
            }
            DA = null;

            foreach(DataRow row in dt.Rows) {
                sb = new StringBuilder();
                foreach(DataColumn column in dt.Columns) {
                    if (exportType.Equals(ExportType.Donation)) {
                        if (column.ColumnName.Equals("CreditCardNumber") || column.ColumnName.Equals("SecurityCode")) {
                            AddColumn(Security.Decrypt(row[column].ToString()), sb, true);
                        } else {
                            AddColumn(row[column].ToString(), sb, false);
                        }
                    } else if (exportType.Equals(ExportType.Membership)) {
                        if (column.ColumnName.Equals("Message")) {
                            AddColumn("", sb, false); //omit from exported data
                        } else if (column.ColumnName.Equals("CreditCardNumber") || column.ColumnName.Equals("SecurityCode")) {
                            AddColumn(Security.Decrypt(row[column].ToString()), sb, true);
                        } else {
                            AddColumn(row[column].ToString(), sb, false);
                        }
                    }
                    else if (exportType.Equals(ExportType.BigBookSaleVolunteer) || exportType.Equals(ExportType.BookAndPlantSaleVolunteer) || exportType.Equals(ExportType.AnniversaryBookSaleVolunteer))
                    {
                        AddColumn(row[column].ToString(), sb, true);
                    } else {
                        AddColumn(row[column].ToString(), sb, false);
                    }
                }
                HttpContext.Current.Response.Write(sb.ToString());
                HttpContext.Current.Response.Write(Environment.NewLine);
            }

            HttpContext.Current.Response.End();
        }