| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889 |
- using System;
- using System.Data;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Text.RegularExpressions;
- using System.Reflection;
- using System.Diagnostics;
- using System.IO;
- using CoreFS.CA06;
- using Infragistics.Win.UltraWinGrid;
- namespace Core.Mes.Client.Comm
- {
- public class Globals
- {
-
- /// <summary>
- /// 字符串转是否能转换成非负数
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool Is_Below_zero(string str)
- {
- try
- {
- if (IsDouble(str) && double.Parse(str) > 0)
- return true;
- else
- return false;
- }
- catch
- {
- return false;
- }
- }
-
- /// <summary>
- /// 保留指定位数小数
- /// </summary>
- /// <param name="sVal"></param>
- /// <param name="Digits"></param>
- /// <returns></returns>
- public static string ValWithDigits(string sVal, int Digits)
- {
- try
- {
- decimal d = decimal.Parse(sVal);
- string sFormat = "0";
- if (Digits > 0)
- {
- sFormat += ".";
- for (int i = 0; i < Digits; i++)
- sFormat += "0";
- }
- return d.ToString(sFormat);
- }
- catch
- {
- return "";
- }
- }
- /// <summary>
- /// 单元格复制内容
- /// </summary>
- /// <param name="strMessage"></param>
- public static void cellCopy(string strMessage)
- {
- Clipboard.SetDataObject(strMessage, false);
- }
- public static void CopyDataToDataTable(DataTable source, DataTable dest, bool clearExists)
- {
- if (source != null && dest != null)
- {
- if (clearExists)
- {
- dest.Rows.Clear();
- }
- DataRow row;
- for (int i = 0; i < source.Rows.Count; i++)
- {
- row = dest.NewRow();
- for (int j = 0; j < source.Columns.Count; j++)
- {
- try
- {
- if (dest.Columns.Contains(source.Columns[j].ColumnName))
- {
- row[source.Columns[j].ColumnName] = source.Rows[i][j];
- }
- }
- catch { }
- }
- dest.Rows.Add(row);
- }
- }
- }
- public static bool ContaisColumn(ref UltraGrid ultraGrid, string strColumn)
- {
- try
- {
- if (ultraGrid == null || ultraGrid.DisplayLayout.Bands[0].Columns.Count == 0 || string.IsNullOrEmpty(strColumn))
- {
- return false;
- }
- foreach (UltraGridColumn col in ultraGrid.DisplayLayout.Bands[0].Columns)
- {
- if (col.Key.Equals(strColumn))
- {
- return true;
- }
- }
- }
- catch { }
- return false;
- }
- public static void LocateRecord(ref UltraGrid ultraGrid, ref Hashtable Columns, out UltraGridRow row)
- {
- row = null;
- try
- {
- if (ultraGrid == null || ultraGrid.Rows.Count == 0)
- {
- return;
- }
- string strColumn = "";
- object value = null;
- ArrayList alistCols = new ArrayList(Columns.Keys);
- for (int i = 0; i < alistCols.Count; i++)
- {
- strColumn = alistCols[i].ToString();
- if (!ContaisColumn(ref ultraGrid, strColumn))
- {
- return;
- }
- }
- bool Found = false;
- UltraGridRow temp = null;
- for (int i = 0; i < ultraGrid.Rows.Count; i++)
- {
- Found = true; ;
- temp = ultraGrid.Rows[i];
- for (int j = 0; j < alistCols.Count; j++)
- {
- strColumn = alistCols[j].ToString();
- value = Columns[strColumn];
- if (!temp.Cells[strColumn].Value.Equals(value))
- {
- Found = false;
- break;
- }
- }
- if (Found)
- {
- ultraGrid.ActiveRow = temp;
- row = temp;
- return;
- }
- }
- }
- catch (Exception ex) { string str = ex.Message; }
- }
- /// <summary>
- /// 清除UltraGrid的DataSource的数据
- /// </summary>
- /// <param name="ulGrid"></param>
- public static void ClearGridDataSourceData(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
- {
- if (ulGrid.DataSource is DataSet)
- {
- for (int i = ((DataSet)ulGrid.DataSource).Tables.Count - 1; i >= 0; i--)
- {
- ((DataSet)ulGrid.DataSource).Tables[i].Rows.Clear();
- }
- }
- }
- /// <summary>
- /// 全选选中的子行
- /// </summary>
- /// <param name="Gride">UltraGrid</param>
- /// <param name="ColumnName">复选框列名</param>
- /// <param name="BoolValue">值</param>
- public static void doCheckedAllChildRow(Infragistics.Win.UltraWinGrid.UltraGrid Gride, string ColumnName, string BoolValue)
- {
- Infragistics.Win.UltraWinGrid.UltraGridRow ulRow;
- if (Gride.ActiveRow.ParentRow == null)
- {
- ulRow = Gride.ActiveRow;
- }
- else
- {
- ulRow = Gride.ActiveRow.ParentRow;
- }
- int Index = ulRow.Index;
- for (int i = 0; i < Gride.Rows[Index].ChildBands[0].Rows.Count; i++)
- {
- Gride.Rows[Index].ChildBands[0].Rows[i].Cells[ColumnName].Value = BoolValue;
- }
- }
- /// <summary>
- /// 选中行中指定的子行
- /// </summary>
- /// <param name="Gride">UltraGrid</param>
- /// <param name="ColumnName">复选框列名</param>
- /// <param name="ColumnName2">需要与子行集合比对的列名</param>
- /// <param name="sArgs">需要选中的子行集合</param>
- public static void doCheckedAppointChildRow(RowEventArgs e, string ColumnName,string ColumnName2, string[] sArgs)
- {
- for (int i = 0; i < e.Row.ChildBands[0].Rows.Count; i++)
- {
- for (int t = 0; t < sArgs.Length; t++)
- {
- if (e.Row.ChildBands[0].Rows[i].Cells[ColumnName2].Value.ToString() == sArgs[t].ToString())
- {
- e.Row.ChildBands[0].Rows[i].Cells[ColumnName].Value = "True";
- }
- }
- }
- }
- /// <summary>
- /// 获取子行中选中的列ID(取的列名为ID_)
- /// </summary>
- /// <param name="Gride">UltraGrid</param>
- /// <param name="ColumnName">复选框列名</param>
- public static string doCheckedRowValue(Infragistics.Win.UltraWinGrid.UltraGrid Gride, string ColumnName)
- {
- Infragistics.Win.UltraWinGrid.UltraGridRow ulRow;
- if (Gride.ActiveRow.ParentRow == null)
- {
- ulRow = Gride.ActiveRow;
- }
- else
- {
- ulRow = Gride.ActiveRow.ParentRow;
- }
- int Index = ulRow.Index;
- string IDString = "";
- for (int i = 0; i < Gride.Rows[Index].ChildBands[0].Rows.Count; i++)
- {
- if (Gride.Rows[Index].ChildBands[0].Rows[i].Cells[ColumnName].Text == "True")
- {
- IDString += Gride.Rows[Index].ChildBands[0].Rows[i].Cells["ID_"].Value + ",";
- }
- }
- if (IDString != "")
- {
- IDString = IDString.Substring(0, IDString.Length - 1);
- }
- return IDString;
- }
- /// <summary>
- /// 选中或取消选中,当前Gride中的所有行
- /// </summary>
- /// <param name="Gride">UltraGrid</param>
- /// <param name="ColumnName">复选框列名</param>
- /// <param name="BoolValue">值</param>
- public static void doCheckeAllRow(Infragistics.Win.UltraWinGrid.UltraGrid Gride, string ColumnName, string BoolValue)
- {
- if (Gride != null)
- {
- for (int i = 0; i < Gride.Rows.Count; i++)
- {
- Gride.Rows[i].Cells[ColumnName].Value = BoolValue;
-
- }
- }
- }
- #region "数据验证"
- /// <summary>
- /// 获取字符串字节长度
- /// </summary>
- /// <param name="strVal">需要获取长度的字符串</param>
- /// <returns>字符串字节长度</returns>
- public static int GetStrBytesLength(string strVal)
- {
- int lenTotal = 0;
- int n = strVal.Length;
- string strWord = "";
- int asc;
- for (int i = 0; i < n; i++)
- {
- strWord = strVal.Substring(i, 1);
- asc = Convert.ToChar(strWord);
- if (asc < 0 || asc > 127)
- lenTotal = lenTotal + 2;
- else
- lenTotal = lenTotal + 1;
- }
- return lenTotal;
- }
- /// <summary>
- /// 验证DataTable是否为空
- /// </summary>
- /// <param name="dt">DataTable数据</param>
- /// <returns>true为空值</returns>
- public static bool IsNullTable(DataTable dt)
- {
- if (dt == null || dt.Rows.Count <= 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 校验字符串是否只包含字母与数字
- /// </summary>
- /// <param name="toVerified">需要校验的字符串</param>
- /// <returns>true表示符合要求,false表示不符合要求</returns>
- public static bool IsOnlyLetterAndDigit(string toVerified)
- {
- Regex rx = new Regex(@"^[a-zA-Z0-9-]*$");
- return rx.IsMatch(toVerified.Trim(), 0);
- }
- /// <summary>
- /// 检验是否是整数
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为整数:true是整数,false非整数</returns>
- public static bool IsInt(string str)
- {
- Regex rx = new Regex(@"^[0123456789]+$");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验是否是整数
- /// </summary>
- /// <param name="sVal"></param>
- /// <returns></returns>
- public static bool IsInt32(string sVal)
- {
- try
- {
- Int32.Parse(sVal);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 校验是否为正的浮点数
- /// </summary>
- /// <param name="price">需要检验的字符串</param>
- /// <returns>是否为正浮点,是返回true,否则返回false</returns>
- public static bool IsFloat(string str)
- {
- Regex rx = new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);
- return rx.IsMatch(str.Trim());
- }
- /// <summary>
- /// 验证浮点数
- /// </summary>
- /// <param name="sVal"></param>
- /// <returns></returns>
- public static bool IsDouble(string sVal)
- {
- try
- {
- Double.Parse(sVal);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 检验是否为数字
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为数字:true代表是,false代表否</returns>
- public static bool IsNumber(string str)
- {
- Regex rx = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验字符串是否为日期时间
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为日期时间:true代表是,false代表否</returns>
- public static bool IsNotDateTime(string str)
- {
- DateTime dt = new DateTime();
- return (!(DateTime.TryParse(str, out dt)));
- }
- /// <summary>
- /// 检验字符串是否为邮政编码
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为邮政编码:true代表是,false代表否</returns>
- public static bool IsPostCode(string str)
- {
- Regex rx = new Regex(@"^[0123456789]{6}$");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验字符串是否为身份证号
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为身份证号:true代表是,false代表否</returns>
- public static bool IsCode(string str)
- {
- Regex rx = new Regex(@"^[0123456789]{15,18}$");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验字符串是否为电子邮件
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为电子邮件:true代表是,false代表否</returns>
- public static bool IsEMail(string str)
- {
- Regex rx = new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验字符串是否为中国地区的电话号码
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为中国地区的电话号码:true代表是,false代表否</returns>
- public static bool IsPhoneNumber(string str)
- {
- Regex rx = new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验字符串是否为汉字
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为汉字:true代表是,false代表否</returns>
- public static bool IsChinese(string str)
- {
- Regex rx = new Regex(@"[\u4e00-\u9fbb]+$");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验字符串是否为双字节字符(包括汉字)
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为双字节字符:true代表是,false代表否</returns>
- public static bool IsDoubleByteChar(string str)
- {
- Regex rx = new Regex(@"[^x00-xff]");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验字符串是否为URL地址
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为URL地址:true代表是,false代表否</returns>
- public static bool IsURLAddress(string str)
- {
- Regex rx = new Regex(@"[a-zA-z]+://[^s]*");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 检验字符串是否为IP地址
- /// </summary>
- /// <param name="str">需要检验的字符串</param>
- /// <returns>是否为IP地址:true代表是,false代表否</returns>
- public static bool IsIPAddress(string str)
- {
- Regex rx = new Regex(@"d+.d+.d+.d+");
- return rx.IsMatch(str);
- }
- /// <summary>
- /// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确)
- /// </summary>
- /// <param name="toEvaluate">指定的要被处理的字符串</param>
- /// <returns>清除HTML标签后的字符串</returns>
- public static string RemoveHtmlTags(string toEvaluate)
- {
- Regex rx = new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase);
- return rx.Replace(toEvaluate, "");
- }
- /// <summary>
- /// 判断输入的字符串是否完全匹配正则
- /// </summary>
- /// <param name="RegexExpression">正则表达式</param>
- /// <param name="str">待判断的字符串</param>
- /// <returns></returns>
- public static bool IsValiable(string RegexExpression, string str)
- {
- bool blResult = false;
- Regex rep = new Regex(RegexExpression, RegexOptions.IgnoreCase);
- //blResult = rep.IsMatch(str);
- Match mc = rep.Match(str);
- if (mc.Success)
- {
- if (mc.Value == str) blResult = true;
- }
- return blResult;
- }
- /// <summary>
- /// 判断DataSet是否是空值
- /// </summary>
- /// <param name="ds">DataSet数据</param>
- /// <returns>true 空值</returns>
- public static bool IsNullData(DataSet ds)
- {
- if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 值比对 sSign:符号,sMin:最小值,sMax:最大值,sRealVal:实际值
- /// </summary>
- /// <param name="sSign"></param>
- /// <param name="sMin"></param>
- /// <param name="sMax"></param>
- /// <param name="sRealVal"></param>
- /// <returns></returns>
- public static bool ValIsEligible(string sSign, string sMin, string sMax, string sRealVal)
- {
- try
- {
- switch (sSign)
- {
- case ">":
- return (double.Parse(sMin) < double.Parse(sRealVal));
- case ">=":
- return (double.Parse(sMin) <= double.Parse(sRealVal));
- case "=":
- if (Globals.IsDouble(sMin))
- return (double.Parse(sMin) == double.Parse(sRealVal));
- else
- return (sMin == sRealVal);
- case "<":
- return (double.Parse(sMax) > double.Parse(sRealVal));
- case "<=":
- return (double.Parse(sMax) >= double.Parse(sRealVal));
- default:
- return true;
- }
- }
- catch
- {
- return false;
- }
- }
- #endregion
- /// <summary>
- /// 数字转换成中文数字
- /// </summary>
- /// <param name="strNum"></param>
- /// <returns></returns>
- public static string ConvertNumberToChinese(string strNum)
- {
- string[] Nums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
- string[] Digits = { "", "拾", "佰", "仟" };
- string[] Units = { "元", "万", "亿", "万亿" };
- string x, y, z = "";
- if (strNum.Length > 2)
- {
- x = strNum.Substring(0, strNum.Length - 2);
- y = strNum.Substring(strNum.Length - 2, 2);
- }
- else
- {
- x = "";
- y = strNum;
- }
- if (y.Length == 2)
- {
- int n = Convert.ToInt32(y.Substring(0, 1));
- z = Nums[n] + "角";
- }
- if (y.Length > 0)
- {
- int n = Convert.ToInt32(y.Substring(y.Length - 1, 1));
- z += Nums[n] + "分";
- }
- if (y.Length == 0)
- {
- if (x.Length == 0)
- z = "零元整";
- else
- z = "整";
- }
- string S = ""; //返回值
- int p = 0; //字符位置指针
- int m = x.Length % 4; //取模
- // 四位一组得到组数
- int k = (m > 0 ? x.Length / 4 + 1 : x.Length / 4);
- // 外层循环在所有组中循环
- // 从左到右 高位到低位 四位一组 逐组处理
- // 每组最后加上一个单位: "[万亿]","[亿]","[万]"
- for (int i = k; i > 0; i--)
- {
- int L = 4;
- if (i == k && m != 0)
- {
- L = m;
- }
- // 得到一组四位数 最高位组有可能不足四位
- string s = x.Substring(p, L);
- int l = s.Length;
- // 内层循环在该组中的每一位数上循环 从左到右 高位到低位
- for (int j = 0; j < l; j++)
- {
- //处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
- int n = Convert.ToInt32(s.Substring(j, 1));
- if (n == 0)
- {
- if (j < l - 1
- && Convert.ToInt32(s.Substring(j + 1, 1)) > 0 //后一位(右低)
- && !S.EndsWith(Nums[n]))
- {
- S += Nums[n];
- }
- }
- else
- {
- //处理 1013 一千零"十三", 1113 一千一百"一十三"
- if (!(n == 1 && (S.EndsWith(Nums[0]) | S.Length == 0) && j == l - 2))
- {
- S += Nums[n];
- }
- S += Digits[l - j - 1];
- }
- }
- p += L;
- // 每组最后加上一个单位: [万],[亿] 等
- if (i < k) //不是最高位的一组
- {
- if (Convert.ToInt32(s) != 0)
- {
- //如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
- S += Units[i - 1];
- }
- }
- else
- {
- //处理最高位的一组,最后必须加上单位
- S += Units[i - 1];
- }
- }
- return S + z;
- }
- /// <summary>
- /// 时间计算返回分
- /// </summary>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <returns></returns>
- public static int caculateTime(DateTime startTime, DateTime endTime)
- {
- int lStayDuration = 0;
- TimeSpan odtSpan;
- if (endTime > startTime)
- {
- odtSpan = endTime - startTime;
- lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
- }
- else if (startTime != endTime)
- {
- if (startTime > DateTime.Now)
- lStayDuration = 0;
- else
- {
- odtSpan = DateTime.Now - startTime;
- lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
- }
- }
- return lStayDuration;
- }
- /// <summary>
- /// 时间计算返回秒
- /// </summary>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <returns></returns>
- public static int caculateTimeSeconds(DateTime startTime, DateTime endTime)
- {
- int lStayDuration = 0;
- TimeSpan odtSpan;
- if (endTime > startTime)
- {
- odtSpan = endTime - startTime;
- lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalSeconds));
- }
- else if (startTime != endTime)
- {
- if (startTime > DateTime.Now)
- lStayDuration = 0;
- else
- {
- odtSpan = DateTime.Now - startTime;
- lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalSeconds));
- }
- }
- return lStayDuration;
- }
- /// <summary>
- /// 获取两个时间段的差值
- /// </summary>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <returns></returns>
- public static int JudgeTime(DateTime startTime, DateTime endTime)
- {
- int lStayDuration = 0;
- TimeSpan odtSpan;
- if (endTime > startTime)
- {
- odtSpan = endTime - startTime;
- lStayDuration = Convert.ToInt32(System.Math.Round(odtSpan.TotalMinutes));
- }
- return lStayDuration;
- }
- /// <summary>
- /// 将ultragrid的数据导出到Excel中
- /// </summary>
- /// <param name="ulGrid">要导出数据的ulGrid名称</param>
- /// <param name="sFileName">Excel文件名</param>
- public static void ulGridToExcel(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid, string sFileName)
- {
- try
- {
- Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter ulGridExt = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
- ulGridExt.CellExporting += new Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventHandler(ultraGridExcelExporter1_CellExporting);
- System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- if (ulGrid.Rows.Count == 0)
- {
- MessageBox.Show("没有数据!", "提示");
- return;
- }
- saveFileDialog1.FileName = sFileName + DateTime.Now.ToString("yyMMdd") + ".xls";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string sFullName = saveFileDialog1.FileName;
- ulGridExt.Export(ulGrid, sFullName);
- ProcessStartInfo p = new ProcessStartInfo(sFullName);
- p.WorkingDirectory = Path.GetDirectoryName(sFullName);
- Process.Start(p);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("导出失败,原因:" + ex.Message);
- }
- }
- /// <summary>
- /// 当Grid导出EXCEL时,列字段中的值为编码时,将编码转换成中文
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public static void ultraGridExcelExporter1_CellExporting(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventArgs e)
- {
- try
- {
- if (e.GridColumn.RowLayoutColumnInfo.LabelPosition == Infragistics.Win.UltraWinGrid.LabelPosition.LabelOnly)
- {
- e.Cancel = true;
- }
- if (e.GridColumn.EditorControl != null || e.GridColumn.ValueList != null)
- {
- e.Value = e.GridRow.GetCellText(e.GridColumn);
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// 清除Grid的列过滤
- /// </summary>
- /// <param name="ulGrid">Grid名称</param>
- public static void ClearUlGridFilter(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
- {
- ulGrid.DisplayLayout.Bands[0].ColumnFilters.ClearAllFilters();
- }
- /// <summary>
- /// 增加Grid的列过滤
- /// </summary>
- /// <param name="ulGrid"></param>
- public static void AddUlGridFilter(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
- {
- ulGrid.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
- }
- //查找treeView结点的父结点
- public static TreeNode findParentNode(TreeNode preLvlNode, string parentKey)
- {
- if (preLvlNode.Name == parentKey) return preLvlNode;
- foreach (TreeNode node in preLvlNode.Nodes)
- {
- if (node.Name.Length <= parentKey.Length)
- {
- if (node.Nodes.Count > 0)
- {
- TreeNode tNode = findParentNode(node, parentKey);
- if (tNode != null)
- return tNode;
- }
- else
- if (node.Name == parentKey)
- return node;
- }
- else
- return null;
- }
- return null;
- }
- }
- }
|