漫漫技术人生路

C#

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  private string _strStep="P1";
  private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  {

   if (_strStep == "P1")
   {
    string strUID = "sylar.wang@sdccn.com";
    string strPWD = "logticket";
    webBrowser1.Document.GetElementById("uid").InnerText=strUID;
    webBrowser1.Document.GetElementById("pwd").InnerText = strPWD;
    webBrowser1.Document.GetElementById("btnSubmit").InvokeMember("click");
    _strStep = "P2";
   }
   if (_strStep == "P2")
   {

    webBrowser1.Document.GetElementById("ddlProduct").GetElementsByTagName("option")[7].SetAttribute("selected", "true");
    webBrowser1.Document.GetElementById("ibtSubmit").InvokeMember("click");
    _strStep = "P3";
   }   
  }


 //解析网页,获取html元素,而后再取出值放到datatable中
   private DataTable CreateDataTable()
        {
            HtmlElementCollection htmlElements = webBrowser1.Document.GetElementsByTagName("table");          
            DataTable dt = null;
            DataRow dr = null;
            string strXmlFile = ConfigurationManager.AppSettings["XmlFile"].ToString();
            //string strXmlFile = "d:\\11.xml";
            int intTabCout = htmlElements.Count;
            string strValue = "";
            string[] arrValue = null;
            foreach (HtmlElement htmlElement in htmlElements)
            {
                if (htmlElement != htmlElements[intTabCout - 1]) continue;
                HtmlElementCollection htmlRows = htmlElement.GetElementsByTagName("tr");
                HtmlElementCollection htmlCells = null;
                foreach (HtmlElement htmlRow in htmlRows)
                {
                    if (htmlRow == htmlRows[0]) continue;
                    htmlCells = htmlRow.GetElementsByTagName("td");
                    //BuildHeader(ref dt, htmlCells);
                    BuildHeader(ref dt, strXmlFile);
                    dr = dt.NewRow();
                    int intII = 0;
                    string strSplitor = "\r\n";                 
                     string strDtValue="";
                    string[] arrDTValue=null;
                    string[] arrDDValue=null;
                    foreach (HtmlElement htmlCell in htmlCells)
                    {
      if (htmlCell.InnerText != null)
      {
       strValue = htmlCell.InnerText.Trim();
       strValue = strValue.Replace("(Fixed by ","").Replace(")","");
       strValue = strValue.Replace("(Last fix by ", "").Replace(")", "");
      }
                        if (strValue.Contains(strSplitor))
                        {
                            arrValue = strValue.Split(new string[] { strSplitor }, StringSplitOptions.RemoveEmptyEntries);
                            for (int intIII = 0; intIII < arrValue.Length; intIII++)
                            {
                                strDtValue = arrValue[intIII];
                                if (arrValue[intIII].Contains("PM") || arrValue[intIII].Contains("AM"))
                                {
                                    arrDTValue = strDtValue.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                    arrDDValue = arrDTValue[0].Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                                    strDtValue = arrDDValue[2]+"-"+string.Format("{0:D2}",int.Parse(arrDDValue[0]))+"-"+string.Format("{0:D2}",int.Parse(arrDDValue[1]));
                                }
                                dr[intII++] = strDtValue;
                                if (intII > dt.Columns.Count - 1) break;
                            }
                        }
                        else
                        {
                            dr[intII++] = strValue;                          
                        }
                        if (intII > dt.Columns.Count - 1) break;
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

 //从datagridview导出数据到excel
  private void ExportToExcel()
  {
   DataGridViewRowCollection rows = this.dataGridView1.Rows;
   DataGridViewColumn col =null;
   string[] ArrColName = new string[this.dataGridView1.Columns.Count];
   object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.ApplicationClass appc = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook wb = appc.Application.Workbooks.Add(true);
            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range range = ws.get_Range("A1", "I1");
            col = this.dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
            ws.Cells[1, 1] = col.HeaderText;//第一行第一列
            ArrColName[0] = col.Name;
            for (int ii = 1; ii < this.dataGridView1.Columns.Count; ii++)
            {
             col = this.dataGridView1.Columns.GetNextColumn(col, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
             ws.Cells[1, ii+1] = col.HeaderText;
             ArrColName[ii] = col.Name;
            }          
            range.EntireColumn.AutoFit();
            range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
            range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MidnightBlue);
            DataGridViewRow row = null;
           
            for (int i = 0; i < rows.Count; i++)
            {
             row = rows[i];
                for (int ii = 0; ii < row.Cells.Count; ii++)
                {
                 ws.Cells[i+2, ii+1] = row.Cells[ArrColName[ii]].Value;
                }
            }           
            range = ws.get_Range("A1", "I" + (rows.Count+1));
            range.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            range.Select();
            appc.Application.Visible = true;
   //以下的注释行是用来保存用的
            //wb.SaveAs("d:\\11.xls", missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing);
            //appc.Application.ActiveWorkbook.Close(false,System.Reflection.Missing.Value,false);
            //appc.Application.Quit();
            appc = null;
  }

posted on 2008-03-12 10:15  javaca88  阅读(345)  评论(0编辑  收藏  举报