ldj/WcfService2/lables/Utils.cs

182 lines
6.0 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Web;
namespace DeiNiu.Utils
{
public enum enumLabelPickType { pick = 0, seed, tran }
public class Util
{
public static string Encrypt(string password)//加密函数
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
}
/// <summary>
/// 组合in sql,return like " 1,2,3"
/// </summary>
/// <param name="al"></param>
/// <returns></returns>
public static string buildWhereIntIn(ArrayList al)
{
string whereString = string.Empty;
for (int i = 0; i < al.Count; i++)
{
whereString += "'"+ al[i] + "',";
}
if (whereString.EndsWith(","))
{
whereString = whereString.Substring(0, whereString.Length - 1);
}
return whereString;
}
public static DataSet Excel2DataSet(string filepath)
{
DataSet ds = new DataSet();
ArrayList SheeNames = ExcelSheetName(filepath);
for(int i=0;i<SheeNames.Count;i++)
{
DataTable dt = (ExcelDataSource(filepath, SheeNames[i].ToString())).Tables[0].Copy();
dt.TableName = SheeNames[i].ToString();
dt.TableName = dt.TableName.Substring(0, dt.TableName.IndexOf("$"));
ds.Tables.Add(dt);
}
return ds;
}
//该方法实现从Excel中导出数据到DataSet中其中filepath为Excel文件的绝对路径 sheetname为excel文件中的表名
public static DataSet ExcelDataSource(string filepath, string sheetname)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);
DataSet ds = new DataSet();
oada.Fill(ds);
conn.Close();
return ds;
}
//获得Excel中的所有sheetname。
public static ArrayList ExcelSheetName(string filepath)
{
ArrayList al = new ArrayList();
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable sheetNames = conn.GetOleDbSchemaTable
(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
conn.Close();
foreach (DataRow dr in sheetNames.Rows)
{
al.Add(dr[2]);
}
return al;
}
public static DataView GetTopDataViewRows(DataView dv, int n)
       {
           DataTable dt = dv.Table.Clone();
   
           for (int i = 0; i < n - 1; i++)
           {
               if (i >= dv.Count)
               {
                   break;
             }
             dt.ImportRow(dv[i].Row);
          }
        return new DataView(dt, dv.RowFilter, dv.Sort,  dv.RowStateFilter);
     }
/// <summary>
/// 读取配置文件某项的值
/// </summary>
/// <param name="key">appSettings的key</param>
/// <returns>appSettings的Value</returns>
//public static string GetConfig(string key)
//{
//string _value = string.Empty;
//System.Configuration.ConfigurationManager config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//if (config.AppSettings.Settings[key] != null)
//{
// _value = config.AppSettings.Settings[key].Value;
//}
//return _value;
//}
//条形码转换
public static string getCode128(string codeIn)
{
//string str ="";
string result;
int checksum = 104;
for (int i = 0; i < codeIn.Length; i++)
{
if (codeIn[i] >= 32)
{
checksum += (codeIn[i] - 32) * (i + 1);
}
else
{
checksum += (codeIn[i] + 64) * (i + 1);
}
}
checksum = checksum % 103;
if (checksum < 95)
{
checksum += 32;
}
else
{
checksum += 100;
}
result = Convert.ToChar(204) + codeIn +
Convert.ToChar(checksum) + Convert.ToChar(206);
return result;
}
public static string getLightKey(int color,int labelId, int address)
{
return string.Format("{0}-{1}-{2}", color, labelId, address);
}
public static Dictionary<int,string> convertEnumToDic(Type enumType){
Dictionary<int, string> dic = new Dictionary<int, string>();
foreach (int myCode in Enum.GetValues(enumType))
{
string strName = Enum.GetName(enumType, myCode);//获取名称
string strVaule = myCode.ToString();//获取值
dic[myCode] = strName;
}
return dic;
}
public static Dictionary<string, bool> WAVE_CURRENT_PICK_STATUS = new Dictionary<string, bool>();
public static Dictionary<string, bool> WAVE_CURRENT_LIGHTS_STATUS = new Dictionary<string, bool>();
}
}