PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/App_Code/DataFlow.cs

https://bitbucket.org/xpertech/paycell
C# | 267 lines | 207 code | 28 blank | 32 comment | 34 complexity | d034c1c5e8e7297049da5aaa358ff8f5 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14. using System.Collections.Generic;
  15. using System.Text;
  16. using System.Data.SqlClient;
  17. using System.Text.RegularExpressions;
  18. /// <summary>
  19. /// Summary description for DataFlow
  20. /// </summary>
  21. public class DataFlow
  22. {
  23. public DataFlow()
  24. {
  25. //
  26. // TODO: Add constructor logic here
  27. //
  28. }
  29. public static DataTable csvToDataTable(string file, bool isRowOneHeader)
  30. {
  31. string head;
  32. DataTable csvDataTable = new DataTable();
  33. //no try/catch - add these in yourselfs or let exception happen
  34. String[] csvData = File.ReadAllLines(file);
  35. // Static method:
  36. Regex re = new Regex("^[-'a-zA-Z]*$");
  37. // Name does not match schema
  38. //if no data in file ‘manually’ throw an exception
  39. if (csvData.Length == 0)
  40. {
  41. throw new Exception("CSV File Appears to be Empty");
  42. }
  43. String[] headings = csvData[0].Split(',');
  44. int index = 0; //will be zero or one depending on isRowOneHeader
  45. if (isRowOneHeader) //if first record lists headers
  46. {
  47. index = 1; //so we won’t take headings as data
  48. //for each heading
  49. for (int i = 0; i < headings.Length; i++)
  50. {
  51. string a = headings[0];
  52. //replace spaces with underscores for column names
  53. headings[i] = headings[i].Replace(" ", "_");
  54. //add a column for each heading
  55. head = headings[i].ToString();
  56. csvDataTable.Columns.Add(headings[i], typeof(string));
  57. }
  58. }
  59. else //if no headers just go for col1, col2 etc.
  60. {
  61. csvDataTable.Columns.Add("Mobile Number", typeof(string));
  62. //csvDataTable.Columns.Add("EmailId", typeof(string));
  63. //csvDataTable.Columns.Add("First Name", typeof(string));
  64. //csvDataTable.Columns.Add("Last Name", typeof(string));
  65. }
  66. //populate the DataTable
  67. for (int i = index; i < csvData.Length; i++)
  68. {
  69. //create new rows
  70. DataRow row = csvDataTable.NewRow();
  71. for (int j = 0; j < headings.Length; j++)
  72. {
  73. //fill them
  74. row[j] = csvData[i].Split(',')[j];
  75. }
  76. //add rows to over DataTable
  77. csvDataTable.Rows.Add(row);
  78. }
  79. string check_special_characters = CheckString(File.ReadAllText(file));
  80. if (check_special_characters.Length == 0)
  81. {
  82. return csvDataTable;
  83. }
  84. else
  85. {
  86. csvDataTable = null;
  87. return csvDataTable;
  88. }
  89. }
  90. public static string CheckString(string text)
  91. {
  92. Regex oRegex = new Regex(@"[<;#$^/>]+", RegexOptions.IgnoreCase);
  93. Match oMatch = oRegex.Match(text);
  94. oRegex = null;
  95. if (oMatch.Success)
  96. return "Please exclude special characters";
  97. else
  98. return "";
  99. }
  100. public void ExportCSV(DataTable data, String filename)
  101. {
  102. HttpContext context = HttpContext.Current;
  103. context.Response.Clear();
  104. context.Response.ContentType = "text/csv";
  105. context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".csv");
  106. //write column header names
  107. for (int i = 0; i < data.Columns.Count; i++)
  108. {
  109. if (i > 0)
  110. {
  111. context.Response.Write(",");
  112. }
  113. context.Response.Write(data.Columns[i].ColumnName);
  114. }
  115. context.Response.Write(Environment.NewLine);
  116. foreach (DataRow row in data.Rows)
  117. {
  118. for (int i = 0; i < data.Columns.Count; i++)
  119. {
  120. if (i > 0)
  121. {
  122. context.Response.Write(",");
  123. }
  124. context.Response.Write(row[i].ToString());
  125. }
  126. context.Response.Write(Environment.NewLine);
  127. }
  128. context.Response.End();
  129. }
  130. public void ExportTXT(DataTable data, String filename)
  131. {
  132. HttpContext context = HttpContext.Current;
  133. context.Response.Clear();
  134. context.Response.ContentType = "text/csv";
  135. context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".txt");
  136. //write column header names
  137. for (int i = 0; i < data.Columns.Count; i++)
  138. {
  139. if (i > 0)
  140. {
  141. context.Response.Write(",");
  142. }
  143. context.Response.Write(data.Columns[i].ColumnName);
  144. }
  145. context.Response.Write(Environment.NewLine);
  146. foreach (DataRow row in data.Rows)
  147. {
  148. for (int i = 0; i < data.Columns.Count; i++)
  149. {
  150. if (i > 0)
  151. {
  152. context.Response.Write(",");
  153. }
  154. context.Response.Write(row[i].ToString());
  155. }
  156. context.Response.Write(Environment.NewLine);
  157. }
  158. context.Response.End();
  159. }
  160. public static void Export(string fileName, GridView gv)
  161. {
  162. HttpContext.Current.Response.Clear();
  163. HttpContext.Current.Response.AddHeader(
  164. "content-disposition", string.Format("attachment; filename={0}", fileName));
  165. HttpContext.Current.Response.ContentType = "application/ms-excel";
  166. using (StringWriter sw = new StringWriter())
  167. {
  168. using (HtmlTextWriter htw = new HtmlTextWriter(sw))
  169. {
  170. // Create a form to contain the grid
  171. Table table = new Table();
  172. table.GridLines = gv.GridLines;
  173. // add the header row to the table
  174. if (gv.HeaderRow != null)
  175. {
  176. DataFlow.PrepareControlForExport(gv.HeaderRow);
  177. table.Rows.Add(gv.HeaderRow);
  178. }
  179. // add each of the data rows to the table
  180. foreach (GridViewRow row in gv.Rows)
  181. {
  182. DataFlow.PrepareControlForExport(row);
  183. table.Rows.Add(row);
  184. }
  185. // add the footer row to the table
  186. if (gv.FooterRow != null)
  187. {
  188. DataFlow.PrepareControlForExport(gv.FooterRow);
  189. table.Rows.Add(gv.FooterRow);
  190. }
  191. // render the table into the htmlwriter
  192. table.RenderControl(htw);
  193. // render the htmlwriter into the response
  194. HttpContext.Current.Response.Write(sw.ToString());
  195. HttpContext.Current.Response.End();
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// Replace any of the contained controls with literals
  201. /// </summary>
  202. /// <param name="control"></param>
  203. private static void PrepareControlForExport(Control control)
  204. {
  205. for (int i = 0; i < control.Controls.Count; i++)
  206. {
  207. Control current = control.Controls[i];
  208. if (current is LinkButton)
  209. {
  210. control.Controls.Remove(current);
  211. control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
  212. }
  213. else if (current is ImageButton)
  214. {
  215. control.Controls.Remove(current);
  216. control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
  217. }
  218. else if (current is HyperLink)
  219. {
  220. control.Controls.Remove(current);
  221. control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
  222. }
  223. else if (current is DropDownList)
  224. {
  225. control.Controls.Remove(current);
  226. control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
  227. }
  228. else if (current is CheckBox)
  229. {
  230. control.Controls.Remove(current);
  231. control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
  232. }
  233. if (current.HasControls())
  234. {
  235. DataFlow.PrepareControlForExport(current);
  236. }
  237. }
  238. }
  239. }