paycell /App_Code/DataFlow.cs

Language C# Lines 268
MD5 Hash d034c1c5e8e7297049da5aaa358ff8f5 Estimated Cost $5,169 (why?)
Repository https://bitbucket.org/xpertech/paycell View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Text.RegularExpressions;


/// <summary>
/// Summary description for DataFlow
/// </summary>
public class DataFlow
{
    
	public DataFlow()
	{
		//
		// TODO: Add constructor logic here
		//  
	}
    public static DataTable csvToDataTable(string file, bool isRowOneHeader)
    {
        string head;
        DataTable csvDataTable = new DataTable();
        //no try/catch - add these in yourselfs or let exception happen
        String[] csvData = File.ReadAllLines(file);

        // Static method:
        Regex re = new Regex("^[-'a-zA-Z]*$");

        // Name does not match schema

        //if no data in file ‘manually’ throw an exception
        if (csvData.Length == 0)
        {
            throw new Exception("CSV File Appears to be Empty");
        }
        String[] headings = csvData[0].Split(',');

        int index = 0; //will be zero or one depending on isRowOneHeader
        if (isRowOneHeader) //if first record lists headers
        {
            index = 1; //so we won’t take headings as data
            //for each heading
            for (int i = 0; i < headings.Length; i++)
            {
                string a = headings[0];

                //replace spaces with underscores for column names
                headings[i] = headings[i].Replace(" ", "_");
                //add a column for each heading
                head = headings[i].ToString();
                csvDataTable.Columns.Add(headings[i], typeof(string));
            }
        }
        else //if no headers just go for col1, col2 etc.
        {
            csvDataTable.Columns.Add("Mobile Number", typeof(string));
            //csvDataTable.Columns.Add("EmailId", typeof(string));
            //csvDataTable.Columns.Add("First Name", typeof(string));
            //csvDataTable.Columns.Add("Last Name", typeof(string));
        }

        //populate the DataTable
        for (int i = index; i < csvData.Length; i++)
        {
            //create new rows
            DataRow row = csvDataTable.NewRow();

            for (int j = 0; j < headings.Length; j++)
            {

                //fill them
                row[j] = csvData[i].Split(',')[j];

            }
            //add rows to over DataTable
            csvDataTable.Rows.Add(row);
        }

        string check_special_characters = CheckString(File.ReadAllText(file));
        if (check_special_characters.Length == 0)
        {
            return csvDataTable;
        }
        else
        {
            csvDataTable = null;
            return csvDataTable;
        }
    }

     public static string CheckString(string text)  
         {
           Regex oRegex = new Regex(@"[<;#$^/>]+", RegexOptions.IgnoreCase);  
           Match oMatch = oRegex.Match(text);  
           oRegex = null;  
         
          if (oMatch.Success)  
             return "Please exclude special characters";  
           else  
             return "";  
         }  
    public void ExportCSV(DataTable data, String filename)
    {
        HttpContext context = HttpContext.Current;
        context.Response.Clear();
        context.Response.ContentType = "text/csv";
        context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".csv");
        //write column header names
        for (int i = 0; i < data.Columns.Count; i++)
        {
            if (i > 0)
            {
                context.Response.Write(",");
            }
            context.Response.Write(data.Columns[i].ColumnName);

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

        foreach (DataRow row in data.Rows)
        {
            for (int i = 0; i < data.Columns.Count; i++)
            {
                if (i > 0)
                {
                    context.Response.Write(",");
                }
                context.Response.Write(row[i].ToString());
            }
            context.Response.Write(Environment.NewLine);
        }
        context.Response.End();

    }
    public void ExportTXT(DataTable data, String filename)
    {
        HttpContext context = HttpContext.Current;
        context.Response.Clear();
        context.Response.ContentType = "text/csv";
        context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".txt");
        //write column header names
        for (int i = 0; i < data.Columns.Count; i++)
        {
            if (i > 0)
            {
                context.Response.Write(",");
            }
            context.Response.Write(data.Columns[i].ColumnName);

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

        foreach (DataRow row in data.Rows)
        {
            for (int i = 0; i < data.Columns.Count; i++)
            {
                if (i > 0)
                {
                    context.Response.Write(",");
                }
                context.Response.Write(row[i].ToString());
            }
            context.Response.Write(Environment.NewLine);
        }
        context.Response.End();

    }
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();
                table.GridLines = gv.GridLines; 

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    DataFlow.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    DataFlow.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    DataFlow.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                DataFlow.PrepareControlForExport(current);
            }
        }
    }
}
Back to Top