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 CoreFS.CA06;
using System.Collections;
using Infragistics.Win.UltraWinGrid;
using Excel1 = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Core.LZMes.Client.UIK
{
public partial class UIK050103 : FrmBase
{
public UIK050103()
{
InitializeComponent();
}
public override void ToolBar_Click(object sender, string ToolbarKey)
{
switch (ToolbarKey)
{
case "Query"://查询
this.DoQuery();
break;
case "export"://添加换下轧辊实绩
this.DoExport(ultraGrid1, "磨削实绩查询");
break;
case "Update"://下发换下轧辊实绩
this.DoUpdate();
break;
}
}
private void DoQuery()
{
try
{
string zgNum = "";
string beginTime = "";
string endTime = "";
string type = "";
CoreClientParam ccp = new CoreClientParam();
DataTable dt = new DataTable();
zgNum = this.ultraTextzgNum.Text;
type = this.ultraComboEditor1.Text;
beginTime = ultraDateTimeEditor1.Value != null ? ultraDateTimeEditor1.DateTime.ToString("yyyyMMdd") : "";
endTime = ultraDateTimeEditor2.Value != null ? ultraDateTimeEditor2.DateTime.ToString("yyyyMMdd") : "";
this.dataSet1.Clear();
ArrayList al = new ArrayList();
al.Add("UIK050103_01.SELECT");
al.Add(zgNum);
al.Add(type);
al.Add(beginTime);
al.Add(endTime);
al.Add(zgNum);
al.Add(type);
al.Add(beginTime);
al.Add(endTime);
ccp = new CoreClientParam();
ccp.ServerName = "UIB.COM.ComDBQuery";
ccp.MethodName = "doSimpleQuery";
ccp.ServerParams = new object[] { al };
ccp.IfShowErrMsg = false;
ccp.SourceDataTable = this.dataSet1.Tables[0];
this.ExecuteQueryToDataTable(ccp, CoreInvokeType.Internal);
}
// }
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString());
}
}
private void DoExport(UltraGrid tmpDataTable, string rFileName)
{
string fileName = saveFileDialog1.FileName;
string FileName;
saveFileDialog1.FileName = rFileName;
saveFileDialog1.DefaultExt = ".xls";
saveFileDialog1.Filter = "excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
fileName = saveFileDialog1.FileName;
FileName = fileName;
}
else
{
return;
}
string FilePath = saveFileDialog1.FileName;
if (tmpDataTable == null)
return;
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.DisplayLayout.Bands[0].Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Excel1.Application xlApp = new Excel1.ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = false;
xlApp.SheetsInNewWorkbook = 1; //设置导出表格工作簿sheet数量
Excel1._Workbook xlBook = (Excel1._Workbook)(xlApp.Workbooks.Add(Missing.Value));//添加新工作簿
Excel1.Worksheet ws = (Excel1.Worksheet)xlBook.Worksheets[1];
ws.Name = rFileName;
Excel1.Range r = ws.get_Range("A1", NumToExeclRowStr(columnNum - 1) + "1");
object[] objHeader = new object[columnNum];
//将DataTable的列名导入Excel表第一行
foreach (UltraGridColumn dc in tmpDataTable.DisplayLayout.Bands[0].Columns)
{
if (columnIndex < columnNum-1)
{
objHeader[columnIndex] = dc.Header.Caption;
columnIndex++;
}
}
r.Value2 = objHeader;
//将DataTable中的数据导入Excel中
int h = 0;
for (int i = 0; i < rowNum; i++)
{
//if (tmpDataTable.Rows[i].Cells[columnNum].Text.ToString() == "True")
// {
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum - 1; j++)
{
objHeader[columnIndex] = tmpDataTable.Rows[h].Cells[j].Text.ToString();
columnIndex++;
}
r = ws.get_Range("A" + (h + 2), NumToExeclRowStr(columnNum) + (h + 2));
r.Value2 = objHeader;
h++;
// }
}
//将合同号的数据转换为excel中的数字格式,否则为科学计数法格式
Excel1.Range RMXNEW;
string Inf0 = "";
Inf0 = "V1:" + "V2";
RMXNEW = ws.get_Range(Inf0, Type.Missing);
RMXNEW.EntireColumn.HorizontalAlignment = Excel1.XlHAlign.xlHAlignCenter;
RMXNEW.EntireColumn.NumberFormat = "0";
Excel1.Range RMXNEW1;
string Inf1 = "";
Inf1 = "W1:" + "W2";
RMXNEW1 = ws.get_Range(Inf1, Type.Missing);
RMXNEW1.EntireColumn.HorizontalAlignment = Excel1.XlHAlign.xlHAlignCenter;
RMXNEW1.EntireColumn.NumberFormat = "0";
////导出到EXCEL中 只需要某些列,下面删除多余的列(已经在datatable中按字段排好序)
//Excel1.Range RMX1;
//string Inf1 = "";
//Inf1 = "A1:" + "W1";
//RMX1 = ws.get_Range(Inf1, Type.Missing);
//RMX1.EntireColumn.Delete(0);
r.EntireColumn.AutoFit();
xlBook.SaveCopyAs(FilePath);
this.KillSpecialExcel(xlApp);
MessageBox.Show("数据成功导出到:" + saveFileDialog1.FileName.ToString(),
"导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
//System.Diagnostics.Process.Start(fileName);
}
private string NumTochr(int Num)
{
int n = 64 + Num;
return "" + (Char)n;
}
private string NumToExeclRowStr(int Num)
{
int X, Y;
if (Num < 27)
{
return NumTochr(Num);
}
X = Num / 26;
Y = Num - X * 26;
//if (Y == 0) Y = 1;
return NumTochr(X) + NumTochr(Y);
}
#region 结束EXCEL.EXE进程的方法
///
/// 结束EXCEL.EXE进程的方法
///
/// EXCEL对象
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
public void KillSpecialExcel(Microsoft.Office.Interop.Excel.Application m_objExcel)
{
try
{
if (m_objExcel != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId);
System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
private void ultraGrid1_DoubleClickRow(object sender, DoubleClickRowEventArgs e)
{
Infragistics.Win.UltraWinGrid.UltraGrid uGrid = (Infragistics.Win.UltraWinGrid.UltraGrid)sender;
ultraTextStand_id.Text = uGrid.ActiveRow.Cells["ROLLID"].Text;
ultraTextEditor3.Text = uGrid.ActiveRow.Cells["roll_type"].Text;//轧辊类型
ultraTextEditor6.Text = uGrid.ActiveRow.Cells["RESERVE"].Text;//直径
ultraTextShangID.Text = uGrid.ActiveRow.Cells["stand_id"].Text;//机架编号
ultraTextEditor2.Text = uGrid.ActiveRow.Cells["spec_no"].Text;//顺序号
ultraCalendarDis_time.Text = uGrid.ActiveRow.Cells["REG_DTIME"].Text;//时间
ultraTextEditor4.Text = uGrid.ActiveRow.Cells["DIS"].Text;
}
private void DoUpdate()
{
try
{
ArrayList al = new ArrayList();
ArrayList list = new ArrayList();
string roll_num = this.ultraTextStand_id.Text; //轧辊编号
string rolltype = this.ultraTextEditor3.Text;//
string diam = this.ultraTextEditor6.Text; //轧辊区域
string jijia = this.ultraTextShangID.Text;//上编号
string shunxu = this.ultraTextEditor2.Text;//软点
//string shijian = this.ultraCalendarDis_time.Value != null ? ultraDateTimeEditor1.DateTime.ToString("yyyyMMddhhmiss") : ""; //this.dateTimePicker1.Text.ToString();
string shijian = this.ultraCalendarDis_time.Text; //this.dateTimePicker1.Text.ToString();
string qufen = this.ultraTextEditor4.Text;
if(qufen=="UP")
{
list.Add("UIK050103_01.UPDATE");
}
else if(qufen == "LO")
{
list.Add("UIK050103_02.UPDATE");
}
list.Add(rolltype);
list.Add(diam);
list.Add(jijia);
list.Add(shijian);
list.Add(roll_num);
list.Add(shunxu);
al.Add(list);
if (al.Count == 0)
{
MessageBox.Show("请勾选数据再做保存操作!", "提示");
return;
}
CoreClientParam ccp = new CoreClientParam();
ccp.ServerName = "UIB.COM.ComDBSave";
ccp.MethodName = "doXmlSave";
ccp.ServerParams = new object[] { al };
ccp = this.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
this.DoQuery();
if (0 != ccp.ReturnCode)
{
MessageBox.Show("保存失败!");
}
else
{
MessageBox.Show("保存成功!");
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString());
}
}
private void UIK050103_Load(object sender, EventArgs e)
{
}
}
}