Пример #1
0
        /// <summary>
        /// Writes to response generated csv file
        /// </summary>
        /// <param name="data">DataTable, or DataView, or Generic List &lt;&gt;</param>
        /// <param name="columnNames">Comma separated list of columns</param>
        /// <param name="fileName">File name to return to user</param>
        public void WriteToCSV(object data, ReportOptions o)
        {
            sb = new StringBuilder();
            if (String.IsNullOrEmpty(o.FileName))
                throw new InvalidOperationException("Parameter fileName is not valid!");

            string attachment = String.Format("attachment; filename={0}.csv", o.FileName);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "text/csv; charset=windows-1251";
            HttpContext.Current.Response.AddHeader("Pragma", "public");

            if (data is DataTable)
            {
                DataTable t = (DataTable)data;
                WriteTable(o.Columns, t, o);
            }
            else if (data is DataView)
            {
                DataTable t = ((DataView)data).Table;
                WriteTable(o.Columns, t, o);
            }
            else if (data is IEnumerable)
            {
                IEnumerator en = ((IEnumerable)data).GetEnumerator();
                WriteHeader(o.HeaderText, o);
                while (en.MoveNext())
                {
                    ProcessListItem(en.Current, o.Columns, o);
                }
            }
            else
            {
                throw new InvalidOperationException("Parameter data is not DataTable, or DataView, or Generic List<>!");
            }
            var Response = HttpContext.Current.Response;
            string result = sb.ToString();
            Response.ContentEncoding = Encoding.GetEncoding(1251);
            Response.Write(ConvertString(result, Encoding.UTF8, Encoding.GetEncoding(1251)));

            Response.Flush();
            HttpContext.Current.Response.End();
        }
        protected void GridViewItemsList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                int id = int.Parse(e.CommandArgument.ToString());
                DownloadUserForms duf = new DownloadUserForms();
                List<DownloadUserForm> list = duf.GetByFileID(id);
                DownloadFiles df = new DownloadFiles();
                var file = df.GetById(id);

                ReportOptions ro = new ReportOptions();
                ro.Columns = "Email,FirstName,LastName";
                ro.HeaderText = "Email,Имя,Фамилия";
                ro.FileName = string.Format("csvreport_{0}_file.csv",file.FileName);
                new CsvHelper().WriteToCSV(list, ro);
            }
        }
Пример #3
0
 private void WriteTable(string columns, DataTable t, ReportOptions o)
 {
     WriteHeader(columns, o);
     foreach (DataRow r in t.Rows)
     {
         ProcessDataRow(r, columns,o );
     }
 }
Пример #4
0
 private void WriteHeader(string columns, ReportOptions o)
 {
     if (!String.IsNullOrEmpty(columns))
     {
         foreach (string c in columns.Split(','))
         {
             sb.Append(c);
             sb.Append(o.Splitter);
         }
         sb.Append(Environment.NewLine);
     }
 }
Пример #5
0
        private void ProcessListItem(object p, string colums, ReportOptions opt)
        {
            StringBuilder sb1 = new StringBuilder();

            foreach (string c in colums.Split(','))
            {

                PropertyInfo info = p.GetType().GetProperty(c);

                if (info == null)
                    throw new Exception(String.Format("No such field: {0}", c));

                if (info.CanRead)
                {
                    object o = info.GetValue(p, null);

                    AddWithComma(o, sb1, opt.Splitter);
                }
            }

            sb.Append(sb1.ToString().TrimEnd(opt.Splitter[0]));
            sb.Append(Environment.NewLine);
        }
Пример #6
0
        private void ProcessDataRow(DataRow r, string columns, ReportOptions o)
        {
            StringBuilder sb1 = new StringBuilder();

            if (String.IsNullOrEmpty(columns))
            {
                foreach (DataColumn c in r.Table.Columns)
                {
                    AddWithComma(r[c.ColumnName], sb1, o.Splitter);
                }
            }
            else
            {
                foreach (string c in columns.Split(','))
                {
                    AddWithComma(r[c], sb1, o.Splitter);
                }
            }

            sb.Append(sb1.ToString().TrimEnd(o.Splitter[0]));
            sb.Append(Environment.NewLine);
        }