using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Security.Cryptography; using DevExpress.XtraGrid.Columns; using DevExpress.Utils; using DevExpress.XtraTreeList.Columns; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraEditors.Repository; using System.Data; using System.Drawing; using System.IO; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Lskj.PubUtils { public static class CommonUtils { #region 计算字宽度 private static string[] words = { "A", "B", "C", "D", "E", "F", "G", " H", " I", " J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; /// /// 计算字宽度 /// /// /// public static int GetFontWidth(string source) { return CutStr_BytesLen(source) * 6 + GetWords(source); } private static int GetWords(string source) { int len = 0; Regex reg = new Regex("[A-Z]"); MatchCollection mcs = reg.Matches(source); foreach (Match item in mcs) { if (words.Contains(item.Value)) len += 2; } return len; } /// /// 截断字符串 1个汉字长度为2 (超过则截断 拼上…) /// /// /// /// private static int CutStr_BytesLen(string strInput)//截取字符串 { strInput = strInput.Trim(); ASCIIEncoding ascii = new ASCIIEncoding(); int intLength = 0; byte[] s = ascii.GetBytes(strInput); for (int i = 0; i < s.Length; i++) { //中文字符转化ASCII吗后不能识别全部都变成63,才回出现这样结果,但是如何碰上日文就麻烦了,全半角假名全部都是63无法识别!所以该方法谨慎使用! if ((int)s[i] == 63) { intLength += 2; } else { intLength += 1; } } return intLength; } #endregion #region 格式化列 /// /// Grid格式化 /// /// /// /// public static void DataFormat(GridColumn gridColumn, string formatString, int fieldType) { if (string.IsNullOrEmpty(formatString)) return; switch (fieldType) { case ControlType.LabTextInt: // 数字格式化 case ControlType.LabCalcText: // 计算器格式化 gridColumn.DisplayFormat.FormatType = FormatType.Numeric; gridColumn.DisplayFormat.FormatString = formatString; break; default: break; } } /// /// TreeList格式化 /// /// /// /// public static void DataFormat(TreeListColumn treeColumn, string formatString, int fieldType) { if (string.IsNullOrEmpty(formatString)) return; // 数字格式化 if (fieldType == 7) { treeColumn.Format.FormatType = FormatType.Numeric; treeColumn.Format.FormatString = formatString; } } #endregion #region 获取参数 public static List GetParamValue(string param) { string SqlParamRegex = "{([^{])+}"; List list = new List(); Regex regex = new Regex(SqlParamRegex); MatchCollection mcs = regex.Matches(param); foreach (Match item in mcs) { list.Add(item.Value); } return list; } #endregion #region 特殊情况导出Excel /// /// 替换GridControl总bit类型,替换为是,否 /// /// /// public static GridControl ReplaceBitType(GridControl SourceGrid) { GridView gridView = SourceGrid.FocusedView as GridView; bool hasBoolean = false; GridColumn sortColumn = null; List boolList = new List(); List visibleList = new List(); foreach (GridColumn col in gridView.Columns) { if ((col.ColumnEdit != null && col.ColumnEdit.GetType() == typeof(RepositoryItemCheckEdit)) || (col.ColumnType != null && col.ColumnType.Name.ToLower() == "boolean")) { boolList.Add(col.FieldName); hasBoolean = true; } if (sortColumn == null && col.SortOrder != DevExpress.Data.ColumnSortOrder.None) { sortColumn = col; } } if (hasBoolean) { ExportExsForm exsForm = new ExportExsForm(); GridControl newGridControl = exsForm.gridControl1; GridView newGridView = exsForm.gridView1; // 复制列 foreach (GridColumn col in gridView.Columns) { if (col.Visible) { GridColumn newCol = new GridColumn(); newCol.Caption = col.Caption; newCol.FieldName = col.FieldName; newCol.Name = col.Name; newCol.VisibleIndex = col.VisibleIndex; newCol.Width = col.Width; newCol.OptionsColumn.AllowEdit = col.OptionsColumn.AllowEdit; newCol.ColumnEdit = boolList.Contains(col.FieldName) ? null : col.ColumnEdit; newGridView.Columns.Add(newCol); visibleList.Add(col.FieldName); } } DataTable sourceTable = (gridView.DataSource as DataView).Table; DataTable dtTable = new DataTable(); // 复制DataTable列 foreach (DataColumn col in sourceTable.Columns) { if (visibleList.Contains(col.ColumnName)) { DataColumn newCol = new DataColumn(); newCol.ColumnName = col.ColumnName; newCol.Caption = col.Caption; newCol.DataType = boolList.Contains(col.ColumnName) ? typeof(String) : col.DataType; dtTable.Columns.Add(newCol); } } // 复制DataTable行 foreach (DataRow row in sourceTable.Rows) { DataRow newRow = dtTable.NewRow(); foreach (DataColumn col in sourceTable.Columns) { if (visibleList.Contains(col.ColumnName)) { if (boolList.Contains(col.ColumnName)) { newRow[col.ColumnName] = row[col.ColumnName] + "" == "0" || (row[col.ColumnName] + "").ToLower() == "false" ? "否" : "是"; } else { if (string.IsNullOrEmpty(row[col.ColumnName] + "")) { if (col.DataType == typeof(Int16) || col.DataType == typeof(Int32) || col.DataType == typeof(Int64) || col.DataType == typeof(Decimal) || col.DataType == typeof(Double)) { newRow[col.ColumnName] = 0; } } else { newRow[col.ColumnName] = row[col.ColumnName] + ""; } } //处理统计 DataColumn dcIsSum = sourceTable.Columns["isSum"]; if (dcIsSum != null && !String.IsNullOrEmpty(row["isSum"] + "")) { int isSum = Convert.ToInt32(row["isSum"]); int fieldsqlTag = Convert.ToInt32(row["fieldsqlTag"]); string dataFormat = row["DataFormat"] + ""; GridColumn gridColumn = newGridView.Columns[col.ColumnName]; CommonUtils.DataFormat(gridColumn, dataFormat, fieldsqlTag); if (isSum == 1) { newGridView.OptionsView.ShowFooter = true; if ((row["calcExpr"] + "").IndexOf("/{") == -1) { gridColumn.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Sum, row["fieldname"].ToString(), "{0:" + dataFormat + "}") }); newGridView.GroupSummary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { new DevExpress.XtraGrid.GridGroupSummaryItem(DevExpress.Data.SummaryItemType.Sum, row["fieldname"].ToString(), null, "{0:" + dataFormat + "}") }); } else { gridColumn.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, row["fieldname"].ToString(), "{0:" + dataFormat + "}") }); newGridView.GroupSummary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { new DevExpress.XtraGrid.GridGroupSummaryItem(DevExpress.Data.SummaryItemType.Custom, row["fieldname"].ToString(), null, "{0:" + dataFormat + "}") }); } } } } } dtTable.Rows.Add(newRow); } if (gridView.Columns.Count > 0) { GridColumn firstColumn = newGridView.Columns[0]; if (firstColumn != null) { newGridView.OptionsView.ShowFooter = true; firstColumn.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count, firstColumn.FieldName.ToString(), "合计: {0:#,###}行") }); newGridView.GroupSummary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { new DevExpress.XtraGrid.GridGroupSummaryItem(DevExpress.Data.SummaryItemType.Count, firstColumn.FieldName.ToString(), null, "合计: {0:#,###}行") }); } } if (sortColumn != null) { DataView dataView = dtTable.DefaultView; dataView.Sort = string.Format(" {0} {1} ", sortColumn.FieldName, sortColumn.SortOrder == DevExpress.Data.ColumnSortOrder.Descending ? "desc" : "asc"); dtTable = dataView.ToTable(); } newGridControl.DataSource = dtTable; return newGridControl; } else { return SourceGrid; } } #endregion #region 设置系统时间 [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")] public static extern int GetSystemDefaultLCID(); [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")] public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData); public const int LOCALE_SLONGDATE = 0x20; public const int LOCALE_SSHORTDATE = 0x1F; public const int LOCALE_STIME = 0x1003; /// /// 设置时间日期格式yyyy-MM-dd HH:mm:ss /// public static void SetDateTimeFormat() { try { int x = GetSystemDefaultLCID(); SetLocaleInfo(x, LOCALE_STIME, "HH:mm:ss"); //时间格式 SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd"); //短日期格式 //SetLocaleInfo(x, LOCALE_SLONGDATE, "yyyy-MM-dd"); //长日期格式 } catch (Exception ex) { Console.WriteLine(ex); } } #endregion } }