PublicFunction.cs
15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
///--------------------------------------------------------------------------
/// 文 件 名:PublicFuction.cs
/// 功能描述:公用方法类
/// CheckIPAddress:判断IP地址
/// IsMatchWith :判断格式(正则表达式)
/// ExportToExcel :导出Excel文件
///--------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Reflection;
using GeneralLib;
using System.Threading;
using System.Security.Principal;
namespace SunVoteARSPPT
{
class PublicFunction
{
/// <summary>
/// 检查IP地址合法性
/// </summary>
/// <returns></returns>
public static bool CheckIPAddress(string sIPAddress)
{
bool isOK = false;
string[] ary = sIPAddress.Split('.');
if (ary.Length == 4)
{
isOK = true;//成功
for (int i = 0; i < ary.Length; i++)
{
int num = -1;
if (int.TryParse(ary[i], out num))
{
if ((num < 0) || (num > 255))
{
isOK = false;
break;
}
}
else
{
isOK = false;
break;
}
}
}
return isOK;
}
/// <summary>
/// 判断字符是否符合格式
/// </summary>
/// <param name="str"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static bool IsMatchWith(String str, String pattern)
{
if (str == null)
{
return false;
}
return new Regex(pattern).IsMatch(str);
}
/// <summary>
/// 导出Excel表格。杨斌 2019-05-29
/// </summary>
public static void ExportToExcelEx(System.Windows.Forms.DataGridView grd, string titleName)
{
try
{
ExcelOper exl = new ExcelOper();
exl.NewExcel();
Microsoft.Office.Interop.Excel.Worksheet oSheet = exl.GetSheetByNum(1);
ExcelOper.SetSheetName(oSheet, titleName);
var ran = ExcelOper.GetRange(oSheet, 1, 1, grd.RowCount + 1, 1);
//ran.Columns.AutoFit();
ran.ColumnWidth = 20;
ran.Rows.WrapText = true;
ExcelOper.SetRangeColor(ran, grd.ColumnHeadersDefaultCellStyle.BackColor);
ran = ExcelOper.GetRange(oSheet, 1, 1, grd.RowCount + 1, 1);
ran.RowHeight = 30;
ran = ExcelOper.GetRange(oSheet, 1, 2, 1, grd.ColumnCount + 1);
ran.ColumnWidth = 5;
ran = ExcelOper.GetRange(oSheet, 1, 1, grd.RowCount + 1, grd.ColumnCount + 1);
ExcelOper.SetBorderLine(ran);
for (int col = 0; col < grd.Columns.Count; col++)
{
int r = 1;
int c = col + 2;
oSheet.Cells[r, c] = grd.Columns[col].HeaderText;
ran = ExcelOper.GetRange(oSheet, r, c, r, c);
//ran.ColumnWidth = grd.Columns[col].Width;
ExcelOper.SetRangeColor(ran, grd.ColumnHeadersDefaultCellStyle.BackColor);
}
for (int row = 0; row < grd.Rows.Count; row++)
{
int r = row + 2;
int c = 1;
oSheet.Cells[r, c] = grd.Rows[row].HeaderCell.Value;
ran = ExcelOper.GetRange(oSheet, r, c, r, c);
//ran.RowHeight = grd.Rows[row].Height;
ExcelOper.SetRangeColor(ran, grd.RowHeadersDefaultCellStyle.BackColor);
for (int col = 0; col < grd.Columns.Count; col++)
{
r = row + 2;
c = col + 2;
//oSheet.Cells[r, c] = "";
ran = ExcelOper.GetRange(oSheet, r, c, r, c);
ExcelOper.SetRangeColor(ran, grd.Rows[row].Cells[col].Style.BackColor);
}
}
exl.ShowExcel(true);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
}
/// <summary>
/// 导出Excel表格
/// </summary>
/// <param name="grd">导出表格控件</param>
/// <param name="titleName">标题</param>
/// <returns>true:导出成功,false:未导出</returns>
public static bool ExportToExcel(System.Windows.Forms.DataGridView grd, string titleName)
{
string fileName = "";
System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
saveFileDialog.InitialDirectory = "C:\\";
//saveFileDialog.Filter = "Execl files (*.xls)|*.xls|Excel files (*.xlsx)|*.xlsx";
saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx";//杨斌 2018-12-18。不兼容2003格式
//saveFileDialog.FilterIndex = (Convert.ToDouble(excelApplication.Version) >= 12) ? 2 : 1;
if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = saveFileDialog.FileName;
}
else
{
return false;
}
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
excelApplication.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Workbook workbook = excelApplication.Workbooks.Add(Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet lastWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(workbook.Worksheets.Count);
Microsoft.Office.Interop.Excel.Worksheet newSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, lastWorksheet, Type.Missing, Type.Missing);
// string excelVertion = (Convert.ToDouble(excelApplication.Version) > 12) ? "xlsx" : "xls";
newSheet.Name = titleName;
for (int col = 0; col < grd.Columns.Count; col++)
{
newSheet.Cells[1, col + 1] = grd.Columns[col].HeaderText;
}
for (int row = 0; row < grd.Rows.Count; row++)
{
for (int col = 0; col < grd.Columns.Count; col++)
{
newSheet.Cells[row + 2, col + 1] = (grd.Rows[row].Cells[col].Value != null ? grd.Rows[row].Cells[col].Value.ToString() : "");
}
}
try
{
workbook.Close(true, fileName, System.Reflection.Missing.Value);
}
catch (Exception e)
{
throw e;
}
excelApplication.Quit();
KillExcel();
return true;
}
/// <summary>
/// 关闭EXCEL进程
/// </summary>
private static void KillExcel()
{
Process[] excelProcesses = Process.GetProcessesByName("EXCEL");
DateTime startTime = new DateTime();
int processId = 0;
for (int i = 0; i < excelProcesses.Length; i++)
{
if (startTime < excelProcesses[i].StartTime)
{
startTime = excelProcesses[i].StartTime;
processId = i;
}
}
if (excelProcesses[processId].HasExited == false)
{
excelProcesses[processId].Kill();
}
}
/// <summary>
/// 数字转换为字母
/// 创建 赵丽
/// </summary>
/// <returns></returns>
public static string NumToABC(int iNum)
{
string option = "";
switch (iNum)
{
case 1:
option = "A";
break;
case 2:
option = "B";
break;
case 3:
option = "C";
break;
case 4:
option = "D";
break;
case 5:
option = "E";
break;
case 6:
option = "F";
break;
case 7:
option = "G";
break;
case 8:
option = "H";
break;
case 9:
option = "I";
break;
case 10:
option = "J";
break;
default:
break;
}
return option;
}
/// <summary>
/// 判断两个数组是否完全相等
/// 创建 赵丽
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool AryIsEqual(string[] a, string[] b)
{
bool bResult = true;
if (a.Length != b.Length) { return false; }
for (int i = 0; i < a.Length; i++)
{
if (Array.IndexOf(b, a[i]) == -1)
{
bResult = false;
break;
}
}
for (int i = 0; i < b.Length; i++)
{
if (Array.IndexOf(a, b[i]) == -1)
{
bResult = false;
break;
}
}
return bResult;
}
/// <summary>
/// 去掉末尾的指定字符串
/// </summary>
/// <param name="sText"></param>
/// <param name="sTrim"></param>
/// <returns></returns>
public static string TrimEndStr(string sText, string sTrim)
{
string res = sText;
if (sText.Length > 0)
{
int len = sText.Length - sTrim.Length;
if ((len > 0) && (sText.Substring(len) == sTrim))
res = sText.Substring(0, len);
}
return res;
}
/// <summary>
/// 数组排序
/// 修改:杨斌 2012-10-12 键盘数量=6000时,非常耗时。修改内部排序方式,不用冒泡。
/// </summary>
/// <param name="KeyList"></param>
/// <returns></returns>
public static string[] KeyIDSort()
{
string[] aryKeyIDStr = new string[GlobalInfo.hardwareManage.KeyStatus.Count];
int[] aryKeyID = new int[GlobalInfo.hardwareManage.KeyStatus.Count];
GlobalInfo.hardwareManage.KeyStatus.Keys.CopyTo(aryKeyIDStr, 0);
for (int i = 0; i < aryKeyID.Length; i++)
{
aryKeyID[i] = ConvertOper.Convert(aryKeyIDStr[i]).ToInt;
}
Array.Sort(aryKeyID);
for (int i = 0; i < aryKeyID.Length; i++)
{
aryKeyIDStr[i] = aryKeyID[i].ToString();
}
return aryKeyIDStr;
//string[] KeyIDList = new string[GlobalInfo.hardwareManage.KeyStatus.Count];
//for (int i = 0; i < KeyIDList.Length; i++) { KeyIDList[i] = "0"; }
//GlobalInfo.hardwareManage.KeyStatus.Keys.CopyTo(KeyIDList, 0);
////冒泡排序从小到大
//Array.Sort(KeyIDList);
//for (int i = 0; i < KeyIDList.Length; i++)
//{
// for (int j = i + 1; j < KeyIDList.Length; j++)
// {
// if (Convert.ToInt32(KeyIDList[i]) > Convert.ToInt32(KeyIDList[j]))
// {
// string iTemp = KeyIDList[i];
// KeyIDList[i] = KeyIDList[j];
// KeyIDList[j] = iTemp.ToString();
// }
// }
//}
//return KeyIDList;
}
public static char SplitItemScore = '|';//杨斌 2019-07-19
/// <summary>
/// 从字符串获取选项分数字典,Key=选项编号,Item=分值
/// </summary>
/// <param name="optionScore"></param>
/// <returns></returns>
public static Dictionary<int, double> GetItemScore(string optionScore)
{
Dictionary<int, double> dicScore = new Dictionary<int, double>();
try
{
string[] aryScore = optionScore.Split(SplitItemScore);
int num = 0;
for (int i = 0; i < aryScore.Length; i++)
{
string[] aryScoreItem = aryScore[i].Split('=');
double score = 0;
if (aryScoreItem.Length >= 2)
{
num = new ConvertOper(aryScoreItem[0]).ToInt;
score = new ConvertOper(aryScoreItem[1]).ToDouble;
if (!dicScore.ContainsKey(num))
dicScore.Add(num, score);
}
}
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
return dicScore;
}
/// <summary>
/// 判断当前用户是否为管理员
/// 创建 赵丽 2012-04-12
/// </summary>
/// <returns></returns>
public static bool IsAdministrator()
{
AppDomain myDomain = Thread.GetDomain();
myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
return myPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
/// <summary>
/// 转半角的函数
/// 创建标志 赵丽 2012-06-06
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
public static int ToInt(string strValue)
{
try
{
return Convert.ToInt32(strValue);
}
catch
{
return 0;
}
}
/// <summary>
/// 获取字符串字节长度,即一个汉字长度为2倍
/// 创建:杨斌 2013-04-19
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int StrLenByte(string str)
{
if (str != null)
return System.Text.Encoding.Default.GetByteCount(str);
else
return 0;
}
/// <summary>
/// 判断路径格式是否正确有效
/// 杨斌 2014-12-05s
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static bool CheckPath(string path)
{
string pattern = @"^[a-zA-Z]:(((\\(?! )[^/:*?<>\""|\\]+)+\\?)|(\\)?)\s*$";
Regex regex = new Regex(pattern);
return regex.IsMatch(path);
}
}
}