using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace GeBoShi.SysCtrl
{
public static class Utils
{
private static Regex RegNumber = new Regex("^[0-9]+$"); //匹配 整数(不可以带正负号)
private static Regex RegDecimal = new Regex("^(-?\\d+)(\\.\\d+)?$"); //浮点 (可以带正负号)
public static bool IsDecimal(object inputData)
{
if (inputData == null) return false;
Match m = RegDecimal.Match(inputData.ToString());
return m.Success;
}
public static bool IsNumber(object inputData)
{
if (inputData == null) return false;
Match m = RegNumber.Match(inputData.ToString());
return m.Success;
}
private static double ContrastLow = 0.8;
private static double ContrastTop = 1.2;
///
/// 获取对比度百分比 0-100 对应0.6-1.4
///
///
///
public static double ContrastToPercent(double val)
{
if (val < ContrastLow)
return 0;
else if (val > ContrastTop)
return 100;
double temp = 100 / (ContrastTop - ContrastLow);
return Math.Round(temp * (val - ContrastLow), 2);
}
public static double PercentToContrast(double val)
{
double tt = 1;
if (val > 100)
tt = 100;
else if (val < 0)
tt = 0;
else
tt = val;
double temp = tt / 100 * (ContrastTop - ContrastLow);
return temp + ContrastLow;
}
public class ExcelUtil
{
public static DataTable ConvertToDataTable(IList data, List lstColName)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
{
if (lstColName.Contains(prop.Name))
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
if (lstColName.Contains(prop.Name))
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
}
table.Rows.Add(row);
}
return table;
}
public static bool DataTable2CSV(string saveFilePath, System.Data.DataTable dt)
{
//MessageBox.Show(AbosultedFilePath);
System.IO.FileStream fs = new FileStream(saveFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
//Tabel header
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].ColumnName);
sw.Write("\t");
}
sw.WriteLine("");
//Table body
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
sw.Write(DelQuota(dt.Rows[i][j].ToString()));
sw.Write("\t");
}
sw.WriteLine("");
}
sw.Flush();
sw.Close();
return true;
}
public static string DelQuota(string str)
{
string result = str;
string[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", "/", ":", "/,", "<", ">", "?" };//".",
for (int i = 0; i < strQuota.Length; i++)
{
if (result.IndexOf(strQuota[i]) > -1)
result = result.Replace(strQuota[i], "");
}
return result;
}
//public static bool saveFile(string saveFilePath, DataTable dataTable)
//{
// try
// {
// //// 创建一个 DataTable 对象来存储数据
// //DataTable dataTable = new DataTable("MyData");
// //// 添加列到 DataTable
// //dataTable.Columns.Add("Name", typeof(string));
// //dataTable.Columns.Add("Age", typeof(int));
// //// 向 DataTable 中添加数据行
// //dataTable.Rows.Add("John Doe", 30);
// //dataTable.Rows.Add("Jane Smith", 25);
// // 使用 ClosedXML 组件导出 Excel 文件
// using (XLWorkbook workbook = new XLWorkbook())
// {
// IXLWorksheet worksheet = workbook.AddWorksheet("MySheet");
// int row = 1; //行从1开始
// for (int i = 0; i < dataTable.Rows.Count; i++)
// {
// for (int j = 0; j < dataTable.Columns.Count; j++)
// {
// if (row == 1)//头标题
// worksheet.Cell(1, j + 1).Value = dataTable.Columns[j].ColumnName;
// //内容
// worksheet.Cell(row + 1, j + 1).Value = dataTable.Rows[i][j].ToString();
// // 设置单元格样式
// //worksheet.Cell(row, 2).Style.Font.Bold = true;
// //worksheet.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
// }
// row++;
// }
// // 将 Excel 文件保存到磁盘
// //string fileName = @"C:\temp\MyExcelFile.xlsx";
// workbook.SaveAs(saveFilePath);
// // 释放资源
// workbook.Dispose();
// }
// return true;
// }
// catch (Exception ex)
// {
// throw ex;
// }
//}
//public static bool test(string saveFilePath, DataTable dataTable1)
//{
// try
// {
// // 创建一个 DataTable 对象来存储数据
// DataTable dataTable = new DataTable("MyData");
// // 添加列到 DataTable
// dataTable.Columns.Add("Name", typeof(string));
// dataTable.Columns.Add("Age", typeof(int));
// // 向 DataTable 中添加数据行
// dataTable.Rows.Add("John Doe", 30);
// dataTable.Rows.Add("Jane Smith", 25);
// // 使用 ClosedXML 组件导出 Excel 文件
// using (XLWorkbook workbook = new XLWorkbook())
// {
// IXLWorksheet worksheet = workbook.AddWorksheet("MySheet");
// int row = 1;
// foreach (DataRow dataRow in dataTable.Rows)
// {
// worksheet.Cell(row, 1).Value = dataRow["Name"].ToString();
// worksheet.Cell(row, 2).Value = Convert.ToInt32(dataRow["Age"]);
// // 设置单元格样式
// worksheet.Cell(row, 2).Style.Font.Bold = true;
// worksheet.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
// row++;
// }
// // 将 Excel 文件保存到磁盘
// //string fileName = @"C:\temp\MyExcelFile.xlsx";
// workbook.SaveAs(saveFilePath);
// // 释放资源
// workbook.Dispose();
// }
// return true;
// }
// catch (Exception ex)
// {
// return false;
// }
//}
}
}
}