using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Office.Interop.Excel; //APP
using System.Xml;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection; //MISSIN
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid.ExcelExport;
using Infragistics.Excel;
namespace Core.StlMes.Client.Plan.Order
{
public class PlanComm
{
public static double PI = 3.1415926;
///
/// 管米单重计算(t/m)
///
/// 外径(mm)
/// 壁厚(mm)
///
public static double WeightOfMi(double outdiameter, double aimwallthick)
{
double weightOfmi = 0.02466 * aimwallthick * (outdiameter - aimwallthick) / 1000;
return weightOfmi;
}
///
/// 管坯米单重(t/m)
///
/// 管坯断面(mm)
///
public static double GpweightOfmi(double outdiameter)
{
return (7.8 * PI / 4 * outdiameter * outdiameter) / 1000 / 1000;
}
///
/// 单位英尺长度转米
///
/// 英尺长度
/// 米长度
public static double FootoMi(double lenth)
{
double converate = 0.3048; //换算率
return Math.Round((lenth * converate), 2);
}
///
/// 导出
///
public static void Export(ref UltraGrid myGrid1, string strFileName)
{
try
{
if (myGrid1.Rows.Count == 0) return;
if (strFileName.Length == 0) strFileName = "未命名";
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "保存";
dlg.OverwritePrompt = true;
dlg.Filter = "Excel文件(*.xls)|*.xls";
dlg.AddExtension = true;
dlg.FileName = strFileName;
if (dlg.ShowDialog() == DialogResult.OK)
{
strFileName = dlg.FileName;
using (UltraGridExcelExporter ultraGridExcelExporter1 = new UltraGridExcelExporter())
{
ultraGridExcelExporter1.Export(myGrid1, strFileName);
}
if (MessageBox.Show("数据导出成功!\n需要打开所导出文件吗?", "提示",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
ProcessStartInfo p = new ProcessStartInfo(strFileName);
p.WorkingDirectory = Path.GetDirectoryName(strFileName);
Process.Start(p);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
///
/// 设置grid单元格可编辑
///
/// grid表格
/// 可编辑的列头名称数组
public static void setGridActivation(UltraGridBand band, params string[] strs)
{
foreach (UltraGridColumn column in band.Columns)
{
if (!strs.Contains(column.Key))
{
column.CellActivation = Activation.ActivateOnly;
}
else
{
column.CellActivation = Activation.AllowEdit;
column.CellAppearance.BackColor = Color.FromArgb(255, 255, 128);
}
}
}
///
/// 表格下拉框,可以有ValueList来提供,可以选择的样式
///
///
///
///
///
public static ValueList GeneralValuelist(ref System.Data.DataTable table, string strKey, string strText)
{
if (table == null || !table.Columns.Contains(strKey) || !table.Columns.Contains(strText))
{
return null;
}
ArrayList alist = new ArrayList();
ValueList vlist = new ValueList();
for (int i = 0; i < table.Rows.Count; i++)
{
try
{
if (!alist.Contains(table.Rows[i][strKey]))
{
alist.Add(table.Rows[i][strKey]);
vlist.ValueListItems.Add(table.Rows[i][strKey], Convert.ToString(table.Rows[i][strText]));
}
}
catch { }
}
return vlist;
}
///
/// 自定义对象深拷贝
///
/// 类名
/// 源对象
///
public static T Clone(T RealObject)
{
using (Stream objectStream = new MemoryStream())
{
//利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, RealObject);
objectStream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(objectStream);
}
}
///
/// 键盘按下Ctrl+D,拷贝活动单元格的数据到所有已勾选的此列的单元格中
///
/// UltraGrid
///
/// 可以进行列名称
public static void setGridCopyActColumn(UltraGrid ugrid, KeyEventArgs e, params string[] strs)
{
if (e.Control && e.KeyCode == Keys.D)
{
if (ugrid != null && ugrid.ActiveRow != null)
{
UltraGridRow ugr = ugrid.ActiveRow;
foreach (string colName in strs)
{
if (ugrid.ActiveCell.Column.Key.Equals(colName))
{
if (ugr.Cells[colName].Activation != Activation.AllowEdit)
{
return;
}
ugrid.UpdateData();
ArrayList list = new ArrayList();
IQueryable checkRows = ugrid.Rows.AsQueryable().Where(" CHECK = 'True' ");
if (checkRows.Count() == 0)
{
return;
}
foreach (UltraGridRow uRow in checkRows)
{
if (uRow != ugr && uRow.Cells[colName].Activation == Activation.AllowEdit)
{
uRow.Cells[colName].Value = ugr.Cells[colName].Value;
}
}
}
}
}
}
}
///
/// 键盘按下Ctrl+D,拷贝活动单元格的数据到所有已勾选的此列的单元格中
///
/// UltraGrid
/// 选择列名(例如"CHC")
///
/// 可以进行列名称
public static void setGridCopyActColumn(UltraGrid ugrid, string key, KeyEventArgs e, params string[] strs)
{
if (e.Control && e.KeyCode == Keys.D)
{
if (ugrid != null && ugrid.ActiveRow != null)
{
UltraGridRow ugr = ugrid.ActiveRow;
foreach (string colName in strs)
{
if (ugrid.ActiveCell.Column.Key.Equals(colName))
{
if (ugr.Cells[colName].Activation != Activation.AllowEdit)
{
return;
}
ugrid.UpdateData();
ArrayList list = new ArrayList();
IQueryable checkRows = ugrid.Rows.AsQueryable().Where(key + " = 'True'");
if (checkRows.Count() == 0)
{
return;
}
foreach (UltraGridRow uRow in checkRows)
{
if (uRow != ugr && uRow.Cells[colName].Activation == Activation.AllowEdit)
{
uRow.Cells[colName].Value = ugr.Cells[colName].Value;
}
}
}
}
}
}
}
///
/// 返回table的column列中是否存在str
///
/// datable
/// 列头名
/// 匹配字符串
///
public static bool isInDataTable(System.Data.DataTable dt, string column, string str)
{
bool isInTable = false;
if (dt == null || column.Equals(""))
{
return isInTable;
}
if (!dt.Columns.Contains(column))
{
return isInTable;
}
foreach(DataRow dr in dt.Rows)
{
if (dr[column].ToString().Equals(str))
{
isInTable = true;
}
}
return isInTable;
}
///
/// 设置数字类型列千分位显示,小数点保留位数
///
/// UltraGridBand
/// 数字最大位数(如:99999.99,最大位数5位)
/// 保留小数位数(如:99999.99,保留2位小数)
/// 数字单元列
public static void setGridDigitalColMaxInput(UltraGridBand band, int maxDigit, int digit, params string[] strs)
{
if (band == null)
{
return;
}
string maskInput = "{LOC}";
if (maxDigit > 0)
{
string str = "";
for (int i = 0; i < maxDigit; i++)
{
if (i != 0 && (i % 3) == 0)
{
str = "," + str;
}
str = "n" + str;
}
maskInput = maskInput + str;
if (digit > 0)
{
maskInput += ".";
for (int i = 0; i < digit; i++)
{
maskInput += "n";
}
}
foreach (UltraGridColumn column in band.Columns)
{
if (strs.Contains(column.Key))
{
column.MaskDisplayMode = Infragistics.Win.UltraWinMaskedEdit.MaskMode.IncludeLiterals;
column.MaskInput = maskInput;
}
}
}
}
///
/// 设置Grid列是数字类型,显示样式
///
/// UltraGridBand
/// 数字最大位数(如:99999.99,最大位数5位)
/// 保留小数位数(如:99999.99,保留2位小数)
/// 数字单元列
public static void setGridDigitalCol(UltraGridBand band, int maxDigit, int digit, params string[] strs)
{
if (band == null)
{
return;
}
string maskInput = "{LOC}";
if (maxDigit > 0)
{
string str = "";
for (int i = 0; i < maxDigit; i++)
{
if (i != 0 && (i % 3) == 0)
{
str = "," + str;
}
str = "n" + str;
}
maskInput = maskInput + str;
if (digit > 0)
{
maskInput += ".";
for (int i = 0; i < digit; i++)
{
maskInput += "n";
}
}
foreach (UltraGridColumn column in band.Columns)
{
if (strs.Contains(column.Key))
{
column.MaskDisplayMode = Infragistics.Win.UltraWinMaskedEdit.MaskMode.IncludeLiterals;
column.MaskInput = maskInput;
column.CellAppearance.TextHAlign = HAlign.Right;
if (digit > 0)
{
column.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Double;
}
else
{
column.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Integer;
}
}
}
}
}
///
/// 等待窗口
///
public static void WaitFromOpen(Cursor cursor)
{
cursor = Cursors.WaitCursor; //控制鼠标的样式为等待
if (Constant.WaitingForm == null)
{
Constant.WaitingForm = new WaitingForm();
}
Constant.WaitingForm.ShowToUser = true;
Constant.WaitingForm.Show();
Constant.WaitingForm.Update();
}
///
/// 关闭等待
///
public static void WaitFromColse(Cursor cursor)
{
cursor = Cursors.Default;
Constant.WaitingForm.ShowToUser = false;
Constant.WaitingForm.Close();
Constant.WaitingForm = null;
}
}
}