革博士程序V1仓库
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

214 行
9.0 KiB

  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. namespace GeBoShi.SysCtrl
  13. {
  14. public static class Utils
  15. {
  16. private static Regex RegNumber = new Regex("^[0-9]+$"); //匹配 整数(不可以带正负号)
  17. private static Regex RegDecimal = new Regex("^(-?\\d+)(\\.\\d+)?$"); //浮点 (可以带正负号)
  18. public static bool IsDecimal(object inputData)
  19. {
  20. if (inputData == null) return false;
  21. Match m = RegDecimal.Match(inputData.ToString());
  22. return m.Success;
  23. }
  24. public static bool IsNumber(object inputData)
  25. {
  26. if (inputData == null) return false;
  27. Match m = RegNumber.Match(inputData.ToString());
  28. return m.Success;
  29. }
  30. private static double ContrastLow = 0.8;
  31. private static double ContrastTop = 1.2;
  32. /// <summary>
  33. /// 获取对比度百分比 0-100 对应0.6-1.4
  34. /// </summary>
  35. /// <param name="val"></param>
  36. /// <returns></returns>
  37. public static double ContrastToPercent(double val)
  38. {
  39. if (val < ContrastLow)
  40. return 0;
  41. else if (val > ContrastTop)
  42. return 100;
  43. double temp = 100 / (ContrastTop - ContrastLow);
  44. return Math.Round(temp * (val - ContrastLow), 2);
  45. }
  46. public static double PercentToContrast(double val)
  47. {
  48. double tt = 1;
  49. if (val > 100)
  50. tt = 100;
  51. else if (val < 0)
  52. tt = 0;
  53. else
  54. tt = val;
  55. double temp = tt / 100 * (ContrastTop - ContrastLow);
  56. return temp + ContrastLow;
  57. }
  58. public class ExcelUtil
  59. {
  60. public static DataTable ConvertToDataTable<T>(IList<T> data, List<string> lstColName)
  61. {
  62. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
  63. DataTable table = new DataTable();
  64. foreach (PropertyDescriptor prop in properties)
  65. {
  66. if (lstColName.Contains(prop.Name))
  67. table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
  68. }
  69. foreach (T item in data)
  70. {
  71. DataRow row = table.NewRow();
  72. foreach (PropertyDescriptor prop in properties)
  73. {
  74. if (lstColName.Contains(prop.Name))
  75. row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
  76. }
  77. table.Rows.Add(row);
  78. }
  79. return table;
  80. }
  81. public static bool DataTable2CSV(string saveFilePath, System.Data.DataTable dt)
  82. {
  83. //MessageBox.Show(AbosultedFilePath);
  84. System.IO.FileStream fs = new FileStream(saveFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
  85. StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
  86. //Tabel header
  87. for (int i = 0; i < dt.Columns.Count; i++)
  88. {
  89. sw.Write(dt.Columns[i].ColumnName);
  90. sw.Write("\t");
  91. }
  92. sw.WriteLine("");
  93. //Table body
  94. for (int i = 0; i < dt.Rows.Count; i++)
  95. {
  96. for (int j = 0; j < dt.Columns.Count; j++)
  97. {
  98. sw.Write(DelQuota(dt.Rows[i][j].ToString()));
  99. sw.Write("\t");
  100. }
  101. sw.WriteLine("");
  102. }
  103. sw.Flush();
  104. sw.Close();
  105. return true;
  106. }
  107. public static string DelQuota(string str)
  108. {
  109. string result = str;
  110. string[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", "/", ":", "/,", "<", ">", "?" };//".",
  111. for (int i = 0; i < strQuota.Length; i++)
  112. {
  113. if (result.IndexOf(strQuota[i]) > -1)
  114. result = result.Replace(strQuota[i], "");
  115. }
  116. return result;
  117. }
  118. //public static bool saveFile(string saveFilePath, DataTable dataTable)
  119. //{
  120. // try
  121. // {
  122. // //// 创建一个 DataTable 对象来存储数据
  123. // //DataTable dataTable = new DataTable("MyData");
  124. // //// 添加列到 DataTable
  125. // //dataTable.Columns.Add("Name", typeof(string));
  126. // //dataTable.Columns.Add("Age", typeof(int));
  127. // //// 向 DataTable 中添加数据行
  128. // //dataTable.Rows.Add("John Doe", 30);
  129. // //dataTable.Rows.Add("Jane Smith", 25);
  130. // // 使用 ClosedXML 组件导出 Excel 文件
  131. // using (XLWorkbook workbook = new XLWorkbook())
  132. // {
  133. // IXLWorksheet worksheet = workbook.AddWorksheet("MySheet");
  134. // int row = 1; //行从1开始
  135. // for (int i = 0; i < dataTable.Rows.Count; i++)
  136. // {
  137. // for (int j = 0; j < dataTable.Columns.Count; j++)
  138. // {
  139. // if (row == 1)//头标题
  140. // worksheet.Cell(1, j + 1).Value = dataTable.Columns[j].ColumnName;
  141. // //内容
  142. // worksheet.Cell(row + 1, j + 1).Value = dataTable.Rows[i][j].ToString();
  143. // // 设置单元格样式
  144. // //worksheet.Cell(row, 2).Style.Font.Bold = true;
  145. // //worksheet.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  146. // }
  147. // row++;
  148. // }
  149. // // 将 Excel 文件保存到磁盘
  150. // //string fileName = @"C:\temp\MyExcelFile.xlsx";
  151. // workbook.SaveAs(saveFilePath);
  152. // // 释放资源
  153. // workbook.Dispose();
  154. // }
  155. // return true;
  156. // }
  157. // catch (Exception ex)
  158. // {
  159. // throw ex;
  160. // }
  161. //}
  162. //public static bool test(string saveFilePath, DataTable dataTable1)
  163. //{
  164. // try
  165. // {
  166. // // 创建一个 DataTable 对象来存储数据
  167. // DataTable dataTable = new DataTable("MyData");
  168. // // 添加列到 DataTable
  169. // dataTable.Columns.Add("Name", typeof(string));
  170. // dataTable.Columns.Add("Age", typeof(int));
  171. // // 向 DataTable 中添加数据行
  172. // dataTable.Rows.Add("John Doe", 30);
  173. // dataTable.Rows.Add("Jane Smith", 25);
  174. // // 使用 ClosedXML 组件导出 Excel 文件
  175. // using (XLWorkbook workbook = new XLWorkbook())
  176. // {
  177. // IXLWorksheet worksheet = workbook.AddWorksheet("MySheet");
  178. // int row = 1;
  179. // foreach (DataRow dataRow in dataTable.Rows)
  180. // {
  181. // worksheet.Cell(row, 1).Value = dataRow["Name"].ToString();
  182. // worksheet.Cell(row, 2).Value = Convert.ToInt32(dataRow["Age"]);
  183. // // 设置单元格样式
  184. // worksheet.Cell(row, 2).Style.Font.Bold = true;
  185. // worksheet.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  186. // row++;
  187. // }
  188. // // 将 Excel 文件保存到磁盘
  189. // //string fileName = @"C:\temp\MyExcelFile.xlsx";
  190. // workbook.SaveAs(saveFilePath);
  191. // // 释放资源
  192. // workbook.Dispose();
  193. // }
  194. // return true;
  195. // }
  196. // catch (Exception ex)
  197. // {
  198. // return false;
  199. // }
  200. //}
  201. }
  202. }
  203. }