基本信息
源码名称:C# 访问网页提交数据实例
源码大小:2.60M
文件格式:.zip
开发语言:C#
更新时间:2018-01-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍

此例子是通过C# 访问网页后台元素,把EXCEL文件的数据自动上传到网页中的程序

private void ForDataUpload(object obj)
        {
            int length = 100000;//EXCEL 数据长度
            //int TempRow = 0;//临时行数
            int i = 0;
            int ii = 0;
            int iii = 0;


            bool isOk = false;// 判断进程中是否有EXCEL;
            bool isExcelName = false;//excel文件名字是否存在
            bool IsGetValue = true;//得到没条记录1到10的值
            int StartTowFor = 0;//每条记录循环的开始的行数
            int EndTowFor = 0;//每条记录循环的结束的行数

            //bool IspaymentOne = false;

            //bool OnePaymentInformation = true;
            //bool OneReferenceInformation =true;
            Microsoft.Office.Interop.Excel.Worksheet sheet = null;

            //try
            //{


            mshtml.HTMLInputElement TempHtml;
            TempHtml = null;
            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
            //遍历所有选项卡
            foreach (SHDocVw.InternetExplorer Browser in shellWindows)
            {
                if (Browser.LocationURL.Contains("https://gfisasiapacific.iweb.ey.com/psp/F3PR/EMPLOYEE/ERP/c/APPLY_PAYMENTS.PAYMENT_ENTRY.GBL?FolderPath=PORTAL_ROOT_OBJECT.EPCO_ACCOUNTS_RECEIVABLE.EPAR_PAYMENTS4.EPAR_CREATE_ONLINE_PAYMENTS3.EP_PAYMENT_ENTRY_GBL&IsFolder=false&IgnoreParamTempl=FolderPath%2cIsFolder") ||
                    Browser.LocationURL.Contains("https://gfisasiapacific.iweb.ey.com/psp/F3PR/EMPLOYEE/ERP/c/APPLY_PAYMENTS.PAYMENT_ENTRY.GBL?PORTALPARAM_PTCNAV=EP_PAYMENT_ENTRY_GBL&EOPP.SCNode=ERP&EOPP.SCPortal=EMPLOYEE&EOPP.SCName=EPCO_ACCOUNTS_RECEIVABLE&EOPP.SCLabel=Online%20Payments&EOPP.SCFName=EPAR_CREATE_ONLINE_PAYMENTS3&EOPP.SCSecondary=true&EOPP.SCPTfname=EPAR_CREATE_ONLINE_PAYMENTS3&FolderPath=PORTAL_ROOT_OBJECT.EPCO_ACCOUNTS_RECEIVABLE.EPAR_PAYMENTS4.EPAR_CREATE_ONLINE_PAYMENTS3.EP_PAYMENT_ENTRY_GBL&IsFolder=false") ||
                    Browser.LocationURL.Contains("https://gfisasiapacific.iweb.ey.com/psp/F3PR/EMPLOYEE/ERP/h/?tab=DEFAULT"))
                                                  
                {
                    ie = Browser;
                    mshtml.HTMLDocument docTemp = (mshtml.HTMLDocument)Browser.Document;
                    doc = docTemp;
                    
                    mshtml.IHTMLElementCollection inputs1 = (mshtml.IHTMLElementCollection)docTemp.all.tags("INPUT");
                    mshtml.HTMLInputElement InvoiceDate1 = (mshtml.HTMLInputElement)inputs1.item("ptnav2srchinput", 0);
                    
                    if (InvoiceDate1 != null)
                    {
                        TempHtml = InvoiceDate1;
                        break;

                    }

                }
            }


            if (TempHtml == null && OneAccWeb==true)
            {
                OneAccWeb = true;

                MessageBox.Show("没找到对应的网页!", "提示信息!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                OneAccWeb = false ;
            }

            //获取当前EXCEL
            Hashtable rot = GetRunningObjectTable();

            //isOk = false; //判断进程中是否有EXCEL;
            ExitFor = false;//判断是否需要退出循环;

            foreach (object o in rot.Values)
            {
                Microsoft.Office.Interop.Excel.Application a = o as Microsoft.Office.Interop.Excel.Application;

                if (a != null)
                {
                    isOk = true;

                    Microsoft.Office.Interop.Excel.Workbooks wbooks = a.Workbooks;
                    isExcelName = false;
                    foreach (Microsoft.Office.Interop.Excel.Workbook wb in wbooks)
                    {
                        if (wb.Name.IndexOf(ExcelFileName) != -1)
                        {
                            isExcelName = true;
                            sheet = wb.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;

                            if (sheet.Cells[1, 1].value != "Account" || sheet.Cells[1, 2].value != "Deposit Unit")
                            {
                                MessageBox.Show("没有找到对应的数据列!", "提示信息!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }

                            for (i = 2; i < length; i )
                            {

                                try
                                {
                                    if (ie.LocationURL != "https://gfisasiapacific.iweb.ey.com/psp/F3PR/EMPLOYEE/ERP/h/?tab=DEFAULT")
                                    //if (ie.LocationURL != "https://gfisasiapacific.iweb.ey.com/psp/F3PR/EMPLOYEE/ERP/c/APPLY_PAYMENTS.PAYMENT_ENTRY.GBL?FolderPath=PORTAL_ROOT_OBJECT.EPCO_ACCOUNTS_RECEIVABLE.EPAR_PAYMENTS4.EPAR_CREATE_ONLINE_PAYMENTS3.EP_PAYMENT_ENTRY_GBL&IsFolder=false&IgnoreParamTempl=FolderPath%2cIsFolder")
                                    {
                                        ie.Navigate("https://gfisasiapacific.iweb.ey.com/psp/F3PR/EMPLOYEE/ERP/c/APPLY_PAYMENTS.PAYMENT_ENTRY.GBL?FolderPath=PORTAL_ROOT_OBJECT.EPCO_ACCOUNTS_RECEIVABLE.EPAR_PAYMENTS4.EPAR_CREATE_ONLINE_PAYMENTS3.EP_PAYMENT_ENTRY_GBL&IsFolder=false&IgnoreParamTempl=FolderPath%2cIsFolder");
                                    }
                                }
                                catch (Exception)
                                {
                                    continue;
                                    //throw;
                                }
                              

                                if ((sheet.Cells[i, 6].value == "" || sheet.Cells[i, 6].value == null) && (sheet.Cells[i, 2].value == "" || sheet.Cells[i, 2].value == null))
                                {
                                    timerNext.Stop();
                                    MessageBox.Show("数据已经全部完成!", "提示信息!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                    break;

                                }
                                PaymentCount = 0;
                                SaveDepositID = "";

                                //IspaymentOne = true;

                                isMulPayments = false; //Payments多条的时候点击按钮
                                isRefValueClick = false; //RefValue 多条时点击按钮


                                //判断记录最开始一条,然后取的前几列的值
                                m_bPause1 = true;

                                EnterAddNewOne();

                                StartTowFor = i;

                                strType1 = sheet.Cells[i, 2].value;
                                strType2 = sheet.Cells[i, 3].value;
                                strType3 = sheet.Cells[i, 4].value;
                                strType4 = sheet.Cells[i, 5].value;
                                strType5 = Convert.ToInt32(sheet.Cells[i, 6].value);
                                strType6 = Convert.ToString(string.Format("{0:M/d/yyyy}", sheet.Cells[i, 7].value));

                                //IsGetValue = false;

                                isDepositUnit = true;

                                isTotals = true;

                                while (m_bPause1)
                                {
                                    Application.DoEvents();
                                }
                                Invoke(new MethodInvoker(delegate
                                {
                                    label1.Text = i.ToString();
                                }));
                                System.Threading.Thread.Sleep(1000);


                                for (int iLocal = i; iLocal < length; iLocal )
                                {
                                    StartTowFor = i;
                                    if (sheet.Cells[iLocal, 1].value == null)
                                    {
                                        i = iLocal - 1;
                                        break;
                                    }
                                    if (sheet.Cells[iLocal, 6].value != null && iLocal != i)
                                    {
                                        i = iLocal - 1;
                                        break;
                                    }
                                }

                                EndTowFor = i 1;
                                //int TempCount = 0;


                                //开始一个填表循环
                                for (ii = StartTowFor; ii < EndTowFor; ii )
                                {
                                    MuNext = false;

                                    ListNo = false;
                                    SaveDepositID = "";

                                    m_bPause1 = true;


                                    string TempNum = "";

                                    //m_bPause2 = true;
                                    //判断Payment ID是否连续
                                    //if (sheet.Cells[ii, 9].value != null)
                                    if (sheet.Cells[ii, 8].value != "" && ii == StartTowFor)
                                    {

                                        strType11 = "";

                                        PaymentCount = PaymentCount 1;
                                        strType7 = sheet.Cells[ii, 8].value;
                                        TempNum = Convert.ToString(sheet.Cells[ii, 9].value);

                                        if (IsNumeric(TempNum))
                                        {

                                            strType8 = sheet.Cells[ii, 9].value;
                                        }
                                        else
                                        {
                                            strType8 = 0;
                                        }


                                        if (sheet.Cells[ii, 10].value != null)
                                        {
                                            strType91 = sheet.Cells[ii, 10].value;
                                            strType92 = sheet.Cells[ii, 11].value;
                                        }
                                        else
                                        {
                                            strType91 = 0;
                                        }


                                        strType9 = sheet.Cells[ii, 12].value;
                                        strType10 = sheet.Cells[ii, 13].value;
                                        if (strType10 != null)
                                        {
                                            strType10 = strType10.Trim();
                                        }

                                        if (strType11 == "")
                                        {
                                            strType11 = sheet.Cells[ii, 19].value;
                                        }
                                        isPayments = true;

                                        //暂停循环
                                        while (m_bPause1)
                                        {
                                            Application.DoEvents();
                                        }
                                        Invoke(new MethodInvoker(delegate
                                        {
                                            label1.Text = i.ToString();
                                        }));
                                        System.Threading.Thread.Sleep(1000);
                                    }

                                    if (sheet.Cells[ii, 8].value != null && ii != StartTowFor)
                                    {

                                        PaymentCount = PaymentCount 1;
                                        strType7 = sheet.Cells[ii, 8].value;

                                        TempNum = Convert.ToString(sheet.Cells[ii, 9].value);
                                        if (IsNumeric(TempNum))
                                        {

                                            strType8 = sheet.Cells[ii, 9].value;
                                        }
                                        else
                                        {
                                            strType8 = 0;
                                        }
                                       

                                        if (sheet.Cells[ii, 10].value != null)
                                        {
                                            strType91 = sheet.Cells[ii, 10].value;
                                            strType92 = sheet.Cells[ii, 11].value;
                                        }
                                        else
                                        {
                                            strType91 = 0;
                                        }

                                        strType9 = sheet.Cells[ii, 12].value;
                                        strType10 = sheet.Cells[ii, 13].value;
                                        if (strType10 !=null)
                                        {
                                            strType10 = strType10.Trim();
                                        }
                                        
                                        //strType11 = sheet.Cells[ii, 19].value;
                                        m_bPause1 = true;


                                        isMulPayments = true;
                                        System.Threading.Thread.Sleep(1000);
                                        //isPayments = true;

                                        while (m_bPause1)
                                        {
                                            Application.DoEvents();
                                        }
                                        Invoke(new MethodInvoker(delegate
                                        {
                                            label1.Text = i.ToString();
                                        }));
                                        System.Threading.Thread.Sleep(1000);


                                    
                                        m_bPause1 = true;
                                        isPayments = true;

                                        //isPayments = true;
                                        while (m_bPause1)
                                        {
                                            Application.DoEvents();
                                        }
                                        Invoke(new MethodInvoker(delegate
                                        {
                                            label1.Text = i.ToString();
                                        }));
                                        System.Threading.Thread.Sleep(1000);
                                    }


                                    //检查Ref是不是多行
                                    if (sheet.Cells[ii 1, 8].value == null) //sheet.Cells[ii, 9].value == null && sheet.Cells[ii, 5].value == null
                                    {

                                        AList.Clear();
                                        for (iii = ii; iii < EndTowFor; iii )
                                        {

                                            if (iii == ii)
                                            {
                                                AList.Add(sheet.Cells[iii, 13].value);
                                            }
                                            else
                                            {
                                                if (sheet.Cells[iii, 8].value == null)
                                                {
                                                    AList.Add(sheet.Cells[iii, 13].value);

                                                    if (iii == EndTowFor - 1 || sheet.Cells[iii 1, 9].value != null)
                                                    {
                                                        ii = iii - 1;
                                                        //执行按下多行的Ref按钮
                                                       
                                                        isRefValueClick = true;
                                                        //m_bPause1 = false;
                                                        MuNext = true;
                                                        break;
                                                    }
                                                   
                                                }
                                            }
                                        }

                                        //20160908
                                        if (ii 1 != EndTowFor)
                                        {
                                            //暂停循环
                                            while (m_bPause1)
                                            {
                                                Application.DoEvents();
                                            }
                                            Invoke(new MethodInvoker(delegate
                                            {
                                                label1.Text = i.ToString();
                                            }));
                                            System.Threading.Thread.Sleep(1000);
                                        }
                                    }

                                    ////暂停循环
                                    //while (m_bPause1)
                                    //{
                                    //    Application.DoEvents();
                                    //}
                                    //Invoke(new MethodInvoker(delegate
                                    //{
                                    //    label1.Text = i.ToString();
                                    //}));
                                    //System.Threading.Thread.Sleep(1000);


                                    if (ii 1 == EndTowFor)
                                    {
                                        //m_bPause2 = true;
                                        ListNo = true;
                                        ClickComments = true;
                                        m_bPause1 = true;
                                    }

                                    //if (ii 1 == EndTowFor-1)
                                    //{
                                    //    MuNext = true;
                                    //}


                                    if (MuNext == false)
                                    {
                                        //暂停循环
                                        while (m_bPause1)
                                        {
                                            Application.DoEvents();
                                            for (int iiiii = 1; iiiii < 5; iiiii )
                                            {
                                                System.Threading.Thread.Sleep(1000);
                                            }
                                            m_bPause1 = false;
                                            
                                        }
                                        Invoke(new MethodInvoker(delegate
                                        {
                                            label1.Text = i.ToString();
                                        }));
                                        System.Threading.Thread.Sleep(1000);
                                    }

                                  

                                    //保存完成以后取值
                                    if (SaveDepositID != "")
                                    {
                                        sheet.Cells[StartTowFor, 14].value = SaveDepositID;
                                    }

                                } // for ii 结束

                            }

                            ExitFor = true;
                            break;
                        }

                    }

                    if (!isExcelName)
                    {
                        MessageBox.Show("没有找到需要上传Excel!", "提示信息!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    //a.DisplayAlerts = false;

                    //a.Quit();

                }
                if (ExitFor == true)
                {
                    break;
                }
            }//for

            if (isOk == false)
            {
                MessageBox.Show("没有打开的EXCEL!", "提示信息!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            timerNext.Stop();

            //catch (Exception ex)
            //{

            //    //throw;
            //    if (ex.Message == "Object reference not set to an instance of an object.")
            //    {
            //        MessageBox.Show("数据获取不完全,有字段值没有获取到!");
            //    }
            //    else
            //    {
            //        MessageBox.Show("网页有错误!");
            //    }
            //    //Object reference not set to an instance of an object.
            //    //MessageBox.Show(ex.Message);

            //    return;
            //}

        }