using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MaterialTraceability.Common { public class MsgUtil { public static string HexStringToASCII(string hexstring) { byte[] buff = new byte[(hexstring.Length - 8)/ 2]; int index = 0; for (int i = 0; i < hexstring.Length - 8; i += 2) { buff[index] = Convert.ToByte(hexstring.Substring(i, 2), 16); ++index; } string result = Encoding.Default.GetString(buff); return result; } public static string StringReverse(string hexstring) { char[] cs = new char[20]; cs = hexstring.ToCharArray(); if (hexstring.Length%2 == 0) { for (int i = 0; i < cs.Length; i += 2) { Array.Reverse(cs, i, 2); } } else { for (int i = 0; i < cs.Length-1; i += 2) { Array.Reverse(cs, i, 2); } } string res = new string(cs); return res; } /// /// 计算时间差 /// /// /// /// public static string ExecDateTime(DateTime BeginTime, DateTime EndTime) { string dateDiff = null; TimeSpan ts1 = new TimeSpan(BeginTime.Ticks); TimeSpan ts2 = new TimeSpan(EndTime.Ticks); TimeSpan ts3 = ts1.Subtract(ts2).Duration(); dateDiff = ts3.Days.ToString() + "天" + ts3.Hours.ToString() + "小时" + ts3.Minutes.ToString() + "分钟" + ts3.Seconds.ToString() + "秒"; return dateDiff; } public static string ExecDateTime(DateTime BeginTime, DateTime EndTime, out double interval) { string dateDiff = null; TimeSpan ts1 = new TimeSpan(BeginTime.Ticks); TimeSpan ts2 = new TimeSpan(EndTime.Ticks); TimeSpan ts3 = ts1.Subtract(ts2).Duration(); dateDiff = ts3.Days.ToString() + "天" + ts3.Hours.ToString() + "小时" + ts3.Minutes.ToString() + "分钟" + ts3.Seconds.ToString() + "秒"; interval = double.Parse(ts3.TotalHours.ToString()); return dateDiff; } /// /// 计算接口调用耗时 /// /// /// /// public static string ElapsedTime(DateTime BeginTime, DateTime EndTime) { string dateDiff = null; TimeSpan ts1 = new TimeSpan(BeginTime.Ticks); TimeSpan ts2 = new TimeSpan(EndTime.Ticks); TimeSpan ts3 = ts1.Subtract(ts2).Duration(); dateDiff = ts3.Milliseconds.ToString() + "ms"; return dateDiff; } protected static int WriteRow(DataRow dr, ISheet sheet, int irows) { int Result = 0; IRow row = sheet.CreateRow(irows); for (int icell = 0; icell < dr.Table.Columns.Count; icell++) { if (dr[icell] == null) { row.CreateCell(icell).SetCellValue(""); } if (dr[icell] is DateTime) { row.CreateCell(icell).SetCellValue(((DateTime)dr[icell]).ToString("yyyy-MM-dd HH:mm:ss").Replace(" 00:00:00", "")); continue; } if (dr[icell] is double) { row.CreateCell(icell).SetCellValue((double)dr[icell]); continue; } if (dr[icell] is decimal) { string s = dr[icell].ToString(); row.CreateCell(icell).SetCellValue(double.Parse(s)); continue; } if (dr[icell] is int) { row.CreateCell(icell).SetCellValue((int)dr[icell]); continue; } row.CreateCell(icell).SetCellValue(dr[icell].ToString()); } Result++; return Result; } public static void WriteMesLog(string filename,string resource, DataTable dt) { if (!Directory.Exists(filename)) { Directory.CreateDirectory(filename); } //filename = filename + @"\" + resource + "_log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls"; filename = filename+DateTime.Now.ToString("yyyy-MM-dd") + ".xls"; if (!File.Exists(filename)) // 返回bool类型,存在返回true,不存在返回false { System.IO.StreamWriter sw = System.IO.File.AppendText(filename); sw.Close(); HSSFWorkbook hssfworkbook = new HSSFWorkbook(); ISheet sheet = hssfworkbook.CreateSheet(resource +"_log_"+ DateTime.Now.ToString("yyyy-MM-dd")); for (int idr = 0; idr < dt.Rows.Count; idr++) { WriteRow(dt.Rows[idr], sheet, idr);//写入每一行 } FileStream fout = new FileStream(filename, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);//写入流 fout.Flush(); hssfworkbook.Write(fout);//写入文件 hssfworkbook = null; fout.Close(); } else { //如果存在文件 FileStream fileReader = new FileStream(filename, FileMode.Open, FileAccess.Read);//读取流 IWorkbook workbook = new HSSFWorkbook(fileReader); ISheet sheet = workbook.GetSheetAt(0); int count = sheet.LastRowNum + 1; for (int idr = 0; idr < dt.Rows.Count; idr++) { WriteRow(dt.Rows[idr], sheet, count);//写入每一行 } FileStream fout = new FileStream(filename, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);//写入流 fout.Flush(); workbook.Write(fout);//写入文件 workbook = null; fout.Close(); } } /// /// 解密 /// 孙本强 @ 2013-04-03 13:12:02 /// /// The SRC. /// The key. /// The encoding. /// /// public static string DecryptString(string src, string key, Encoding encoding) { int KeyLen; int KeyPos; int offset; string dest; int SrcPos; int SrcAsc; int TmpSrcAsc; KeyLen = key.Length; if (KeyLen == 0) { key = "Mesnac"; } KeyPos = 0; SrcPos = 0; SrcAsc = 0; if (src.Length <= 2) { return string.Empty; } dest = ""; offset = Convert.ToInt32(src.Substring(0, 2), 16); SrcPos = 2; while (SrcPos < src.Length) { SrcAsc = Convert.ToInt32(src.Substring(SrcPos, 2), 16); if (KeyPos < KeyLen) { KeyPos = KeyPos + 1; } else { KeyPos = 0; } TmpSrcAsc = SrcAsc ^ (int)key[KeyPos]; //异或 if (TmpSrcAsc <= offset) { TmpSrcAsc = 255 + TmpSrcAsc - offset; } else { TmpSrcAsc = TmpSrcAsc - offset; } dest = dest + (char)TmpSrcAsc; offset = SrcAsc; SrcPos = SrcPos + 2; } return dest; } } }