【源码链接在文末】
C#,Chart图形,cmd,Cookie&Session,CSV文件转换,DataTable转实体,DBHelper,DecimalUtility及中文大写数字,DLL,Excel操作类
,FTP操作类,H5-微信,Html操作类,INI文件读写类,IP辅助类,Javascript,Json,JSON操作,JS操作,Lib,Mime,Net,NPOI,obj,packages,Path,PDF,Properties,QueryString地址栏参数
,RDLC直接打印帮助类,ResourceManager,RMB,SqlHelper,SQL语句拦截器,URL的操作类,VerifyCode,XML操作类,上传下载,二维码操作类,共用工具类,其他,分词辅助类,分页
,加密解密,压缩解压缩,各种验证帮助类,图片,图片操作类,图片验证码,处理多媒体的公共类,处理枚举类,字符串,对象转换处理,帮助文档,序列化,异步线程,弹出消息类
,数据展示控件绑定数据类,文件操作类,日历,日志,时间戳,时间操作类,条形码,条形码帮助类,条形码转HTML,检测是否有Sql危险字符,正则表
以下是每个类的部分代码,完整代码链接在文末
,Chart图形
/// <summary>
/// 进行作统计图的封装类。
/// </summary>
public class OWCChart
{
#region 属性
private string _phaysicalimagepath;
private string _title;
private string _seriesname;
private int _picwidth;
private int _pichight;
private DataTable _datasource;
private string strCategory;
private string strValue;
public string PhaysicalImagePath
{
set { _phaysicalimagepath = value; }
get { return _phaysicalimagepath; }
}
public string Title
{
set { _title = value; }
get { return _title; }
}
public string SeriesName
{
set { _seriesname = value; }
get { return _seriesname; }
}
public int PicWidth
{
set { _picwidth = value; }
get { return _picwidth; }
}
cmd
public class RunCmd
{
private Process proc = null;
public RunCmd()
{
proc = new Process();
}
public void Exe(string cmd)
{
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
// proc.OutputDataReceived += new DataReceivedEventHandler(sortProcess_OutputDataReceived);
proc.Start();
StreamWriter cmdWriter = proc.StandardInput;
proc.BeginOutputReadLine();
if (!String.IsNullOrEmpty(cmd))
{
cmdWriter.WriteLine(cmd);
}
cmdWriter.Close();
proc.Close();
}
Cookie&Session,
public class CookieHelper
{
/// <summary>
/// 清除指定Cookie
/// </summary>
/// <param name="cookiename">cookiename</param>
public static void ClearCookie(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddYears(-3);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
/// <summary>
/// 获取指定Cookie值
/// </summary>
/// <param name="cookiename">cookiename</param>
/// <returns></returns>
public static string GetCookieValue(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
string str = string.Empty;
if (cookie != null)
{
str =HttpUtility.UrlDecode(cookie.Value);
}
return str;
}
CSV文件转换,
public class CSVHelper
{
/// <summary>
/// CSV转换成DataTable(OleDb数据库访问方式)
/// </summary>
/// <param name="csvPath">csv文件路径</param>
/// <returns></returns>
public static DataTable CSVToDataTableByOledb(string csvPath)
{
DataTable csvdt = new DataTable("csv");
if (!File.Exists(csvPath))
{
throw new FileNotFoundException("csv文件路径不存在!");
}
FileInfo fileInfo = new FileInfo(csvPath);
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileInfo.DirectoryName + ";Extended Properties='Text;'"))
{
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + fileInfo.Name + "]", conn);
adapter.Fill(csvdt);
}
return csvdt;
}
DataTable转实体,
public static class DataTableExtensions
{
public static T ToEntity<T>(this DataTable table) where T : new()
{
T entity = new T();
foreach (DataRow row in table.Rows)
{
foreach (var item in entity.GetType().GetProperties())
{
if (row.Table.Columns.Contains(item.Name))
{
if
DBHelper,
DecimalUtility及中文大写数字,
/// <summary>
/// 转中文大写数字
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ConvertNumToZHUpperCase(decimal value)
{
string[] numList = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
string[] unitList = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };
decimal money = value;
if (money == 0)
{
return "零元整";
}
StringBuilder strMoney = new StringBuilder();
//只取小数后2位
string strNum = decimal.Truncate(money * 100).ToString();
int len = strNum.Length;
int zero = 0;
for (int i = 0; i < len; i++)
{
int num = int.Parse(strNum.Substring(i, 1));
int unitNum = len - i - 1;
if (num == 0)
{
zero++;
if (unitNum == 2 || unitNum == 6 || unitNum == 10)
{
if (unitNum == 2 || zero < 4)
strMoney.Append(unitList[unitNum]);
zero = 0;
}
}
else
{
if (zero > 0)
{
strMoney.Append(numList[0]);
zero = 0;
}
strMoney.Append(numList[num]);
strMoney.Append(unitList[unitNum]);
}
}
if (zero > 0)
strMoney.Append("整");
return strMoney.ToString();
}
Excel操作类
,FTP操作类,
H5-微信,
Html操作类,
/// <summary>
/// Html操作相关类
/// </summary>
public class HtmlHelper
{
#region 私有字段
private static CookieContainer cc = new CookieContainer();
private static string contentType = "application/x-www-form-urlencoded";
private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
private static Encoding encoding = Encoding.GetEncoding("utf-8");
private static int delay = 1000;
private static int maxTry = 300;
private static int currentTry = 0;
#endregion
#region 公有属性
/// <summary>
/// Cookie
/// </summary>
public static CookieContainer CookieContainer
{
get
{
return cc;
}
}
/// <summary>
/// 语言
/// </summary>
public static Encoding Encoding
{
get
{
return encoding;
}
set
{
encoding = value;
}
}
public static int NetworkDelay
{
get
{
Random r = new Random();
return (r.Next(delay, delay * 2));
}
set
{
delay = value;
}
}
public static int MaxTry
{
get
{
return maxTry;
}
set
{
maxTry = value;
}
}
#endregion
#region 获取HTML
/// <summary>
/// 获取HTML
/// </summary>
/// <param name="url">地址</param>
/// <param name="postData">post 提交的字符串</param>
/// <param name="isPost">是否是post</param>
/// <param name="cookieContainer">CookieContainer</param>
public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
{
if (string.IsNullOrEmpty(postData)) return GetHtml(url, cookieContainer);
Thread.Sleep(NetworkDelay);
currentTry++;
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
INI文件读写类,
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp, 255, this.path);
return temp.ToString();
}
IP辅助类,
#region 获得用户IP
/// <summary>
/// 获得用户IP
/// </summary>
public static string GetUserIp()
{
string ip;
string[] temp;
bool isErr = false;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
ip = System.Web.HttpContext.Curr
Javascript,
Json,JSON操作,
//JSON转换类
public class ConvertJson
{
#region 私有方法
/// <summary>
/// 过滤特殊字符
/// </summary>
private static string String2Json(String s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s.ToCharArray()[i];
switch (c)
{
JS操作,
/// <summary>
/// 客户端脚本输出
/// </summary>
public class JsHelper
{
/// <summary>
/// 弹出信息,并跳转指定页面。
/// </summary>
public static void AlertAndRedirect(string message, string toURL)
{
string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
HttpContext.Current.Response.Write(string.Format(js, message, toURL));
HttpContext.Current.Response.End();
}
Lib,
Mime,
Net,
NPOI,
/// <summary>
/// NPOI操作帮助类
/// </summary>
public class NPOIHelper
{
/// <summary>
/// DataTable导出到Excel文件
/// </summary>
/// <param name="dtSource">源DataTable</param>
/// <param name="strHeaderText">表头文本</param>
/// <param name="strFileName">保存位置</param>
public static void Export(DataTable dtSource, string strHeaderText, string strFileName)
{
using (MemoryStream ms = Export(dtSource, strHeaderText))
{
obj,
packages,
Path,
/// <summary>
/// 网站路径操作类
/// </summary>
public static class WebSitePathHelper
{
/// <summary>
///
/// </summary>
public enum SortType
{
/// <summary>
///
/// </summary>
Photo = 1,
/// <summary>
///
/// </summary>
Article = 5,
/// <summary>
///
/// </summary>
Diary = 7,
/// <summary>
///
/// </summary>
Pic = 2,
/// <summary>
///
/// </summary>
Music = 6,
/// <summary>
///
/// </summary>
AddressList = 4,
/// <summary>
///
/// </summary>
Favorite = 3,
}
#region 根据给出的相对地址获取网站绝对地址
/// <summary>
/// 根据给出的相对地址获取网站绝对地址
/// </summary>
/// <param name="localPath">相对地址</param>
/// <returns>绝对地址</returns>
public static string GetWebPath(string localPath)
{
string path = HttpContext.Current.Request.ApplicationPath;
string thisPath;
string thisLocalPath;
//如果不是根目录就加上"/" 根目录自己会加"/"
if (path != "/")
{
thisPath = path + "/";
}
else
{
thisPath = path;
}
if (localPath.StartsWith("~/"))
{
PDF,
/// <summary>
/// PDF文档操作类
/// </summary>
//------------------------------------调用--------------------------------------------
//PDFHelper pdf = new PDFHelper();
//pdf.Open(new FileStream(path, FileMode.Create));
//pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
//pdf.AddParagraph("测试文档(生成时间:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
//pdf.Close();
//-------------------------------------------------------------------------------------
public class PDFHelper
{
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public PDFHelper()
{
rect = PageSize.A4;
document = new Document(rect);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="type">页面大小(如"A4")</param>
public PDFHelper(string type)
{
SetPageSize(type);
document = new Document(rect);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="type">页面大小(如"A4")</param>
/// <param name="marginLeft">内容距左边框距离</param>
/// <param name="marginRight">内容距右边框距离</param>
/// <param name="marginTop">内容距上边框距离</param>
/// <param name="marginBottom">内容距下边框距离</param>
public PDFHelper(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
{
SetPageSize(type);
document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
}
#endregion
#region 私有字段
private Font font;
private Rectangle rect; //文档大小
private Document document;//文档对象
private BaseFont basefont;//字体
#endregion
【源码链接】