using System;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Core.Mes.ClientFrameWork;
using Core.Mes.IBaseInterface;
using System.Data;
using System.Reflection;
using System.Runtime.InteropServices;
using Core.XgMes.Client.JGKC.TurnoffSendManager;
using System.Diagnostics;
using Infragistics.Win.UltraWinEditors;

namespace Core.XgMes.Client.JGKC.MaterialManager
{
    public partial class MidAccpetSheetNew : Mes.ClientFrameWork.FrmBase
    {
        public MidAccpetSheetNew()
        {
            InitializeComponent();
        }

        private void Cmb_wzdm_TextChanged(object sender, EventArgs e)
        {
           
        }
      



      


        private void Cmb_wzdm_DoubleClick(object sender, EventArgs e)
        {
            
        }

        private void Cmb_wzdm_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            FrmMaterialDaioageNew metaterial = new FrmMaterialDaioageNew();
            metaterial.StartPosition = FormStartPosition.CenterParent;
            metaterial.ShowDialog();
            Cmb_wzdm.Text = metaterial.strwzdm;
            txt_WZMC.Text = metaterial.strwzmc;
            txt_GGXH.Text = metaterial.strgg;
            txt_JLDW.Text = metaterial.strjldw;
            txt_CZ.Text = metaterial.strcz;
            txt_DZ.Text = metaterial.strdz;
            Cmb_CKDM.Text = metaterial.strckcode;
            cmb_StorageLB.Text = metaterial.strcklb;
            txt_CKMC.Text = metaterial.strckmc;
            ultraCombo1.Text = metaterial.strlbdm;
            ultraTextEditor42.Text = metaterial.strlbmc;
        }

