Sin descripción

Tool.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Data;
  7. using System.Collections;
  8. using System.Text.RegularExpressions;
  9. namespace XYFDRQ.Common
  10. {
  11. public class Tool
  12. {
  13. public Tool()
  14. {
  15. //
  16. // TODO: 在此处添加构造函数逻辑
  17. //
  18. }
  19. #region 去除指定字段重复的行
  20. /// <summary>
  21. /// 去除指定字段重复的行
  22. /// </summary>
  23. /// <param name="dt">需要处理的datatable</param>
  24. /// <param name="DtFile">检测重复的字段</param>
  25. /// <returns>处理后的表</returns>
  26. public static DataTable DelDtSameRow(DataTable dt, params string[] DtFile)
  27. {
  28. ArrayList arrList = new ArrayList(); //记录删除重复字段
  29. List<int> DelRowIndex = new List<int>();//记录删除行在元数据表中的索引值
  30. for (int i = 0; i < dt.Rows.Count; i++)
  31. {
  32. string tmpStr = "";
  33. for (int m = 0; m < DtFile.Length; m++)
  34. {
  35. tmpStr += dt.Rows[i][DtFile[m]] + ",";
  36. }
  37. tmpStr = tmpStr.Length > 0 ? tmpStr.TrimEnd(',') : "";
  38. if (arrList.Contains(tmpStr))
  39. {
  40. DelRowIndex.Add(i);
  41. }
  42. else
  43. {
  44. arrList.Add(tmpStr);
  45. }
  46. }
  47. for (int i = 0; i < DelRowIndex.Count; i++)
  48. {
  49. dt.Rows.RemoveAt(DelRowIndex[i] - i);
  50. }
  51. return dt;
  52. }
  53. #endregion
  54. public static bool CheckMobile(string mobile)
  55. {
  56. Regex r = new Regex(@"^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$");
  57. return r.IsMatch(mobile);
  58. }
  59. /// <summary>
  60. /// DataTable保存到Excel
  61. /// </summary>
  62. /// <param name="p_Table"></param>
  63. /// <param name="p_Response"></param>
  64. public static void SaveToExcelUtf8(DataTable p_Table, HttpResponse p_Response, string fileName)
  65. {
  66. int _CountR = p_Table.Rows.Count;//行数
  67. int _CountC = p_Table.Columns.Count;//列数
  68. p_Response.Clear();
  69. p_Response.Buffer = true;
  70. p_Response.Charset = "UTF-8";
  71. p_Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
  72. p_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(fileName, Encoding.UTF8).ToString());
  73. p_Response.ContentType = "application/ms-excel";
  74. //写表头
  75. for (int i = 0; i < _CountC; i++)
  76. {
  77. p_Response.Write(p_Table.Columns[i].ColumnName + "\t");
  78. }
  79. p_Response.Write("\n");
  80. //写表内容
  81. for (int RowNo = 0; RowNo <= _CountR - 1; RowNo++)
  82. {
  83. string RowContent = "";
  84. for (int CloumnNo = 0; CloumnNo <= _CountC - 1; CloumnNo++)
  85. {
  86. RowContent += Convert.ToString(p_Table.Rows[RowNo][CloumnNo]) + "\t";
  87. }
  88. RowContent += "\n";
  89. p_Response.Write(RowContent);
  90. }
  91. p_Response.End();
  92. }
  93. /// <summary>
  94. /// DataTable保存到Excel
  95. /// </summary>
  96. public static void SaveToExcelGB2312(DataTable p_Table, HttpResponse p_Response, string fileName)
  97. {
  98. int _CountR = p_Table.Rows.Count;//行数
  99. int _CountC = p_Table.Columns.Count;//列数
  100. p_Response.Clear();
  101. p_Response.Buffer = true;
  102. //设置编码
  103. p_Response.Charset = "GB2312";
  104. p_Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  105. p_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
  106. p_Response.ContentType = "application/ms-excel";
  107. //写表头
  108. for (int i = 0; i < _CountC; i++)
  109. {
  110. p_Response.Write(p_Table.Columns[i].ColumnName + "\t");
  111. }
  112. p_Response.Write("\n");
  113. //写表内容
  114. for (int RowNo = 0; RowNo <= _CountR - 1; RowNo++)
  115. {
  116. string RowContent = "";
  117. for (int CloumnNo = 0; CloumnNo <= _CountC - 1; CloumnNo++)
  118. {
  119. RowContent += Convert.ToString(p_Table.Rows[RowNo][CloumnNo]).Replace("\t", "").Replace("\r", "").Replace("\n", "").Replace("\"", "") + "\t";
  120. }
  121. RowContent += "\n";
  122. p_Response.Write(RowContent);
  123. }
  124. p_Response.End();
  125. }
  126. /// <summary>
  127. /// 保存数据到Excel表格
  128. /// </summary>
  129. /// <param name="gv"></param>
  130. /// <param name="p_Response"></param>
  131. /// <param name="fileName"></param>
  132. public static void SaveDataToExcelGB2312(string content, HttpResponse p_Response, string fileName)
  133. {
  134. p_Response.Charset = "GB2312";
  135. p_Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  136. p_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
  137. p_Response.ContentType = "application/ms-excel";
  138. p_Response.Write(content);
  139. p_Response.End();
  140. }
  141. }
  142. }