        private void Cmb_CKDM_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            FrmDepotDialog depot = new FrmDepotDialog();
            depot.StartPosition = FormStartPosition.CenterParent;
            depot.ShowDialog();
            Cmb_CKDM.Text = depot.strckdm;
            cmb_StorageLB.Text = depot.strcklb;
            txt_CKMC.Text = depot.stckmc;

        }

        private void Cmd_GFCode_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            FrmProvlderDialog provlder = new FrmProvlderDialog();
            provlder.StartPosition = FormStartPosition.CenterParent;
            provlder.ShowDialog();
            Cmd_GFCode.Text = provlder.strgfdm;
            txt_GYMC.Text = provlder.strgfmc;
        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "Add":
                    Add("ADD");
                    break;
                case "Query":
                    DoQuery();
                    break;
                case "UPDATE":
                    Add("UPDATE");
                    break;
                    break;
                case "Del":
                    Add("Del");
                    break;
                case "Export":
                    Export();
                    break;
                case "ExportLL":
                    ExportLL();
                    break;
                case "Close":
                    this.Close();
                    break;
                case "SaveIN":
                    SaveInvoice_No();
                    break;
                    
            }
        }
        private void gd_CK_AfterRowActivate(object sender, EventArgs e)
        {
           

       

                    this.txt_BH.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["PICKING_NO"].Value); //1 领料单号
                    this.Cmb_CKDM.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["STORAGE_CODE"].Value); //3 仓库代码
                    this.cmb_StorageLB.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["STORAGE_STYLE"].Value);//4 仓库类别
                    this.txt_CKMC.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["ISSUE_STORE"].Value);//5 仓库名称
                    this.txt_ylbz.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["PICKING_NAME"].Value);//6 领料班组
                    this.Cmb_wzdm.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["MATERIAL_CODE"].Value);//7 物资代码
                    this.txt_WZMC.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["MATERIAL_NAME"].Value);//8 物资名称
                    this.txt_GGXH.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["SPET"].Value);//9 规格型号
                    this.txt_JLDW.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["UNIT"].Value);//10 计量单位
                    this.txt_CZ.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["MATERIAL"].Value);//11 材质
                    this.txt_DZ.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["WEIGHT"].Value);//12 单重
                    this.txt_PlanR.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["SCHEDULER"].Value);///13 计划人
                    this.txt_qlsl.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["PLEASE_NUM"].Value);///14 请领数量
                    this.txt_YSS.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["REAL_NUM"].Value);//15 实发数量
                    this.txt_Price.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["PRICE"].Value);//16 单价
                    this.txt_Moneny.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["MONERY"].Value);//17 金额
                    this.txt_YR.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["PICKING_RECODER"].Value);//19 领料人
                    this.Cmd_GFCode.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["GF_CODE"].Value);///20 供方代码
                    this.txt_GYMC.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["GF_NAME"].Value);///21供方名称
                    this.txt_Remark.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["REMARK"].Value);///22 备注
                    this.ultraCombo1.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["MATERIAL_LB"].Value);///22 备注

                    this.ultraTextEditor42.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["MATERIAL_LB_NAME"].Value);///22 备注
                    this.ultraTextEditor43.Text = Comm.ObjToStr(this.gd_CK.ActiveRow.Cells["USE_TO"].Value);///22 备注
 


                
        }






        private void chk_AllowFilter_CheckedChanged(object sender, System.EventArgs e)
        {
            if (this.chk_AllowFilter.Checked)
            {
                this.gd_CK.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
            }
            else
            {
                this.gd_CK.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.False;

            }

        }
	
        private void Export()
        {
            if (this.gd_CK.Rows.Count > 0)
            {
                Comm.ExPortExcel(this.gd_CK, this.excelExporter);
            }
            else
            {
                MessageBox.Show("无领料单信息数据，不能导出！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        private void Add(string strMode)
        {
            try
            {
                string strerr = "";

                System.Collections.ArrayList MetarilInfo = new ArrayList();


                if (strMode == "UPDATE")
                {
                    if (txt_BH.Text.Trim().ToString() == "")
                    {
                        MessageBox.Show("领料单号不能为空");
                        return;
                    }
                }
                /*if (this.txt_BH.Text.Trim().ToString().Substring(0,1) != "L")
                {
                    MessageBox.Show("领料单号第一位请输入L");
                    return;
                }*/
                if (!(this.txt_CKMC.Text.Trim().ToString() == "2#" || this.txt_CKMC.Text.Trim().ToString()=="3#"))
                {
                    MessageBox.Show("请选择实物库库位进行领料");
                    return;
                }
                MetarilInfo.Add(Comm.ObjToStr(this.txt_BH.Text.Trim().ToString()));//1 领料单号
                MetarilInfo.Add(Comm.ObjToStr(Convert.ToDateTime( this.date_YS.Value.ToString().Trim()).ToString("yyyyMMdd")));//2 发料日期
                MetarilInfo.Add(Comm.ObjToStr(this.Cmb_CKDM.Text.ToString().Trim()));//3 仓库代码
                MetarilInfo.Add(Comm.ObjToStr(this.cmb_StorageLB.Text.Trim().ToString()));//4 仓库类别
                MetarilInfo.Add(Comm.ObjToStr(this.txt_CKMC.Text.Trim().ToString()));//5 仓库名称
                MetarilInfo.Add(Comm.ObjToStr(this.txt_ylbz.Text.Trim().ToString()));//6 领料班组



                MetarilInfo.Add(Comm.ObjToStr(this.Cmb_wzdm.Text.Trim().ToString()));//7 物资代码
                MetarilInfo.Add(Comm.ObjToStr(this.txt_WZMC.Text.Trim().ToString()));//8 物资名称
                MetarilInfo.Add(Comm.ObjToStr(this.txt_GGXH.Text.Trim().ToString()));//9 规格型号
                MetarilInfo.Add(Comm.ObjToStr(this.txt_JLDW.Text.Trim().ToString()));//10 计量单位
                MetarilInfo.Add(Comm.ObjToStr(this.txt_CZ.Text.Trim().ToString()));//11 材质
                MetarilInfo.Add(Comm.ObjToStr(this.txt_DZ.Text.Trim().ToString()));//12 单重
                MetarilInfo.Add(Comm.ObjToStr(this.txt_PlanR.Text.Trim().ToString()));//13 计划人


                MetarilInfo.Add(Comm.ObjToStr(this.txt_qlsl.Text.Trim().ToString()));//14 请领数量
                MetarilInfo.Add(Comm.ObjToStr(this.txt_YSS.Text.Trim().ToString()));//15 实发数量
                MetarilInfo.Add(Comm.ObjToStr(this.txt_Price.Text.Trim().ToString()));//16 单价
                MetarilInfo.Add(Comm.ObjToStr(this.txt_Moneny.Text.Trim().ToString()));//17 金额
                MetarilInfo.Add(Comm.ObjToStr(Convert.ToDateTime(this.date_td.Value.ToString().Trim()).ToString("yyyyMMdd")));//18 填单日期
                MetarilInfo.Add(Comm.ObjToStr(txt_YR.Text.Trim().ToString()));//19 领料人


                MetarilInfo.Add(Comm.ObjToStr(this.Cmd_GFCode.Text.Trim().ToString()));//20 供方代码
                MetarilInfo.Add(Comm.ObjToStr(this.txt_GYMC.Text.Trim().ToString()));//21供方名称
                
                MetarilInfo.Add(Comm.ObjToStr(this.txt_Remark.Text.Trim().ToString()));//22 备注
        




                MetarilInfo.Add(ClientCommon._UserInfo.UserName.ToString());//23 操作人
                MetarilInfo.Add(strMode);//19 操作模式
                MetarilInfo.Add(Comm.ObjToStr(this.ultraCombo1.Text.Trim().ToString()));// 大类代码
                MetarilInfo.Add(Comm.ObjToStr(this.ultraTextEditor42.Text.Trim().ToString()));// 大类名称
                MetarilInfo.Add(Comm.ObjToStr(this.ultraTextEditor43.Text.Trim().ToString()));// 用途


                object obj = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWWLStuff", "Core.XgMes.Server.JGKC.MaterialManager.Metarial",
                    "PickingSheetAddNew", new object[] { MetarilInfo }, out strerr);
                if (obj != null && obj.ToString() == "1")
                {
                    MessageBox.Show("新增成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DoQuery();
                }
                else
                {
                    MessageBox.Show(strerr, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void SaveInvoice_No()
        {
            try
            {
                string where = "";
                string sql = "";
                string err = "";
                sql = " update PICKING_MANAGER1 t set t.invoice_no = '"+this.gd_CK.ActiveRow.Cells["INVOICE_NO"].Text.ToString()+"' where t.picking_no = '"
                    + this.gd_CK.ActiveRow.Cells["PICKING_NO"].Text.ToString() + "'";
                string dr = "";
                dr = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "ExecuteNone", new object[] { sql }, out err).ToString();
                if (dr != "1")
                {
                    MessageBox.Show(dr);
                    return;
                }

                MessageBox.Show("保存发票号成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DoQuery();
            }
            catch (Exception ex)
            {
            }
        }

        public static bool FillComboEditor(ref UltraComboEditor ulComboEditor, DataSet dset)
        {
            if (dset == null) return false;
            if (dset.Tables.Count < 1) return false;
            if (dset.Tables[0].Columns.Count < 2) return false;

            ulComboEditor.Items.Clear();
            for (int i = 0; i < dset.Tables[0].Rows.Count; i++)
                ulComboEditor.Items.Add(dset.Tables[0].Rows[i][0].ToString(), dset.Tables[0].Rows[i][1].ToString());
            return true;
        }
        private void DoQuery()
        {
            string strWhere = "";
            string sql = "";
            if (chk_Time.Checked)
            {
                strWhere += " AND  ISSUE_DATE >='" + Convert.ToDateTime(dte_BeginTime.Value.ToString()).ToString("yyyyMMdd") + "' AND ISSUE_DATE <='" + Convert.ToDateTime(dte_EndTime.Value.ToString()).ToString("yyyyMMdd") + "'";
            }
            if (Chk_CKLB.Checked && txt_ckm.Text.Trim().Length>0)
            {
                strWhere += " AND STORAGE_STYLE LIKE '%" + txt_ckm.Text.Trim().ToString() + "%' ";
            }
            if (Chk_WZDM.Checked && txt_wzdm.Text.Trim().Length > 0)
            {
                strWhere += " AND MATERIAL_CODE LIKE '%" + txt_wzdm.Text.Trim().ToString() + "%' ";
            }
            if (chk_wzmc.Checked && txt_wz.Text.Trim().Length > 0)
            {
                strWhere += " AND MATERIAL_NAME LIKE '%" + txt_wz.Text.Trim().ToString() + "%' ";
            }
            if (chk_Spet.Checked && txt_Spet.Text.Trim().Length > 0)
            {
                strWhere += " AND SPET LIKE '%" + txt_Spet.Text.Trim().ToString() + "%' ";
            }

            string err = "";
            sql = "select PICKING_NO,ISSUE_DATE,STORAGE_CODE,STORAGE_STYLE,ISSUE_STORE,PICKING_NAME,MATERIAL_CODE,MATERIAL_NAME, SPET,"
                         + "UNIT,MATERIAL, WEIGHT,SCHEDULER,PLEASE_NUM,REAL_NUM,PRICE, MONERY,FILLING_DATE, PICKING_RECODER,GF_CODE,GF_NAME,RECODER,INPUT_DATE,REMARK,USE_TO,MATERIAL_LB,MATERIAL_LB_NAME,INVOICE_NO     "
                         + " from PICKING_MANAGER1 where isvalid='1' " + strWhere;
 
                           
            DataSet ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet", new object[] { sql }, out err);
            if (err == "")
            {
                gd_CK.DataSource = ds;
            }
        }
        private void Init()
        {
            try
            {
                string strOut = "";
                string sql = "";
                DataSet ds;
                
                //审核人
                sql = "SELECT CHECKER FROM KCJ_CHECKER order by CHECKER ";
                ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet", new object[] { sql }, out strOut);
                ds.Tables[0].TableName = "KCJ_CHECKER";
                Cmb_Checker.DataSource = (DataSet)ds;
                Cmb_Checker.DisplayMember = "CHECKER";

                Cmb_Checker.SelectedRow = null;


            


            }
            catch (Exception ex)
            {
            }
        }
        private void AccpetSheet_Load(object sender, EventArgs e)
        {
            dte_BeginTime.DateTime = DateTime.Now;
            dte_EndTime.DateTime = DateTime.Now;
            date_YS.DateTime = DateTime.Now;
            date_td.DateTime = DateTime.Now;
            Init();
        }

        private void Chk_CKLB_CheckedChanged(object sender, EventArgs e)
        {
            txt_ckm.Enabled = Chk_CKLB.Checked;
        }

        private void Chk_WZDM_CheckedChanged(object sender, EventArgs e)
        {
            txt_wzdm.Enabled = Chk_WZDM.Checked;
        }

        private void Cmb_CKDM_KeyDown(object sender, KeyEventArgs e)
        {
            
            string err = "";
            string sql = "SELECT BB.MATERIAL_CODE,BB.MATERIAL_NAME,BB.SPEC,BB.MATERIAL,BB.SUBSTANCE,BB.UNITS,CC.DEPOTID,CC.DEPOTNAME,AA.STOCK_CODE FROM MATERIAL_CODE_MANAGE BB,KCJ_STORESMAGNAGER1 AA,KCJ_DEPOTMAGAGER1 CC  WHERE BB.MATERIAL_CODE=AA.MATERIALCODE(+) AND AA.STOCK_CODE=CC.DEPOT_LB(+)  "
                        + " AND CC.DEPOTID  like '" + Cmb_CKDM.Text.ToString().Trim() + "' "
                        + " ORDER BY TO_NUMBER(BB.SORT_CODE)  ASC ";
            DataSet ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet", new object[] { sql }, out err);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (err == "")
                {
                    Cmb_wzdm.Text = ds.Tables[0].Rows[0]["MATERIAL_CODE"].ToString();
                    txt_WZMC.Text = ds.Tables[0].Rows[0]["MATERIAL_NAME"].ToString();
                    txt_GGXH.Text = ds.Tables[0].Rows[0]["SPEC"].ToString();
                    txt_JLDW.Text = ds.Tables[0].Rows[0]["UNITS"].ToString();
                    txt_CZ.Text = ds.Tables[0].Rows[0]["MATERIAL"].ToString();
                    txt_DZ.Text = ds.Tables[0].Rows[0]["SUBSTANCE"].ToString();
                    Cmb_CKDM.Text = ds.Tables[0].Rows[0]["DEPOTID"].ToString();
                    cmb_StorageLB.Text = ds.Tables[0].Rows[0]["STOCK_CODE"].ToString();
                    txt_CKMC.Text = ds.Tables[0].Rows[0]["DEPOTNAME"].ToString();

                }
            }
            else
            {


                txt_WZMC.Text = "";
                txt_GGXH.Text = "";
                txt_JLDW.Text = "";
                txt_CZ.Text = "";
                txt_DZ.Text = "";
            
                cmb_StorageLB.Text = "";
                txt_CKMC.Text = "";

            }
        }

        private void Cmb_wzdm_KeyDown(object sender, KeyEventArgs e)
        {
           
            string err = "";
            string sql = "SELECT BB.MATERIAL_CODE,BB.MATERIAL_NAME,BB.SPEC,BB.MATERIAL,BB.SUBSTANCE,BB.UNITS,CC.DEPOTID,CC.DEPOTNAME,AA.STOCK_CODE FROM MATERIAL_CODE_MANAGE BB,KCJ_STORESMAGNAGER AA,KCJ_DEPOTMAGAGER CC  WHERE BB.MATERIAL_CODE=AA.MATERIALCODE(+) AND AA.STOCK_CODE=CC.DEPOT_LB(+)  "
                        + " AND BB.MATERIAL_CODE  like '" + Cmb_wzdm.Text.ToString().Trim() + "' "
                        + " ORDER BY TO_NUMBER(BB.SORT_CODE)  ASC ";
            DataSet ds = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("BJZWStuff", "Core.XgMes.Server.JGKC.SteelPlateManager.Common", "GetDataSet", new object[] { sql }, out err);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (err == "")
                {
                    Cmb_wzdm.Text = ds.Tables[0].Rows[0]["MATERIAL_CODE"].ToString();
                    txt_WZMC.Text = ds.Tables[0].Rows[0]["MATERIAL_NAME"].ToString();
                    txt_GGXH.Text = ds.Tables[0].Rows[0]["SPEC"].ToString();
                    txt_JLDW.Text = ds.Tables[0].Rows[0]["UNITS"].ToString();
                    txt_CZ.Text = ds.Tables[0].Rows[0]["MATERIAL"].ToString();
                    txt_DZ.Text = ds.Tables[0].Rows[0]["SUBSTANCE"].ToString();
                    Cmb_CKDM.Text = ds.Tables[0].Rows[0]["DEPOTID"].ToString();
                    cmb_StorageLB.Text = ds.Tables[0].Rows[0]["STOCK_CODE"].ToString();
                    txt_CKMC.Text = ds.Tables[0].Rows[0]["DEPOTNAME"].ToString();

                }
            }
            else
            {


                txt_WZMC.Text = "";
                txt_GGXH.Text = "";
                txt_JLDW.Text = "";
                txt_CZ.Text = "";
                txt_DZ.Text = "";
                Cmb_CKDM.Text = "";
                cmb_StorageLB.Text = "";
                txt_CKMC.Text = "";

            }
        }

        private void chk_wzmc_CheckedChanged(object sender, EventArgs e)
        {
            txt_wz.Enabled = chk_wzmc.Checked;
        }

        private void chk_Spet_CheckedChanged(object sender, EventArgs e)
        {
            txt_Spet.Enabled=chk_Spet.Checked  ;
        }

        private void ultraCombo1_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            FrmMaterialLBDaioage metaterial = new FrmMaterialLBDaioage();
            metaterial.StartPosition = FormStartPosition.CenterParent;
            metaterial.ShowDialog();
            /*Cmb_wzdm.Text = metaterial.strwzdm;
            txt_WZMC.Text = metaterial.strwzmc;
            txt_GGXH.Text = metaterial.strgg;
            txt_JLDW.Text = metaterial.strjldw;
            txt_CZ.Text = metaterial.strcz;
            txt_DZ.Text = metaterial.strdz;
            Cmb_CKDM.Text = metaterial.strckcode;
            cmb_StorageLB.Text = metaterial.strcklb;
            txt_CKMC.Text = metaterial.strckmc;*/
            ultraCombo1.Text = metaterial.strwzdm;
            ultraTextEditor42.Text = metaterial.strwzmc;
        }
        private void ExportLL()
        {
            string Rfilename = Application.StartupPath + "\\LLD.xls";
            /*string Efilename = "";

            if (!File.Exists(Rfilename))
            {
                MessageBox.Show("指定路径的模板文件不存在！");
                return;
            }
            saveFileDialog1.FileName = "领料单" + DateTime.Now.ToString("yyMMdd") + ".xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Efilename = saveFileDialog1.FileName;
            }
            else
                return;

            /*FileInfo Model = new FileInfo(Rfilename);
            FileInfo Rptmp = new FileInfo(Efilename);
            try
            {
                Model.CopyTo(Efilename, true);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return;
            }
            object missing = Missing.Value;
            DateTime beforeTime = DateTime.Now;
            Excel.Application myExcel = new Excel.Application();
            DateTime afterTime = DateTime.Now;
            //this.excelExporter.Export(this.gd_CK, Rfilename);

            ProcessStartInfo p = new ProcessStartInfo(Rfilename);
            p.WorkingDirectory = Path.GetDirectoryName(Rfilename);
            myExcel.Application.Workbooks.Open(Rfilename, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            //Excel.Workbook myBook = myExcel.Application.Workbooks.Add(Rfilename);
            /*Excel._Worksheet mySheet = (Excel._Worksheet)(myExcel.Worksheets.get_Item(1));
            mySheet.Activate();
            myExcel.Visible = false;*/
            Excel.ApplicationClass myExcel = new Excel.ApplicationClass();
            myExcel.Application.Workbooks.Open(Rfilename, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            
            myExcel.DisplayAlerts = false;
           
            Excel.Range RM;
            /*RM = myExcel.get_Range(myExcel.Cells[1, 1], myExcel.Cells[1, 9]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "新余新钢板材加工有限公司劳保领料单";*/

            RM = myExcel.get_Range(myExcel.Cells[2, 1], myExcel.Cells[2, 5]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignJustify;
            RM.Value = "领料班组：" + this.gd_CK.ActiveRow.Cells["PICKING_NAME"].Text;

            RM = myExcel.get_Range(myExcel.Cells[2, 6], myExcel.Cells[2, 9]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignJustify;
            RM.Value = "发料              " + System.DateTime.Now.Year + "年"
                    + System.DateTime.Now.Month + "月" + System.DateTime.Now.Day + "日";

            /*RM = myExcel.get_Range(myExcel.Cells[3, 1], myExcel.Cells[4, 1]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "领料单号";

            RM = myExcel.get_Range(myExcel.Cells[3, 2], myExcel.Cells[4, 2]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "发料仓库";

            RM = myExcel.get_Range(myExcel.Cells[3, 3], myExcel.Cells[4, 3]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "物资代码";

            RM = myExcel.get_Range(myExcel.Cells[3, 4], myExcel.Cells[4, 4]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "物资名称";

            RM = myExcel.get_Range(myExcel.Cells[3, 5], myExcel.Cells[4, 5]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "规格型号";
           
            RM = myExcel.get_Range(myExcel.Cells[3, 6], myExcel.Cells[4, 6]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "单位";

            RM = myExcel.get_Range(myExcel.Cells[3, 7], myExcel.Cells[4, 7]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "单价";

            RM = myExcel.get_Range(myExcel.Cells[3, 8], myExcel.Cells[4, 8]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "实发数量";

           
            RM = myExcel.get_Range(myExcel.Cells[3, 9], myExcel.Cells[4, 9]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "金额";*/


            int RowNum = this.gd_CK.Rows.Count;
            int RowPosition = 4;
            ArrayList TaskNoList = new ArrayList();


            try
            {
                for (int i = 0; i < RowNum; i++)
                {
                    RowPosition = 5 + i;

                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 1]);
                    RM.Value = this.gd_CK.Rows[i].Cells["PICKING_NO"].Text; ;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    /*myExcel.Cells[RowPosition, 1] = this.gd_CK.Rows[i].Cells["PICKING_NO"].Text;
                    myExcel.Cells[RowPosition, 2] = this.gd_CK.Rows[i].Cells["ISSUE_STORE"].Text;
                    myExcel.Cells[RowPosition, 3] = this.gd_CK.Rows[i].Cells["MATERIAL_CODE"].Text;
                    myExcel.Cells[RowPosition, 4] = this.gd_CK.Rows[i].Cells["MATERIAL_NAME"].Text;
                    myExcel.Cells[RowPosition, 5] = this.gd_CK.Rows[i].Cells["SPET"].Text;
                    myExcel.Cells[RowPosition, 6] = this.gd_CK.Rows[i].Cells["UNIT"].Text;
                    myExcel.Cells[RowPosition, 7] = this.gd_CK.Rows[i].Cells["PRICE"].Text;
                    myExcel.Cells[RowPosition, 8] = this.gd_CK.Rows[i].Cells["REAL_NUM"].Text;
                    myExcel.Cells[RowPosition, 9] = this.gd_CK.Rows[i].Cells["MONERY"].Text;
                    //myExcel.Cells[RowPosition, 1] = this.gd_CK.Rows[i].Cells["PICKING_NO"].Text; */

                    string BatchNo = this.gd_CK.Rows[i].Cells["ISSUE_STORE"].Text;
                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 2]);
                    RM.Value = BatchNo;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 3]);
                    RM.Value = "'" + this.gd_CK.Rows[i].Cells["MATERIAL_CODE"].Text;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 4]);
                    RM.Value = this.gd_CK.Rows[i].Cells["MATERIAL_NAME"].Text;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 5]);
                    RM.Value = this.gd_CK.Rows[i].Cells["SPET"].Text;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 6]);
                    RM.Value = this.gd_CK.Rows[i].Cells["UNIT"].Text;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 8]);
                    RM.Value = this.gd_CK.Rows[i].Cells["PICKING_NAME"].Text;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 7]);
                    RM.Value = this.gd_CK.Rows[i].Cells["REAL_NUM"].Text;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    RM = ((Excel.Range)myExcel.Cells[RowPosition, 9]);
                    RM.Value = this.gd_CK.Rows[i].Cells["USE_TO"].Text;
                    RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
                   

                   
                  

                }
            }
            catch { }
           
            /*myExcel.get_Range(myExcel.Cells[4, 1], myExcel.Cells[4, 1]).ColumnWidth = 10.5;
            myExcel.get_Range(myExcel.Cells[4, 2], myExcel.Cells[4, 2]).ColumnWidth = 8.5;
            myExcel.get_Range(myExcel.Cells[4, 3], myExcel.Cells[4, 3]).ColumnWidth = 8.5;
            myExcel.get_Range(myExcel.Cells[4, 4], myExcel.Cells[4, 4]).ColumnWidth = 8.5;
            myExcel.get_Range(myExcel.Cells[4, 5], myExcel.Cells[4, 5]).ColumnWidth = 8.5;
            myExcel.get_Range(myExcel.Cells[4, 6], myExcel.Cells[4, 6]).ColumnWidth = 5.5;
            myExcel.get_Range(myExcel.Cells[4, 7], myExcel.Cells[4, 7]).ColumnWidth = 5.5;
            myExcel.get_Range(myExcel.Cells[4, 8], myExcel.Cells[4, 8]).ColumnWidth = 6.5;
            myExcel.get_Range(myExcel.Cells[4, 9], myExcel.Cells[4, 9]).ColumnWidth = 8.8;*/
            

            /*myExcel.get_Range(myExcel.Cells[1, 1], myExcel.Cells[RowNum + 5, 41]).WrapText = true;
            myExcel.get_Range(myExcel.Cells[1, 42], myExcel.Cells[RowNum + 5, 42]).WrapText = false;
            myExcel.get_Range(myExcel.Cells[1, 43], myExcel.Cells[RowNum + 5, 43]).WrapText = true;
            myExcel.get_Range(myExcel.Cells[1, 46], myExcel.Cells[RowNum + 5, 46]).WrapText = true;*/
            RM = myExcel.get_Range(myExcel.Cells[RowNum + 5, 1], myExcel.Cells[RowNum + 5, 1]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
            RM.Value = "备注 ";
            //myExcel.Cells[RowNum + 5, 1] = "备注 ";
            RM = myExcel.get_Range(myExcel.Cells[RowNum + 5, 2], myExcel.Cells[RowNum + 5, 9]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

            //myExcel.Cells[RowNum + 6, 1] = "仓库主管: 刘飚 ";
            RM = myExcel.get_Range(myExcel.Cells[RowNum + 6, 1], myExcel.Cells[RowNum + 6, 3]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignJustify;
            RM.Value = "仓库主管: 刘飚";

            RM = myExcel.get_Range(myExcel.Cells[RowNum + 6, 4], myExcel.Cells[RowNum + 6, 4]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignJustify;
            RM.Value = "保管员: 侯建勇";

            RM = myExcel.get_Range(myExcel.Cells[RowNum + 6, 6], myExcel.Cells[RowNum + 6, 7]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignJustify;
            RM.Value = "审核人: 康安";


            RM = myExcel.get_Range(myExcel.Cells[RowNum + 6, 8], myExcel.Cells[RowNum + 6, 8]);
            RM.Select();
            RM.MergeCells = true;
            RM.Merge(0);
            RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignJustify;
            RM.Value = "领料员:  ";

            /*if (System.DateTime.Now.Hour >= 8 && System.DateTime.Now.Hour <= 18)
            {
                RM = myExcel.get_Range(myExcel.Cells[RowNum + 6, 1], myExcel.Cells[RowNum + 6, 48]);
                RM.Select();
                RM.MergeCells = true;
                RM.Merge(0);
                RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
                RM.Value = "组坯人:" + Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName + "     审核人:  " + System.DateTime.Now.Year + "年"
                    + System.DateTime.Now.Month + "月" + System.DateTime.Now.Day + "日(早) 送表时间:" + System.DateTime.Now.ToString("HH:mm");
            }
            else
            {
                RM = myExcel.get_Range(myExcel.Cells[RowNum + 6, 1], myExcel.Cells[RowNum + 6, 48]);
                RM.Select();
                RM.MergeCells = true;
                RM.Merge(0);
                RM.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
                RM.Value = "组坯人:" + Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserName + "     审核人:   " + System.DateTime.Now.Year + "年"
                    + System.DateTime.Now.Month + "月" + System.DateTime.Now.Day + "日(晚)送表时间:" + System.DateTime.Now.ToString("HH:mm");
            }*/
            /*myExcel.get_Range(myExcel.Cells[1, 1], myExcel.Cells[1, 9]).Font.Name = "Arial Narrow";
            myExcel.get_Range(myExcel.Cells[1, 1], myExcel.Cells[1, 9]).Font.Size = "14";
            myExcel.get_Range(myExcel.Cells[1, 1], myExcel.Cells[1, 9]).Font.Bold = true;*/

            myExcel.get_Range(myExcel.Cells[5, 1], myExcel.Cells[RowNum + 6, 9]).Font.Name = "Arial Narrow";
            myExcel.get_Range(myExcel.Cells[5, 1], myExcel.Cells[RowNum + 6, 9]).Font.Bold = true;

            //myExcel.get_Range(myExcel.Cells[1, 1], myExcel.Cells[RowNum + 6, 9]).Borders.get_Item(XlBordersIndex.xlEdgeLeft);
            //myExcel.get_Range(myExcel.Cells[1, 1], myExcel.Cells[RowNum + 6, 9]).Borders.get_Item = true;
            myExcel.Visible = true;
            this.Cursor = Cursors.Default;
            myExcel.DisplayAlerts = true;

        }
    }
}